Skip to content

Commit 18078bf

Browse files
alvar-boltalvarhansen
authored andcommitted
[scan] Fix build_for_testing failure to fail
Scan's `ErrorHandler.handle_build_error` has explicit logic that checks for `/Executed/` pattern in logs. It seems to be meant for case where tests will be run with same command and so we would want to wait for test results parsing instead. But this is causing scan to not to fail under `build_for_testing` configuration when there is such pattern in logs (eg. some file name contains it). In order to fix this, I propose to explicitly check for exit status code in `build_for_testing` mode and produce an error if status is not ok. Fixes #20685
1 parent a9a7255 commit 18078bf

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

scan/lib/scan/runner.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,13 @@ def handle_results(tests_exit_status)
264264
zip_build_products
265265
copy_xctestrun
266266

267-
return nil if Scan.config[:build_for_testing]
267+
# Ensure we still fail the step on any non-zero exit status from xcodebuild.
268+
if Scan.config[:build_for_testing]
269+
unless tests_exit_status == 0
270+
UI.build_failure!("Build for testing failed. Exit status: #{tests_exit_status}")
271+
end
272+
return nil
273+
end
268274

269275
results = trainer_test_results
270276

scan/spec/runner_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,37 @@
173173
end.to raise_error(FastlaneCore::Interface::FastlaneTestFailure, "Test execution failed. Exit status: 1")
174174
end
175175
end
176+
177+
describe "when :build_for_testing is true" do
178+
it "fails the run if xcodebuild exits with a non-zero status even when error output contains 'Executed'", requires_xcodebuild: true do
179+
Scan.config = FastlaneCore::Configuration.create(Scan::Options.available_options, {
180+
output_directory: '/tmp/scan_results',
181+
project: './scan/examples/standard/app.xcodeproj',
182+
build_for_testing: true,
183+
fail_build: true
184+
})
185+
186+
test_command_generator = @scan.instance_variable_get(:@test_command_generator)
187+
allow(test_command_generator).to receive(:generate).and_return("xcodebuild build-for-testing")
188+
189+
error_output = <<~ERROR
190+
Some random xcodebuild failure output
191+
/tmp/MyAppTestsFileThatContainsWordExecuted.swift:12: error: Something broke
192+
ERROR
193+
194+
expect(Scan::ErrorHandler).to receive(:handle_build_error).with(error_output, anything).and_call_original
195+
196+
allow(FastlaneCore::CommandExecutor).to receive(:execute) do |command:, print_all:, print_command:, prefix:, loading:, suppress_output:, error:|
197+
system("ruby -e 'exit 65'")
198+
error.call(error_output)
199+
""
200+
end
201+
202+
expect do
203+
@scan.run
204+
end.to raise_error(FastlaneCore::Interface::FastlaneBuildFailure, "Build for testing failed. Exit status: 65")
205+
end
206+
end
176207
end
177208

178209
describe "retry_execute" do

0 commit comments

Comments
 (0)