|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | +from os.path import join as jp, dirname |
| 5 | +from pprint import pprint |
| 6 | +from subprocess import check_call |
| 7 | + |
| 8 | +from coveralls import Coveralls |
| 9 | + |
| 10 | +COVERAGE_CONFIG = """ |
| 11 | +[run] |
| 12 | +branch = True |
| 13 | +data_file = %s |
| 14 | +
|
| 15 | +[paths] |
| 16 | +source = %s |
| 17 | + %s |
| 18 | +""" |
| 19 | + |
| 20 | +COVERAGE_CODE_STANZA = """ |
| 21 | +import sys |
| 22 | +
|
| 23 | +sys.path.append(%r) |
| 24 | +
|
| 25 | +exec(''' |
| 26 | +import foo |
| 27 | +
|
| 28 | +foo.test_func(%r) |
| 29 | +''') |
| 30 | +""" |
| 31 | + |
| 32 | +COVERAGE_TEMPLATE_PATH = jp(dirname(__file__), "coverage_templates") |
| 33 | + |
| 34 | + |
| 35 | +class IntegrationTest(unittest.TestCase): |
| 36 | + @classmethod |
| 37 | + def setUpClass(cls): |
| 38 | + try: |
| 39 | + cls.old_cwd = os.getcwd() |
| 40 | + except FileNotFoundError: |
| 41 | + cls.old_cwd = None |
| 42 | + |
| 43 | + @classmethod |
| 44 | + def tearDownClass(cls): |
| 45 | + if cls.old_cwd: |
| 46 | + os.chdir(cls.old_cwd) |
| 47 | + |
| 48 | + def setUp(self): |
| 49 | + self.temp_dir = tempfile.TemporaryDirectory() |
| 50 | + os.chdir(self.temp_dir.name) |
| 51 | + self.covrc = jp(self.temp_dir.name, ".coveragerc") |
| 52 | + self.cov = jp(self.temp_dir.name, ".coverage") |
| 53 | + self.test_file = jp(self.temp_dir.name, "test.py") |
| 54 | + |
| 55 | + def tearDown(self): |
| 56 | + self.temp_dir.cleanup() |
| 57 | + |
| 58 | + def _test_harness(self, code): |
| 59 | + with open(self.covrc, "wt") as f: |
| 60 | + f.write(COVERAGE_CONFIG % (self.cov, COVERAGE_TEMPLATE_PATH, self.temp_dir)) |
| 61 | + with open(self.test_file, "wt") as f: |
| 62 | + f.write(code) |
| 63 | + |
| 64 | + check_call(["coverage", "run", "test.py"]) |
| 65 | + |
| 66 | + os.unlink(self.test_file) |
| 67 | + |
| 68 | + coverallz = Coveralls(repo_token="xxx", |
| 69 | + config_file=self.covrc) |
| 70 | + report = coverallz.create_data() |
| 71 | + coverallz.create_report() # This is purely for coverage |
| 72 | + |
| 73 | + source_files = set(f["name"] for f in report["source_files"]) |
| 74 | + self.assertNotIn(self.test_file, source_files) |
| 75 | + self.assertIn(jp(COVERAGE_TEMPLATE_PATH, "foo.py"), source_files) |
| 76 | + self.assertTrue(jp(COVERAGE_TEMPLATE_PATH, "bar.py") in source_files or |
| 77 | + jp(COVERAGE_TEMPLATE_PATH, "bar_310.py") in source_files) |
| 78 | + self.assertFalse(jp(COVERAGE_TEMPLATE_PATH, "bar.py") in source_files and |
| 79 | + jp(COVERAGE_TEMPLATE_PATH, "bar_310.py") in source_files) |
| 80 | + |
| 81 | + def _test_number(self, num): |
| 82 | + self._test_harness(COVERAGE_CODE_STANZA % (COVERAGE_TEMPLATE_PATH, num)) |
| 83 | + |
| 84 | + def test_5(self): |
| 85 | + self._test_number(5) |
| 86 | + |
| 87 | + def test_7(self): |
| 88 | + self._test_number(7) |
| 89 | + |
| 90 | + def test_11(self): |
| 91 | + self._test_number(11) |
0 commit comments