@@ -20,7 +20,7 @@ def test_handler
2020 file = Tempfile . new ( %w[ test- .test ] )
2121 file . puts ( "test" )
2222
23- result = run_cli ( "ast" , file : file )
23+ result = run_cli ( "ast" , contents : file )
2424 assert_equal ( "\" test\\ n\" + \" test\\ n\" \n " , result . stdio )
2525 ensure
2626 SyntaxTree ::HANDLERS . delete ( ".test" )
@@ -32,10 +32,7 @@ def test_ast
3232 end
3333
3434 def test_ast_syntax_error
35- file = Tempfile . new ( %w[ test- .rb ] )
36- file . puts ( "foo\n <>\n bar\n " )
37-
38- result = run_cli ( "ast" , file : file )
35+ result = run_cli ( "ast" , contents : "foo\n <>\n bar\n " )
3936 assert_includes ( result . stderr , "syntax error" )
4037 end
4138
@@ -45,18 +42,13 @@ def test_check
4542 end
4643
4744 def test_check_unformatted
48- file = Tempfile . new ( %w[ test- .rb ] )
49- file . write ( "foo" )
50-
51- result = run_cli ( "check" , file : file )
45+ result = run_cli ( "check" , contents : "foo" )
5246 assert_includes ( result . stderr , "expected" )
5347 end
5448
5549 def test_check_print_width
56- file = Tempfile . new ( %w[ test- .rb ] )
57- file . write ( "#{ "a" * 40 } + #{ "b" * 40 } \n " )
58-
59- result = run_cli ( "check" , "--print-width=100" , file : file )
50+ contents = "#{ "a" * 40 } + #{ "b" * 40 } \n "
51+ result = run_cli ( "check" , "--print-width=100" , contents : contents )
6052 assert_includes ( result . stdio , "match" )
6153 end
6254
@@ -104,15 +96,12 @@ def test_write
10496 file = Tempfile . new ( %w[ test- .test ] )
10597 filepath = file . path
10698
107- result = run_cli ( "write" , file : file )
99+ result = run_cli ( "write" , contents : file )
108100 assert_includes ( result . stdio , filepath )
109101 end
110102
111103 def test_write_syntax_tree
112- file = Tempfile . new ( %w[ test- .rb ] )
113- file . write ( "<>" )
114-
115- result = run_cli ( "write" , file : file )
104+ result = run_cli ( "write" , contents : "<>" )
116105 assert_includes ( result . stderr , "syntax error" )
117106 end
118107
@@ -146,19 +135,15 @@ def test_no_arguments_no_tty
146135 def test_generic_error
147136 SyntaxTree . stub ( :format , -> ( *) { raise } ) do
148137 result = run_cli ( "format" )
138+
149139 refute_equal ( 0 , result . status )
150140 end
151141 end
152142
153143 def test_plugins
154- Dir . mktmpdir do |directory |
155- Dir . mkdir ( File . join ( directory , "syntax_tree" ) )
156- $:. unshift ( directory )
144+ with_plugin_directory do |directory |
145+ directory . plugin ( "plugin" , "puts 'Hello, world!'" )
157146
158- File . write (
159- File . join ( directory , "syntax_tree" , "plugin.rb" ) ,
160- "puts 'Hello, world!'"
161- )
162147 result = run_cli ( "format" , "--plugins=plugin" )
163148
164149 assert_equal ( "Hello, world!\n test\n " , result . stdio )
@@ -181,85 +166,67 @@ def test_language_server
181166 end
182167
183168 def test_config_file
184- config_file = File . join ( Dir . pwd , SyntaxTree ::CLI ::CONFIG_FILE )
185169 config = <<~TXT
186170 --print-width=100
187171 --plugins=plugin
188172 TXT
189- File . write ( config_file , config )
190173
191- Dir . mktmpdir do | directory |
192- Dir . mkdir ( File . join ( directory , "syntax_tree" ) )
193- $: . unshift ( directory )
174+ with_config_file ( config ) do
175+ with_plugin_directory do | directory |
176+ directory . plugin ( "plugin" , "puts 'Hello, world!'" )
194177
195- File . write (
196- File . join ( directory , "syntax_tree" , "plugin.rb" ) ,
197- "puts 'Hello, world!'"
198- )
178+ contents = "#{ "a" * 40 } + #{ "b" * 40 } \n "
179+ result = run_cli ( "format" , contents : contents )
199180
200- file = Tempfile . new ( %w[ test- .rb ] )
201- contents = "#{ "a" * 40 } + #{ "b" * 40 } \n "
202- file . write ( contents )
203-
204- result = run_cli ( "format" , file : file )
205- assert_equal ( "Hello, world!\n #{ contents } " , result . stdio )
181+ assert_equal ( "Hello, world!\n #{ contents } " , result . stdio )
182+ end
206183 end
207- ensure
208- FileUtils . rm ( config_file )
209184 end
210185
211186 def test_print_width_args_with_config_file
212- config_file = File . join ( Dir . pwd , SyntaxTree :: CLI :: CONFIG_FILE )
213- File . write ( config_file , "--print-width=100 ")
187+ with_config_file ( "--print-width=100" ) do
188+ result = run_cli ( "check" , contents : " #{ "a" * 40 } + #{ "b" * 40 } \n ")
214189
215- contents = "#{ "a" * 40 } + #{ "b" * 40 } \n "
190+ assert_includes ( result . stdio , "match" )
191+ end
192+ end
216193
217- file = Tempfile . new ( %w[ test- .rb ] )
218- file . write ( contents )
219- result = run_cli ( "check" , file : file )
220- assert_includes ( result . stdio , "match" )
194+ def test_print_width_args_with_config_file_override
195+ with_config_file ( "--print-width=100" ) do
196+ contents = " #{ "a" * 40 } + #{ "b" * 40 } \n "
197+ result = run_cli ( "check" , "--print-width=82" , contents : contents )
221198
222- file = Tempfile . new ( %w[ test- .rb ] )
223- file . write ( contents )
224- result = run_cli ( "check" , "--print-width=82" , file : file )
225- assert_includes ( result . stderr , "expected" )
226- ensure
227- FileUtils . rm ( config_file )
199+ assert_includes ( result . stderr , "expected" )
200+ end
228201 end
229202
230203 def test_plugin_args_with_config_file
231- config_file = File . join ( Dir . pwd , SyntaxTree ::CLI ::CONFIG_FILE )
232- File . write ( config_file , "--plugins=hello_plugin" )
204+ with_config_file ( "--plugins=hello" ) do
205+ with_plugin_directory do |directory |
206+ directory . plugin ( "hello" , "puts 'Hello, world!'" )
207+ directory . plugin ( "goodbye" , "puts 'Bye, world!'" )
233208
234- Dir . mktmpdir do |directory |
235- Dir . mkdir ( File . join ( directory , "syntax_tree" ) )
236- $:. unshift ( directory )
209+ result = run_cli ( "format" , "--plugins=goodbye" )
237210
238- File . write (
239- File . join ( directory , "syntax_tree" , "hello_plugin.rb" ) ,
240- "puts 'Hello, world!'"
241- )
242- File . write (
243- File . join ( directory , "syntax_tree" , "bye_plugin.rb" ) ,
244- "puts 'Bye, world!'"
245- )
246-
247- result = run_cli ( "format" , "--plugins=bye_plugin" )
248- assert_equal ( "Hello, world!\n Bye, world!\n test\n " , result . stdio )
211+ assert_equal ( "Hello, world!\n Bye, world!\n test\n " , result . stdio )
212+ end
249213 end
250- ensure
251- FileUtils . rm ( config_file )
252214 end
253215
254216 private
255217
256218 Result = Struct . new ( :status , :stdio , :stderr , keyword_init : true )
257219
258- def run_cli ( command , *args , file : nil )
259- if file . nil?
260- file = Tempfile . new ( %w[ test- .rb ] )
261- file . puts ( "test" )
262- end
220+ def run_cli ( command , *args , contents : :default )
221+ file =
222+ case contents
223+ when :default
224+ Tempfile . new ( %w[ test- .rb ] ) . tap { |file | file . puts ( "test" ) }
225+ when String
226+ Tempfile . new ( %w[ test- .rb ] ) . tap { |file | file . write ( contents ) }
227+ else
228+ contents
229+ end
263230
264231 file . rewind
265232
@@ -272,5 +239,37 @@ def run_cli(command, *args, file: nil)
272239 file . close
273240 file . unlink
274241 end
242+
243+ def with_config_file ( contents )
244+ filepath = File . join ( Dir . pwd , SyntaxTree ::CLI ::CONFIG_FILE )
245+ File . write ( filepath , contents )
246+
247+ yield
248+ ensure
249+ FileUtils . rm ( filepath )
250+ end
251+
252+ class PluginDirectory
253+ attr_reader :directory
254+
255+ def initialize ( directory )
256+ @directory = directory
257+ end
258+
259+ def plugin ( name , contents )
260+ File . write ( File . join ( directory , "#{ name } .rb" ) , contents )
261+ end
262+ end
263+
264+ def with_plugin_directory
265+ Dir . mktmpdir do |directory |
266+ $:. unshift ( directory )
267+
268+ plugin_directory = File . join ( directory , "syntax_tree" )
269+ Dir . mkdir ( plugin_directory )
270+
271+ yield PluginDirectory . new ( plugin_directory )
272+ end
273+ end
275274 end
276275end
0 commit comments