def test_nonascii_directory(self): # https://github.com/nedbat/coveragepy/issues/573 self.make_file("테스트/program.py", "a = 1") with change_dir("테스트"): cov = coverage.Coverage() self.start_import_stop(cov, "program") cov.xml_report()
def in_venv_world_fixture(self, venv_world): """For running tests inside venv_world, and cleaning up made files.""" with change_dir(venv_world): self.make_file( "myproduct.py", """\ import colorsys import third import nspkg.fifth import nspkg.sixth print(third.third(11)) print(nspkg.fifth.fifth(22)) print(nspkg.sixth.sixth(33)) print(sum(colorsys.rgb_to_hls(1, 0, 0))) """) self.del_environ( "COVERAGE_TESTING") # To avoid needing contracts installed. self.set_environ("COVERAGE_DEBUG_FILE", "debug_out.txt") self.set_environ("COVERAGE_DEBUG", "trace") yield for fname in os.listdir("."): if fname not in {"venv", "another_pkg", "bug888"}: os.remove(fname)
def test_combine_relative(self): self.make_file("dir1/foo.py", "a = 1") self.make_file( "dir1/.coveragerc", """\ [run] relative_files = true """) with change_dir("dir1"): cov = coverage.Coverage(source=["."], data_suffix=True) self.start_import_stop(cov, "foo") cov.save() shutil.move(glob.glob(".coverage.*")[0], "..") self.make_file("dir2/bar.py", "a = 1") self.make_file( "dir2/.coveragerc", """\ [run] relative_files = true """) with change_dir("dir2"): cov = coverage.Coverage(source=["."], data_suffix=True) self.start_import_stop(cov, "bar") cov.save() shutil.move(glob.glob(".coverage.*")[0], "..") self.make_file( ".coveragerc", """\ [run] relative_files = true """) cov = coverage.Coverage() cov.combine() cov.save() self.make_file("foo.py", "a = 1") self.make_file("bar.py", "a = 1") cov = coverage.Coverage() cov.load() files = cov.get_data().measured_files() assert files == {'foo.py', 'bar.py'} res = cov.report() assert res == 100
def _temp_dir(self, tmpdir_factory): """Create a temp dir for the tests, if they want it.""" if self.run_in_temp_dir: tmpdir = tmpdir_factory.mktemp("t") self.temp_dir = str(tmpdir) with change_dir(self.temp_dir): # Modules should be importable from this temp directory. We don't # use '' because we make lots of different temp directories and # nose's caching importer can get confused. The full path prevents # problems. sys.path.insert(0, os.getcwd()) yield else: yield
def test_moving_stuff(self): # When using absolute file names, moving the source around results in # "No source for code" errors while reporting. self.make_file("foo.py", "a = 1") cov = coverage.Coverage(source=["."]) self.start_import_stop(cov, "foo") res = cov.report() assert res == 100 expected = re.escape("No source for code: '{}'.".format(abs_file("foo.py"))) os.remove("foo.py") self.make_file("new/foo.py", "a = 1") shutil.move(".coverage", "new/.coverage") with change_dir("new"): cov = coverage.Coverage() cov.load() with pytest.raises(CoverageException, match=expected): cov.report()
def test_other(self): self.make_file( "src/here.py", """\ import other if 1 < 2: h = 3 else: h = 4 """) self.make_file( "othersrc/other.py", """\ # A file in another directory. We're checking that it ends up in the # HTML report. print("This is the other src!") """) with change_dir("src"): sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) self.start_import_stop(cov, "here") cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it actual_file = list(glob.glob("out/other/*_other_py.html")) assert len(actual_file) == 1 os.rename(actual_file[0], "out/other/blah_blah_other_py.html") compare_html( gold_path("html/other"), "out/other", extra_scrubs=[ (r'href="d_[0-9a-z]{16}_', 'href="_TEST_TMPDIR_othersrc_'), ], ) contains( 'out/other/index.html', '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def test_moving_stuff_with_relative(self): # When using relative file names, moving the source around is fine. self.make_file("foo.py", "a = 1") self.make_file(".coveragerc", """\ [run] relative_files = true """) cov = coverage.Coverage(source=["."]) self.start_import_stop(cov, "foo") res = cov.report() assert res == 100 os.remove("foo.py") self.make_file("new/foo.py", "a = 1") shutil.move(".coverage", "new/.coverage") shutil.move(".coveragerc", "new/.coveragerc") with change_dir("new"): cov = coverage.Coverage() cov.load() res = cov.report() assert res == 100
def test_other(self): self.make_file( "src/here.py", """\ import other if 1 < 2: h = 3 else: h = 4 """) self.make_file( "othersrc/other.py", """\ # A file in another directory. We're checking that it ends up in the # HTML report. print("This is the other src!") """) with change_dir("src"): sys.path.insert(0, "") # pytest sometimes has this, sometimes not!? sys.path.insert(0, "../othersrc") cov = coverage.Coverage(include=["./*", "../othersrc/*"]) self.start_import_stop(cov, "here") cov.html_report(directory="../out/other") # Different platforms will name the "other" file differently. Rename it for p in glob.glob("out/other/*_other_py.html"): os.rename(p, "out/other/blah_blah_other_py.html") compare_html(gold_path("html/other"), "out/other") contains( "out/other/index.html", '<a href="here_py.html">here.py</a>', 'other_py.html">', 'other.py</a>', )
def venv_world_fixture(tmp_path_factory): """Create a virtualenv with a few test packages for VirtualenvTest to use. Returns the directory containing the "venv" virtualenv. """ venv_world = tmp_path_factory.mktemp("venv_world") with change_dir(venv_world): # Create a virtualenv. run_command("python -m venv venv") # A third-party package that installs a few different packages. make_file( "third_pkg/third/__init__.py", """\ import fourth def third(x): return 3 * x """) # Use plugin2.py as third.plugin with open(os.path.join(os.path.dirname(__file__), "plugin2.py")) as f: make_file("third_pkg/third/plugin.py", f.read()) # A render function for plugin2 to use for dynamic file names. make_file( "third_pkg/third/render.py", """\ def render(filename, linenum): return "HTML: {}@{}".format(filename, linenum) """) # Another package that third can use. make_file( "third_pkg/fourth/__init__.py", """\ def fourth(x): return 4 * x """) # Some namespace packages. make_file( "third_pkg/nspkg/fifth/__init__.py", """\ def fifth(x): return 5 * x """) # The setup.py to install everything. make_file( "third_pkg/setup.py", """\ import setuptools setuptools.setup( name="third", packages=["third", "fourth", "nspkg.fifth"], ) """) # Some namespace packages. make_file( "another_pkg/nspkg/sixth/__init__.py", """\ def sixth(x): return 6 * x """) make_file( "another_pkg/setup.py", """\ import setuptools setuptools.setup( name="another", packages=["nspkg.sixth"], ) """) # Bug888 code. make_file( "bug888/app/setup.py", """\ from setuptools import setup setup( name='testcov', packages=['testcov'], namespace_packages=['testcov'], ) """) make_file( "bug888/app/testcov/__init__.py", """\ try: # pragma: no cover __import__('pkg_resources').declare_namespace(__name__) except ImportError: # pragma: no cover from pkgutil import extend_path __path__ = extend_path(__path__, __name__) """) make_file( "bug888/app/testcov/main.py", """\ import pkg_resources for entry_point in pkg_resources.iter_entry_points('plugins'): entry_point.load()() """) make_file( "bug888/plugin/setup.py", """\ from setuptools import setup setup( name='testcov-plugin', packages=['testcov'], namespace_packages=['testcov'], entry_points={'plugins': ['testp = testcov.plugin:testp']}, ) """) make_file( "bug888/plugin/testcov/__init__.py", """\ try: # pragma: no cover __import__('pkg_resources').declare_namespace(__name__) except ImportError: # pragma: no cover from pkgutil import extend_path __path__ = extend_path(__path__, __name__) """) make_file( "bug888/plugin/testcov/plugin.py", """\ def testp(): print("Plugin here") """) # Install everything. coverage_src = nice_file(TESTS_DIR, "..") run_in_venv("python -m pip install --no-index " + "./third_pkg " + "-e ./another_pkg " + "-e ./bug888/app -e ./bug888/plugin " + coverage_src) shutil.rmtree("third_pkg") return venv_world