Skip to content

Commit ad0410f

Browse files
committed
refactor: remove remaining call_count patterns in compiler spec
1 parent ca32acf commit ad0410f

1 file changed

Lines changed: 20 additions & 22 deletions

File tree

spec/shakapacker/compiler_spec.rb

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,17 @@
116116

117117
hook_status = OpenStruct.new(success?: true, exitstatus: 0)
118118
webpack_status = OpenStruct.new(success?: true)
119+
hook_command = "bin/verbose-hook"
119120

120-
call_count = 0
121121
allow(Open3).to receive(:capture3) do |*args|
122-
call_count += 1
123-
if call_count == 1
122+
if args[1] == hook_command
124123
["Standard output", "Warning message", hook_status]
125124
else
126125
["", "", webpack_status]
127126
end
128127
end
129128

130-
allow(Shakapacker.config).to receive(:precompile_hook).and_return("bin/verbose-hook")
129+
allow(Shakapacker.config).to receive(:precompile_hook).and_return(hook_command)
131130

132131
expect(Shakapacker.compiler.compile).to be true
133132
end
@@ -172,19 +171,19 @@
172171

173172
hook_status = OpenStruct.new(success?: true, exitstatus: 0)
174173
webpack_status = OpenStruct.new(success?: true)
174+
hook_command = "'bin/my script' --arg1 --arg2"
175+
hook_executable = "bin/my script"
175176

176-
call_count = 0
177177
allow(Open3).to receive(:capture3) do |*args|
178-
call_count += 1
179-
if call_count == 1
178+
if args[1] == hook_executable
180179
["", "", hook_status]
181180
else
182181
["", "", webpack_status]
183182
end
184183
end
185184

186185
# Hook command with quoted path containing spaces
187-
allow(Shakapacker.config).to receive(:precompile_hook).and_return("'bin/my script' --arg1 --arg2")
186+
allow(Shakapacker.config).to receive(:precompile_hook).and_return(hook_command)
188187
allow(File).to receive(:exist?).and_call_original
189188
allow(File).to receive(:exist?).with(anything).and_return(true)
190189

@@ -198,18 +197,17 @@
198197

199198
hook_status = OpenStruct.new(success?: true, exitstatus: 0)
200199
webpack_status = OpenStruct.new(success?: true)
200+
hook_command = "bin/nonexistent-hook"
201201

202-
call_count = 0
203202
allow(Open3).to receive(:capture3) do |*args|
204-
call_count += 1
205-
if call_count == 1
203+
if args[1] == hook_command
206204
["", "", hook_status]
207205
else
208206
["", "", webpack_status]
209207
end
210208
end
211209

212-
allow(Shakapacker.config).to receive(:precompile_hook).and_return("bin/nonexistent-hook")
210+
allow(Shakapacker.config).to receive(:precompile_hook).and_return(hook_command)
213211
allow(File).to receive(:exist?).and_call_original
214212
allow(File).to receive(:exist?).with(anything).and_return(false)
215213

@@ -245,13 +243,13 @@
245243

246244
hook_status = OpenStruct.new(success?: true, exitstatus: 0)
247245
webpack_status = OpenStruct.new(success?: true)
246+
hook_command = "bin/prepare && rm -rf /"
247+
hook_executable = "bin/prepare"
248248

249-
call_count = 0
250249
captured_args = []
251250
allow(Open3).to receive(:capture3) do |env, *args|
252-
call_count += 1
253-
captured_args << args if call_count == 1
254-
if call_count == 1
251+
captured_args << args if args[0] == hook_executable
252+
if args[0] == hook_executable
255253
["", "", hook_status]
256254
else
257255
["", "", webpack_status]
@@ -260,7 +258,7 @@
260258

261259
# This malicious command would execute "rm -rf /" if passed to a shell
262260
# With shell-free execution, it's treated as arguments to bin/prepare
263-
allow(Shakapacker.config).to receive(:precompile_hook).and_return("bin/prepare && rm -rf /")
261+
allow(Shakapacker.config).to receive(:precompile_hook).and_return(hook_command)
264262
allow(File).to receive(:exist?).and_call_original
265263
allow(File).to receive(:exist?).with(anything).and_return(true)
266264

@@ -278,20 +276,20 @@
278276

279277
hook_status = OpenStruct.new(success?: true, exitstatus: 0)
280278
webpack_status = OpenStruct.new(success?: true)
279+
hook_command = "FOO=bar BAZ=qux bin/hook --arg"
280+
hook_executable = "bin/hook"
281281

282-
call_count = 0
283282
captured_env = nil
284283
allow(Open3).to receive(:capture3) do |env, *args|
285-
call_count += 1
286-
captured_env = env if call_count == 1
287-
if call_count == 1
284+
captured_env = env if args[0] == hook_executable
285+
if args[0] == hook_executable
288286
["", "", hook_status]
289287
else
290288
["", "", webpack_status]
291289
end
292290
end
293291

294-
allow(Shakapacker.config).to receive(:precompile_hook).and_return("FOO=bar BAZ=qux bin/hook --arg")
292+
allow(Shakapacker.config).to receive(:precompile_hook).and_return(hook_command)
295293
allow(File).to receive(:exist?).and_call_original
296294
allow(File).to receive(:exist?).with(anything).and_return(true)
297295

0 commit comments

Comments
 (0)