@@ -20,7 +20,7 @@ def test_handler
20
20
file = Tempfile . new ( %w[ test- .test ] )
21
21
file . puts ( "test" )
22
22
23
- result = run_cli ( "ast" , file : file )
23
+ result = run_cli ( "ast" , contents : file )
24
24
assert_equal ( "\" test\\ n\" + \" test\\ n\" \n " , result . stdio )
25
25
ensure
26
26
SyntaxTree ::HANDLERS . delete ( ".test" )
@@ -32,10 +32,7 @@ def test_ast
32
32
end
33
33
34
34
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 " )
39
36
assert_includes ( result . stderr , "syntax error" )
40
37
end
41
38
@@ -45,18 +42,13 @@ def test_check
45
42
end
46
43
47
44
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" )
52
46
assert_includes ( result . stderr , "expected" )
53
47
end
54
48
55
49
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 )
60
52
assert_includes ( result . stdio , "match" )
61
53
end
62
54
@@ -104,15 +96,12 @@ def test_write
104
96
file = Tempfile . new ( %w[ test- .test ] )
105
97
filepath = file . path
106
98
107
- result = run_cli ( "write" , file : file )
99
+ result = run_cli ( "write" , contents : file )
108
100
assert_includes ( result . stdio , filepath )
109
101
end
110
102
111
103
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 : "<>" )
116
105
assert_includes ( result . stderr , "syntax error" )
117
106
end
118
107
@@ -146,19 +135,15 @@ def test_no_arguments_no_tty
146
135
def test_generic_error
147
136
SyntaxTree . stub ( :format , -> ( *) { raise } ) do
148
137
result = run_cli ( "format" )
138
+
149
139
refute_equal ( 0 , result . status )
150
140
end
151
141
end
152
142
153
143
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!'" )
157
146
158
- File . write (
159
- File . join ( directory , "syntax_tree" , "plugin.rb" ) ,
160
- "puts 'Hello, world!'"
161
- )
162
147
result = run_cli ( "format" , "--plugins=plugin" )
163
148
164
149
assert_equal ( "Hello, world!\n test\n " , result . stdio )
@@ -181,85 +166,67 @@ def test_language_server
181
166
end
182
167
183
168
def test_config_file
184
- config_file = File . join ( Dir . pwd , SyntaxTree ::CLI ::CONFIG_FILE )
185
169
config = <<~TXT
186
170
--print-width=100
187
171
--plugins=plugin
188
172
TXT
189
- File . write ( config_file , config )
190
173
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!'" )
194
177
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 )
199
180
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
206
183
end
207
- ensure
208
- FileUtils . rm ( config_file )
209
184
end
210
185
211
186
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 ")
214
189
215
- contents = "#{ "a" * 40 } + #{ "b" * 40 } \n "
190
+ assert_includes ( result . stdio , "match" )
191
+ end
192
+ end
216
193
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 )
221
198
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
228
201
end
229
202
230
203
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!'" )
233
208
234
- Dir . mktmpdir do |directory |
235
- Dir . mkdir ( File . join ( directory , "syntax_tree" ) )
236
- $:. unshift ( directory )
209
+ result = run_cli ( "format" , "--plugins=goodbye" )
237
210
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
249
213
end
250
- ensure
251
- FileUtils . rm ( config_file )
252
214
end
253
215
254
216
private
255
217
256
218
Result = Struct . new ( :status , :stdio , :stderr , keyword_init : true )
257
219
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
263
230
264
231
file . rewind
265
232
@@ -272,5 +239,37 @@ def run_cli(command, *args, file: nil)
272
239
file . close
273
240
file . unlink
274
241
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
275
274
end
276
275
end
0 commit comments