Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for swift-testing based tests #1313

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions test/fixtures/xctest_runner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ swift_test(
name = "EmptyUnitTests",
tags = FIXTURE_TAGS,
)

swift_test(
name = "FailingSwiftTestingTests",
srcs = ["FailingSwiftTestingTests.swift"],
tags = FIXTURE_TAGS,
)

swift_test(
name = "EmptySwiftTestingSuite",
srcs = ["EmptySwiftTestingSuite.swift"],
tags = FIXTURE_TAGS,
)

swift_test(
name = "PassingSwiftTestingTests",
srcs = ["PassingSwiftTestingTests.swift"],
tags = FIXTURE_TAGS,
)
6 changes: 6 additions & 0 deletions test/fixtures/xctest_runner/EmptySwiftTestingSuite.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Testing

@Suite("Empty test suite")
class EmptyTestSuite {

}
6 changes: 6 additions & 0 deletions test/fixtures/xctest_runner/FailingSwiftTestingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Testing

@Test("A failing test")
func aFalingTest(){
#expect(false, "This assertion will always fail")
}
6 changes: 6 additions & 0 deletions test/fixtures/xctest_runner/PassingSwiftTestingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Testing

@Test("A passing test")
func aPassingTest(){
#expect(true, "This assertion should pass")
}
2 changes: 1 addition & 1 deletion test/rules/swift_shell_runner.sh.template
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set -euxo pipefail
executable="%executable%"
expected_return_code="%expected_return_code%"
expected_logs=%expected_logs%
not_expected_logs=%expected_logs%
not_expected_logs=%not_expected_logs%

# execute the target under test while recording its outputs and return code
tmp_dir=$(mktemp -d)
Expand Down
36 changes: 36 additions & 0 deletions test/xctest_runner_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def xctest_runner_test_suite(name, tags = []):
"Test Suite 'PassingUnitTests.xctest' passed",
"Executed 3 tests, with 0 failures",
],
not_expected_logs = ["error: no tests were executed, is the test bundle empty"],
tags = all_tags,
target_under_test = "@build_bazel_rules_swift//test/fixtures/xctest_runner:PassingUnitTests",
target_compatible_with = ["@platforms//os:macos"],
Expand All @@ -35,6 +36,7 @@ def xctest_runner_test_suite(name, tags = []):
"Test Suite 'FailingUnitTests.xctest' failed",
"Executed 1 test, with 1 failure",
],
not_expected_logs = ["error: no tests were executed, is the test bundle empty"],
tags = all_tags,
target_under_test = "@build_bazel_rules_swift//test/fixtures/xctest_runner:FailingUnitTests",
target_compatible_with = ["@platforms//os:macos"],
Expand All @@ -52,6 +54,40 @@ def xctest_runner_test_suite(name, tags = []):
target_compatible_with = ["@platforms//os:macos"],
)

swift_shell_test(
name = "{}_swift_testing_no_tests".format(name),
expected_return_code = 1,
expected_logs = [
"Test run with 0 tests passed after",
"error: no tests were executed",
],
tags = all_tags,
target_under_test = "@build_bazel_rules_swift//test/fixtures/xctest_runner:EmptySwiftTestingSuite",
target_compatible_with = ["@platforms//os:macos"],
)

swift_shell_test(
name = "{}_swift_testing_pass".format(name),
expected_return_code = 0,
expected_logs = [
"Test run with 1 test passed after",
],
not_expected_logs = ["error: no tests were executed, is the test bundle empty"],
tags = all_tags,
target_under_test = "@build_bazel_rules_swift//test/fixtures/xctest_runner:PassingSwiftTestingTests",
)

swift_shell_test(
name = "{}_swift_testing_fail".format(name),
expected_return_code = 1,
expected_logs = [
"Test run with 1 test failed after",
],
not_expected_logs = ["error: no tests were executed, is the test bundle empty"],
tags = all_tags,
target_under_test = "@build_bazel_rules_swift//test/fixtures/xctest_runner:FailingSwiftTestingTests",
)

native.test_suite(
name = name,
tags = all_tags,
Expand Down
18 changes: 15 additions & 3 deletions tools/xctest_runner/xctest_runner.sh.template
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@ if [[ "$exit_status" -ne 0 ]]; then
fi

# Fail when bundle executes nothing
test_target_execution_count=$(grep -e "Executed [[:digit:]]\{1,\} tests*," "$testlog" | tail -n1)
if echo "$test_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures"; then
echo "error: no tests were executed, is the test bundle empty?" >&2
xctest_target_execution_count=$(grep -e "Executed [[:digit:]]\{1,\} tests*," "$testlog" | tail -n1)
swift_testing_target_execution_count=$(grep -e "Test run with [[:digit:]]\{1,\} tests*" "$testlog" | tail -n1 || true)
error_msg="error: no tests were executed, is the test bundle empty?"
echo "xctest_target_execution_count: $xctest_target_execution_count"
echo "swift_testing_target_execution_count: $swift_testing_target_execution_count"

if echo "$xctest_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures" && \
[ -z "$swift_testing_target_execution_count" ] ; then
echo "$error_msg" >&2
exit 1
fi

if echo "$xctest_target_execution_count" | grep -q -e "Executed 0 tests, with 0 failures" && \
echo "$swift_testing_target_execution_count" | grep -q -e "Test run with 0 tests" ; then
echo "$error_msg" >&2
exit 1
fi

Expand Down