From b70036b3f37d5cb6744a913bfb94adf56539cd95 Mon Sep 17 00:00:00 2001 From: Tim Hatch Date: Tue, 10 Mar 2020 16:40:07 -0700 Subject: [PATCH] Fixes and test for test --- bowler/main.py | 12 +++++++----- bowler/tests/smoke-selftest.py | 17 +++++++++++++++++ bowler/tests/smoke.py | 12 ++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 bowler/tests/smoke-selftest.py diff --git a/bowler/main.py b/bowler/main.py index de5f5ed..ff2f692 100755 --- a/bowler/main.py +++ b/bowler/main.py @@ -6,8 +6,11 @@ # LICENSE file in the root directory of this source tree. import importlib +import importlib.util import logging +import os.path import sys +import unittest from pathlib import Path from typing import List @@ -143,18 +146,17 @@ def run(codemod: str, argv: List[str]) -> None: def test(codemod: str) -> None: """ Run the tests in the codemod file - """ - import unittest - import importlib.util - import os.path + """ + # TODO: Unify the import code between 'run' and 'test' module_name_from_codemod = os.path.basename(codemod).replace(".py", "") spec = importlib.util.spec_from_file_location(module_name_from_codemod, codemod) foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) suite = unittest.TestLoader().loadTestsFromModule(foo) - unittest.TextTestRunner().run(suite) + result = unittest.TextTestRunner().run(suite) + sys.exit(not result.wasSuccessful()) if __name__ == "__main__": diff --git a/bowler/tests/smoke-selftest.py b/bowler/tests/smoke-selftest.py new file mode 100644 index 0000000..294e190 --- /dev/null +++ b/bowler/tests/smoke-selftest.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +# +# Copyright (c) Facebook, Inc. and its affiliates. +# +# This source code is licensed under the MIT license found in the +# LICENSE file in the root directory of this source tree. + + +from bowler.tests.lib import BowlerTestCase + + +class Tests(BowlerTestCase): + def test_pass(self): + pass + + def test_fail(self): + assert False diff --git a/bowler/tests/smoke.py b/bowler/tests/smoke.py index f1965fa..9b07df6 100644 --- a/bowler/tests/smoke.py +++ b/bowler/tests/smoke.py @@ -7,6 +7,8 @@ import io import logging +import subprocess +import sys from pathlib import Path from unittest import TestCase from unittest.mock import Mock @@ -82,3 +84,13 @@ def test_check_ast(self): ) self.assertTrue(any(isinstance(e, BadTransform) for e in query.exceptions)) mock_processor.assert_not_called() + + def test_click_test(self): + proc = subprocess.run( + [sys.executable, "-m", "bowler", "test", "bowler/tests/smoke-selftest.py"], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + encoding="utf-8", + ) + self.assertIn("Ran 2 tests", proc.stderr) + self.assertEqual(1, proc.returncode)