@@ -20,7 +20,7 @@ M.Adapter = { name = "neotest-kotest" }
20
20
--- @param dir string @Directory to treat as cwd
21
21
--- @return string | nil @Absolute root dir of test suite
22
22
function M .Adapter .root (dir )
23
- return lib .files .match_root_pattern (" gradlew" )(dir )
23
+ return lib .files .match_root_pattern (" gradlew" )(dir )
24
24
end
25
25
26
26
--- Filter directories when searching for test files
30
30
--- @param root string Root directory of project
31
31
--- @return boolean
32
32
function M .Adapter .filter_dir (name , rel_path , root )
33
- return filter .test_directory (name )
33
+ return filter .test_directory (name )
34
34
end
35
35
36
36
--- @async
37
37
--- @param file_path string
38
38
--- @return boolean
39
39
function M .Adapter .is_test_file (file_path )
40
- return filter .is_test_file (file_path )
40
+ return filter .is_test_file (file_path )
41
41
end
42
42
43
43
--- Given a file path, parse all the tests within it.
44
44
--- @async
45
45
--- @param file_path string Absolute file path
46
46
--- @return neotest.Tree | nil
47
47
function M .Adapter .discover_positions (file_path )
48
- local positions = lib .treesitter .parse_positions (file_path , treesitter_query , {
49
- nested_namespaces = true ,
50
- nested_tests = false ,
51
- })
48
+ local positions = lib .treesitter .parse_positions (file_path , treesitter_query , {
49
+ nested_namespaces = true ,
50
+ nested_tests = false ,
51
+ })
52
52
53
- return positions
53
+ return positions
54
54
end
55
55
56
+ --- Determines the package of a directory
57
+ --- @param dir string
58
+ --- @return string ? package
59
+ local function dir_determine_package (dir )
60
+ if not lib .files .is_dir (dir ) then
61
+ error (string.format (" expected '%s' be a directory, but it's not" , dir ))
62
+ end
63
+
64
+ local test_file = nil
65
+ local files = vim .fn .globpath (dir , " **/*.kt" , false , true )
66
+ for _ , file in ipairs (files ) do
67
+ if filter .is_test_file (file ) then
68
+ test_file = file
69
+ break
70
+ end
71
+ end
72
+
73
+ if test_file == nil then
74
+ return nil
75
+ end
76
+
77
+ return position_parser .get_first_match_string (test_file , package_query )
78
+ end
79
+
80
+ --- @class Context
81
+ --- @field results_path string path to the results file
82
+ --- @field path string path to the directory /file
83
+
84
+ --- @class neotest.RunSpec
85
+ --- @field cwd string ?
86
+ --- @field context Context
87
+ --- @field command string
88
+
56
89
--- @param args neotest.RunArgs
57
90
--- @return nil | neotest.RunSpec | neotest.RunSpec[]
58
91
function M .Adapter .build_spec (args )
59
- local results_path = async .fn .tempname () .. " .json"
60
-
61
- -- Write something so there is a place to stream to...
62
- lib .files .write (results_path , " " )
63
-
64
- local tree = args .tree
65
-
66
- if not tree then
67
- return
68
- end
69
-
70
- local pos = tree :data ()
71
-
72
- local root = M .Adapter .root (pos .path )
73
- local pkg = position_parser .get_first_match_string (pos .path , package_query )
74
- local className = position_parser .get_first_match_string (pos .path , class_query )
75
- local specPackage = pkg .. " ." .. className
76
- local tests = " *"
77
-
78
- local gradle_command = command .parse (tests , specPackage , results_path )
79
-
80
- local stream_data , stop_stream = lib .files .stream_lines (results_path )
81
-
82
- print (" command: " .. gradle_command )
83
-
84
- local all_results = {}
85
-
86
- return {
87
- command = gradle_command ,
88
- cwd = root ,
89
- context = {
90
- all_results = all_results ,
91
- results_path = results_path ,
92
- file = pos .path ,
93
- stop_stream = stop_stream ,
94
- },
95
- stream = function ()
96
- return function ()
97
- local new_results = stream_data ()
98
- local success , parsed_result = pcall (output_parser .parse_lines , new_results , pos .path , specPackage )
99
- if not success then
100
- print (" An error ocurred while attempting to stream data to result: " ..
101
- vim .inspect (err ) .. " new_results: " .. vim .inspect (new_results ))
102
- return nil
103
- else
104
- -- merge the parsed results with all results...
105
- for k , v in pairs (parsed_result ) do all_results [k ] = v end
106
- return parsed_result
107
- end
108
- end
109
- end ,
110
- }
92
+ local tree = args .tree
93
+ if not tree then
94
+ return
95
+ end
96
+
97
+ --- @type string
98
+ local results_path = async .fn .tempname () .. " .txt"
99
+ local pos = tree :data ()
100
+ local tests = " *"
101
+
102
+ --- @type neotest.RunSpec
103
+ local run_spec = {
104
+ cwd = M .Adapter .root (pos .path ),
105
+ context = {
106
+ results_path = results_path ,
107
+ path = pos .path ,
108
+ },
109
+ }
110
+
111
+ if pos .type == " dir" then
112
+ local package = dir_determine_package (pos .path ) .. " .*"
113
+ run_spec .command = command .parse (tests , package , results_path )
114
+ elseif pos .type == " file" or pos .type == " namespace" or pos .type == " test" then
115
+ local package = string.format (
116
+ " %s.%s" ,
117
+ position_parser .get_first_match_string (pos .path , package_query ),
118
+ position_parser .get_first_match_string (pos .path , class_query )
119
+ )
120
+
121
+ run_spec .command = command .parse (tests , package , results_path )
122
+ end
123
+
124
+ print (run_spec .command )
125
+
126
+ return run_spec
111
127
end
112
128
113
129
--- @class neotest.Result
122
138
--- @param tree neotest.Tree
123
139
--- @return table<string , neotest.Result>
124
140
function M .Adapter .results (spec , result , tree )
125
- spec .context .stop_stream ()
126
- return spec .context .all_results
141
+ local result_path = spec .context .results_path
142
+ local path = spec .context .path
143
+
144
+ --- @type string[]
145
+ local lines = lib .files .read_lines (result_path )
146
+ return output_parser .parse_lines (lines , path )
127
147
end
128
148
129
149
return M .Adapter
0 commit comments