Repository: carbon-language/carbon-lang
File: examples/bazel_test_runner.py (Python)

Code before:
            # `2`  -- Command line or environment problem.
            # `3`  -- Tests failed or timed out, we don't retry at this layer
            #         on execution timeout.
            # `4`  -- Test command but no tests found.
            # `8`  -- Explicitly interrupted build.
            #
            # Note that `36` is documented as "likely permanent", but we retry
            # it as most of our transient failures actually produce that error
            # code.
            perm_error = (1, 2, 3, 4, 8)
            if exit_code in perm_error:
                break

            for line in stderr:
                print(line, file=sys.stderr)

            if exit_code == 0:
                return stdout

            # Retry transient errors with a brief delay.
            print(f"Attempt {attempt + 1} failed with exit code {exit_code}")
            time.sleep(attempt)
        self.AssertExitCode(exit_code, 0, stderr)

    def test_compile_lib(self) -> None:
        # TODO: Can remove this in favor of always running`test_run` if we can
        # make linking a binary sufficiently efficient.
        self._run_bazel(["build", "//:example_lib"])

    @unittest.skipUnless(
        "CARBON_BAZEL_TEST_FULL" in os.environ,
        "Skipping expensive test step for minimal testing",
    )
    def test_run(self) -> None:
        stdout = self._run_bazel(["run", "//:example"])
        self.assertEqual(stdout, ["Hello World!"])


if __name__ == "__main__":
    unittest.main()


Review comment (refactor):
```suggestion
        # TODO: Can remove this in favor of always running `test_run` if we can
```

Code after:
            # `2`  -- Command line or environment problem.
            # `3`  -- Tests failed or timed out, we don't retry at this layer
            #         on execution timeout.
            # `4`  -- Test command but no tests found.
            # `8`  -- Explicitly interrupted build.
            #
            # Note that `36` is documented as "likely permanent", but we retry
            # it as most of our transient failures actually produce that error
            # code.
            perm_error = (1, 2, 3, 4, 8)
            if exit_code in perm_error:
                break

            for line in stderr:
                print(line, file=sys.stderr)

            if exit_code == 0:
                return stdout

            # Retry transient errors with a brief delay.
            print(f"Attempt {attempt + 1} failed with exit code {exit_code}")
            time.sleep(attempt)
        self.AssertExitCode(exit_code, 0, stderr)

    def test_compile_lib(self) -> None:
        # TODO: Can remove this in favor of always running `test_run` if we can
        # make linking a binary sufficiently efficient.
        self._run_bazel(["build", "//:example_lib"])

    @unittest.skipUnless(
        "CARBON_BAZEL_TEST_FULL" in os.environ,
        "Skipping expensive test step for minimal testing",
    )
    def test_run(self) -> None:
        stdout = self._run_bazel(["run", "//:example"])
        self.assertEqual(stdout, ["Hello World!"])


if __name__ == "__main__":
    unittest.main()