Skip to content

Commit

Permalink
refactor: nice_file can be used as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Mar 22, 2021
1 parent e613a75 commit 66173dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
21 changes: 8 additions & 13 deletions tests/coveragetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from coverage.cmdline import CoverageScript

from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs, assert_count_equal
from tests.helpers import run_command
from tests.helpers import nice_file, run_command
from tests.mixins import PytestBase, StdStreamCapturingMixin, SysPathModulesMixin, TempDirMixin


Expand Down Expand Up @@ -262,15 +262,10 @@ def capture_warning(msg, slug=None, once=False): # pylint: disable=unused
finally:
cov._warn = original_warn

def nice_file(self, *fparts):
"""Canonicalize the file name composed of the parts in `fparts`."""
fname = os.path.join(*fparts)
return os.path.normcase(os.path.abspath(os.path.realpath(fname)))

def assert_same_files(self, flist1, flist2):
"""Assert that `flist1` and `flist2` are the same set of file names."""
flist1_nice = [self.nice_file(f) for f in flist1]
flist2_nice = [self.nice_file(f) for f in flist2]
flist1_nice = [nice_file(f) for f in flist1]
flist2_nice = [nice_file(f) for f in flist2]
assert_count_equal(flist1_nice, flist2_nice)

def assert_exists(self, fname):
Expand Down Expand Up @@ -393,8 +388,8 @@ def run_command_status(self, cmd):
if env.JYTHON:
pythonpath_name = "JYTHONPATH" # pragma: only jython

testmods = self.nice_file(self.working_root(), 'tests/modules')
zipfile = self.nice_file(self.working_root(), 'tests/zipmods.zip')
testmods = nice_file(self.working_root(), "tests/modules")
zipfile = nice_file(self.working_root(), "tests/zipmods.zip")
pypath = os.getenv(pythonpath_name, '')
if pypath:
pypath += os.pathsep
Expand All @@ -407,7 +402,7 @@ def run_command_status(self, cmd):

def working_root(self):
"""Where is the root of the coverage.py working tree?"""
return os.path.dirname(self.nice_file(coverage.__file__, ".."))
return os.path.dirname(nice_file(coverage.__file__, ".."))

def report_from_command(self, cmd):
"""Return the report from the `cmd`, with some convenience added."""
Expand Down Expand Up @@ -451,8 +446,8 @@ def setup_test(self):
super(UsingModulesMixin, self).setup_test()

# Parent class saves and restores sys.path, we can just modify it.
sys.path.append(self.nice_file(TESTS_DIR, 'modules'))
sys.path.append(self.nice_file(TESTS_DIR, 'moremodules'))
sys.path.append(nice_file(TESTS_DIR, "modules"))
sys.path.append(nice_file(TESTS_DIR, "moremodules"))


def command_line(args):
Expand Down
6 changes: 6 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def make_file(filename, text="", bytes=b"", newline=None):
return filename


def nice_file(*fparts):
"""Canonicalize the file name composed of the parts in `fparts`."""
fname = os.path.join(*fparts)
return os.path.normcase(os.path.abspath(os.path.realpath(fname)))


class CheckUniqueFilenames(object):
"""Asserts the uniqueness of file names passed to a function."""
def __init__(self, wrapped):
Expand Down
10 changes: 5 additions & 5 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from coverage.misc import CoverageException

from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
from tests.helpers import assert_count_equal, change_dir
from tests.helpers import assert_count_equal, change_dir, nice_file


class ApiTest(CoverageTest):
Expand Down Expand Up @@ -879,7 +879,7 @@ def test_source_package_as_package(self):
assert lines['p1c'] == 0

def test_source_package_as_dir(self):
os.chdir(self.nice_file(TESTS_DIR, 'modules'))
os.chdir(nice_file(TESTS_DIR, "modules"))
assert os.path.isdir("pkg1")
lines = self.coverage_usepkgs(source=["pkg1"])
self.filenames_in(lines, "p1a p1b")
Expand All @@ -905,7 +905,7 @@ def test_source_package_part_omitted(self):
# the search for unexecuted files, and given a score of 0%.

# The omit arg is by path, so need to be in the modules directory.
os.chdir(self.nice_file(TESTS_DIR, 'modules'))
os.chdir(nice_file(TESTS_DIR, "modules"))
lines = self.coverage_usepkgs(source=["pkg1"], omit=["pkg1/p1b.py"])
self.filenames_in(lines, "p1a")
self.filenames_not_in(lines, "p1b")
Expand All @@ -920,15 +920,15 @@ def test_source_package_as_package_part_omitted(self):

def test_ambiguous_source_package_as_dir(self):
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
os.chdir(self.nice_file(TESTS_DIR, 'modules', "ambiguous"))
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
# pkg1 defaults to directory because tests/modules/ambiguous/pkg1 exists
lines = self.coverage_usepkgs(source=["pkg1"])
self.filenames_in(lines, "ambiguous")
self.filenames_not_in(lines, "p1a p1b p1c")

def test_ambiguous_source_package_as_package(self):
# pkg1 is a directory and a pkg, since we cd into tests/modules/ambiguous
os.chdir(self.nice_file(TESTS_DIR, 'modules', "ambiguous"))
os.chdir(nice_file(TESTS_DIR, "modules", "ambiguous"))
lines = self.coverage_usepkgs(source_pkgs=["pkg1"])
self.filenames_in(lines, "p1a p1b")
self.filenames_not_in(lines, "p2a p2b othera otherb osa osb ambiguous")
Expand Down

0 comments on commit 66173dc

Please sign in to comment.