From 73cbe1b6e3b89e6cdbd61f4a45e2f83d446c7baa Mon Sep 17 00:00:00 2001 From: Edmund Kohlwey Date: Wed, 25 Sep 2024 10:42:02 -0400 Subject: [PATCH] Add some tests for swift-testing based output Closes https://github.com/bazelbuild/rules_swift/pull/1313 --- test/fixtures/xctest_runner/BUILD | 18 +++++++++ .../EmptySwiftTestingSuite.swift | 6 +++ .../FailingSwiftTestingTests.swift | 6 +++ .../PassingSwiftTestingTests.swift | 6 +++ test/rules/swift_shell_runner.sh.template | 4 +- test/xctest_runner_tests.bzl | 39 ++++++++++++++++++- 6 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/xctest_runner/EmptySwiftTestingSuite.swift create mode 100644 test/fixtures/xctest_runner/FailingSwiftTestingTests.swift create mode 100644 test/fixtures/xctest_runner/PassingSwiftTestingTests.swift diff --git a/test/fixtures/xctest_runner/BUILD b/test/fixtures/xctest_runner/BUILD index 778c8946d..af5be0e9b 100644 --- a/test/fixtures/xctest_runner/BUILD +++ b/test/fixtures/xctest_runner/BUILD @@ -70,3 +70,21 @@ swift_test( tags = FIXTURE_TAGS, deps = [":MainLibrary"], ) + +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, +) diff --git a/test/fixtures/xctest_runner/EmptySwiftTestingSuite.swift b/test/fixtures/xctest_runner/EmptySwiftTestingSuite.swift new file mode 100644 index 000000000..95513f36f --- /dev/null +++ b/test/fixtures/xctest_runner/EmptySwiftTestingSuite.swift @@ -0,0 +1,6 @@ +import Testing + +@Suite("Empty test suite") +class EmptyTestSuite { + +} diff --git a/test/fixtures/xctest_runner/FailingSwiftTestingTests.swift b/test/fixtures/xctest_runner/FailingSwiftTestingTests.swift new file mode 100644 index 000000000..c4bf3f7a7 --- /dev/null +++ b/test/fixtures/xctest_runner/FailingSwiftTestingTests.swift @@ -0,0 +1,6 @@ +import Testing + +@Test("A failing test") +func aFalingTest(){ + #expect(false, "This assertion will always fail") +} diff --git a/test/fixtures/xctest_runner/PassingSwiftTestingTests.swift b/test/fixtures/xctest_runner/PassingSwiftTestingTests.swift new file mode 100644 index 000000000..5cca7e61a --- /dev/null +++ b/test/fixtures/xctest_runner/PassingSwiftTestingTests.swift @@ -0,0 +1,6 @@ +import Testing + +@Test("A passing test") +func aPassingTest(){ + #expect(true, "This assertion should pass") +} diff --git a/test/rules/swift_shell_runner.sh.template b/test/rules/swift_shell_runner.sh.template index 37d90dc6d..742af60ea 100755 --- a/test/rules/swift_shell_runner.sh.template +++ b/test/rules/swift_shell_runner.sh.template @@ -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) @@ -59,7 +59,7 @@ done # verify the absence of unexpected logs for unexpected in "${not_expected_logs[@]}" do - if ! grep --quiet --invert-match --regexp "$unexpected" "$testlog"; then + if grep --quiet --regexp "$unexpected" "$testlog"; then echo "Fail: found unexpected log: $unexpected" exit 1 fi diff --git a/test/xctest_runner_tests.bzl b/test/xctest_runner_tests.bzl index ad56c8ae1..7a557ab9e 100644 --- a/test/xctest_runner_tests.bzl +++ b/test/xctest_runner_tests.bzl @@ -2,6 +2,8 @@ load("//test/rules:swift_shell_test.bzl", "swift_shell_test") +_NO_TESTS_DISCOVERED = "ERROR: No tests were discovered" + def xctest_runner_test_suite(name, tags = []): """Test suite for xctest runner. @@ -19,6 +21,7 @@ def xctest_runner_test_suite(name, tags = []): "Test Suite 'PassingUnitTests.xctest' passed", "Executed 3 tests, with 0 failures", ], + not_expected_logs = [_NO_TESTS_DISCOVERED], tags = all_tags, target_under_test = "//test/fixtures/xctest_runner:PassingUnitTests", target_compatible_with = ["@platforms//os:macos"], @@ -32,6 +35,7 @@ def xctest_runner_test_suite(name, tags = []): "Test Suite 'FailingUnitTests.xctest' failed", "Executed 1 test, with 1 failure", ], + not_expected_logs = [_NO_TESTS_DISCOVERED], tags = all_tags, target_under_test = "//test/fixtures/xctest_runner:FailingUnitTests", target_compatible_with = ["@platforms//os:macos"], @@ -42,13 +46,46 @@ def xctest_runner_test_suite(name, tags = []): expected_return_code = 1, expected_logs = [ "Executed 0 tests, with 0 failures", - "ERROR: No tests were discovered", + _NO_TESTS_DISCOVERED, ], tags = all_tags, target_under_test = "//test/fixtures/xctest_runner:EmptyUnitTests", 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", + _NO_TESTS_DISCOVERED, + ], + tags = all_tags, + target_under_test = "//test/fixtures/xctest_runner:EmptySwiftTestingSuite", + ) + + 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 = [_NO_TESTS_DISCOVERED], + tags = all_tags, + target_under_test = "//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 = [_NO_TESTS_DISCOVERED], + tags = all_tags, + target_under_test = "//test/fixtures/xctest_runner:FailingSwiftTestingTests", + ) + native.test_suite( name = name, tags = all_tags,