@@ -20,15 +20,15 @@ NeotestAdapter.root = lib.files.match_root_pattern("Gemfile", ".rspec", ".gitign
2020--- @param file_path string
2121--- @return boolean
2222function NeotestAdapter .is_test_file (file_path )
23- return vim .endswith (file_path , " _spec.rb" )
23+ return vim .endswith (file_path , " _spec.rb" )
2424end
2525
2626--- Given a file path, parse all the tests within it.
2727--- @async
2828--- @param file_path string Absolute file path
2929--- @return neotest.Tree | nil
3030function NeotestAdapter .discover_positions (path )
31- local query = [[
31+ local query = [[
3232 ((call
3333 method: (identifier) @func_name (#match? @func_name "^(describe|context|feature)$")
3434 arguments: (argument_list (_) @namespace.name)
@@ -45,89 +45,95 @@ function NeotestAdapter.discover_positions(path)
4545 )) @test.definition
4646 ]]
4747
48- return lib .treesitter .parse_positions (path , query , {
49- nested_tests = true ,
50- require_namespaces = true ,
51- position_id = " require('neotest-rspec.utils').generate_treesitter_id" ,
52- })
48+ return lib .treesitter .parse_positions (path , query , {
49+ nested_tests = true ,
50+ require_namespaces = true ,
51+ position_id = " require('neotest-rspec.utils').generate_treesitter_id" ,
52+ })
5353end
5454
5555--- @param test_name string
5656--- @return string
5757local function clean_test_name (test_name )
58- if string.sub (test_name , - 1 ) == ' "' or string.sub (test_name , - 1 ) == " '" then test_name = test_name :sub (1 , - 2 ) end
59- if string.sub (test_name , 1 , 1 ) == ' "' or string.sub (test_name , 1 , 1 ) == " '" then
60- test_name = test_name :sub (2 , # test_name )
61- end
62- return test_name
58+ if string.sub (test_name , - 1 ) == ' "' or string.sub (test_name , - 1 ) == " '" then
59+ test_name = test_name :sub (1 , - 2 )
60+ end
61+ if string.sub (test_name , 1 , 1 ) == ' "' or string.sub (test_name , 1 , 1 ) == " '" then
62+ test_name = test_name :sub (2 , # test_name )
63+ end
64+ return test_name
6365end
6466
6567--- @return string
6668local function get_rspec_cmd ()
67- return vim .tbl_flatten ({
68- " bundle" ,
69- " exec" ,
70- " rspec" ,
71- })
69+ return vim .tbl_flatten ({
70+ " bundle" ,
71+ " exec" ,
72+ " rspec" ,
73+ })
7274end
7375
7476--- @param args neotest.RunArgs
7577--- @return neotest.RunSpec | nil
7678function NeotestAdapter .build_spec (args )
77- local position = args .tree :data ()
78- local results_path = async .fn .tempname ()
79-
80- local script_args = vim .tbl_flatten ({
81- " -f" ,
82- " json" ,
83- " -o" ,
84- results_path ,
85- " -f" ,
86- " progress" ,
87- })
88-
89- local function run_by_filename ()
90- table.insert (script_args , position .path )
91- end
92-
93- local function run_by_test_name ()
94- table.insert (
95- script_args ,
96- vim .tbl_flatten ({
97- " -e" ,
98- clean_test_name (position .name ),
99- })
100- )
101- end
102-
103- local function run_by_line_number ()
104- table.insert (
105- script_args ,
106- vim .tbl_flatten ({
107- position .path .. " :" .. tonumber (position .range [1 ] + 1 ),
108- })
109- )
110- end
111-
112- if position .type == " file" then run_by_filename () end
113-
114- if position .type == " test" or (position .type == " namespace" and vim .bo .filetype ~= " neotest-summary" ) then
115- run_by_line_number ()
116- end
117-
118- if position .type == " dir" and vim .bo .filetype == " neotest-summary" then run_by_filename () end
119-
120- local command = vim .tbl_flatten ({
121- get_rspec_cmd (),
122- script_args ,
123- })
124-
125- return {
126- command = command ,
127- context = {
128- results_path = results_path ,
129- },
130- }
79+ local position = args .tree :data ()
80+ local results_path = async .fn .tempname ()
81+
82+ local script_args = vim .tbl_flatten ({
83+ " -f" ,
84+ " json" ,
85+ " -o" ,
86+ results_path ,
87+ " -f" ,
88+ " progress" ,
89+ })
90+
91+ local function run_by_filename ()
92+ table.insert (script_args , position .path )
93+ end
94+
95+ local function run_by_test_name ()
96+ table.insert (
97+ script_args ,
98+ vim .tbl_flatten ({
99+ " -e" ,
100+ clean_test_name (position .name ),
101+ })
102+ )
103+ end
104+
105+ local function run_by_line_number ()
106+ table.insert (
107+ script_args ,
108+ vim .tbl_flatten ({
109+ position .path .. " :" .. tonumber (position .range [1 ] + 1 ),
110+ })
111+ )
112+ end
113+
114+ if position .type == " file" then
115+ run_by_filename ()
116+ end
117+
118+ if position .type == " test" or (position .type == " namespace" and vim .bo .filetype ~= " neotest-summary" ) then
119+ run_by_line_number ()
120+ end
121+
122+ if position .type == " dir" and vim .bo .filetype == " neotest-summary" then
123+ run_by_filename ()
124+ end
125+
126+ local command = vim .tbl_flatten ({
127+ get_rspec_cmd (),
128+ script_args ,
129+ })
130+
131+ return {
132+ command = command ,
133+ context = {
134+ results_path = results_path ,
135+ },
136+ }
131137end
132138
133139--- @async
@@ -136,44 +142,44 @@ end
136142--- @param tree neotest.Tree
137143--- @return neotest.Result[]
138144function NeotestAdapter .results (spec , result , tree )
139- local output_file = spec .context .results_path
140-
141- local ok , data = pcall (lib .files .read , output_file )
142- if not ok then
143- logger .error (" No test output file found:" , output_file )
144- return {}
145- end
146-
147- local ok , parsed_data = pcall (vim .json .decode , data , { luanil = { object = true } })
148- if not ok then
149- logger .error (" Failed to parse test output:" , output_file )
150- return {}
151- end
152-
153- local ok , results = pcall (utils .parse_json_output , parsed_data , output_file )
154- if not ok then
155- logger .error (" Failed to get test results:" , output_file )
156- return {}
157- end
158-
159- return results
145+ local output_file = spec .context .results_path
146+
147+ local ok , data = pcall (lib .files .read , output_file )
148+ if not ok then
149+ logger .error (" No test output file found:" , output_file )
150+ return {}
151+ end
152+
153+ local ok , parsed_data = pcall (vim .json .decode , data , { luanil = { object = true } })
154+ if not ok then
155+ logger .error (" Failed to parse test output:" , output_file )
156+ return {}
157+ end
158+
159+ local ok , results = pcall (utils .parse_json_output , parsed_data , output_file )
160+ if not ok then
161+ logger .error (" Failed to get test results:" , output_file )
162+ return {}
163+ end
164+
165+ return results
160166end
161167
162168local is_callable = function (obj )
163- return type (obj ) == " function" or (type (obj ) == " table" and obj .__call )
169+ return type (obj ) == " function" or (type (obj ) == " table" and obj .__call )
164170end
165171
166172setmetatable (NeotestAdapter , {
167- __call = function (_ , opts )
168- if is_callable (opts .rspec_cmd ) then
169- get_rspec_cmd = opts .rspec_cmd
170- elseif opts .rspec_cmd then
171- get_rspec_cmd = function ()
172- return opts .rspec_cmd
173- end
174- end
175- return NeotestAdapter
176- end ,
173+ __call = function (_ , opts )
174+ if is_callable (opts .rspec_cmd ) then
175+ get_rspec_cmd = opts .rspec_cmd
176+ elseif opts .rspec_cmd then
177+ get_rspec_cmd = function ()
178+ return opts .rspec_cmd
179+ end
180+ end
181+ return NeotestAdapter
182+ end ,
177183})
178184
179185return NeotestAdapter
0 commit comments