summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-24 09:53:53 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-24 09:53:53 -0400
commit20baf284341555803d800ec4c248fd0f1b5351f1 (patch)
tree315429176663e477a732c2fe9c0ad42f63fde0f3
parent4902b6c53740c8e871bb03e69e4345c5cafad96e (diff)
downloadpython-coveragepy-git-20baf284341555803d800ec4c248fd0f1b5351f1.tar.gz
refactor(test): os_sep and remove_tree helpers
-rw-r--r--tests/helpers.py14
-rw-r--r--tests/mixins.py7
-rw-r--r--tests/test_cmdline.py6
-rw-r--r--tests/test_filereporter.py19
-rw-r--r--tests/test_process.py4
-rw-r--r--tests/test_python.py4
6 files changed, 30 insertions, 24 deletions
diff --git a/tests/helpers.py b/tests/helpers.py
index 1420bed7..185f20dd 100644
--- a/tests/helpers.py
+++ b/tests/helpers.py
@@ -9,6 +9,7 @@ import glob
import os
import os.path
import re
+import shutil
import subprocess
import textwrap
@@ -95,6 +96,11 @@ def nice_file(*fparts):
return os.path.normcase(os.path.abspath(os.path.realpath(fname)))
+def os_sep(s):
+ """Replace slashes in `s` with the correct separator for the OS."""
+ return s.replace("/", os.sep)
+
+
class CheckUniqueFilenames:
"""Asserts the uniqueness of file names passed to a function."""
def __init__(self, wrapped):
@@ -156,6 +162,14 @@ def remove_files(*patterns):
for fname in glob.glob(pattern):
os.remove(fname)
+def remove_tree(dirname):
+ """Remove a directory tree.
+
+ It's fine for the directory to not exist in the first place.
+ """
+ if os.path.exists(dirname):
+ shutil.rmtree(dirname)
+
# Map chars to numbers for arcz_to_arcs
_arcz_map = {'.': -1}
diff --git a/tests/mixins.py b/tests/mixins.py
index 4a3347b2..1043c2d2 100644
--- a/tests/mixins.py
+++ b/tests/mixins.py
@@ -10,13 +10,12 @@ Some of these are transitional while working toward pure-pytest style.
import importlib
import os
import os.path
-import shutil
import sys
import pytest
from coverage.misc import SysModuleSaver
-from tests.helpers import change_dir, make_file, remove_files
+from tests.helpers import change_dir, make_file, remove_files, remove_tree
class PytestBase:
@@ -108,9 +107,7 @@ class RestoreModulesMixin:
# resolution is only one second, a changed file might not be
# picked up.
remove_files("*.pyc", "*$py.class")
- if os.path.exists("__pycache__"):
- shutil.rmtree("__pycache__")
-
+ remove_tree("__pycache__")
importlib.invalidate_caches()
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index caaa43c8..7b558bbb 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -3,7 +3,6 @@
"""Test cmdline.py for coverage.py."""
-import os
import pprint
import sys
import textwrap
@@ -20,6 +19,7 @@ from coverage.exceptions import ExceptionDuringRun
from coverage.version import __url__
from tests.coveragetest import CoverageTest, OK, ERR, command_line
+from tests.helpers import os_sep
class BaseCmdLineTest(CoverageTest):
@@ -818,7 +818,7 @@ class CmdLineStdoutTest(BaseCmdLineTest):
@pytest.mark.skipif(env.JYTHON, reason="Jython gets mad if you patch sys.argv")
def test_help_contains_command_name(self):
# Command name should be present in help output.
- fake_command_path = "lorem/ipsum/dolor".replace("/", os.sep)
+ fake_command_path = os_sep("lorem/ipsum/dolor")
expected_command_name = "dolor"
fake_argv = [fake_command_path, "sit", "amet"]
with mock.patch.object(sys, 'argv', new=fake_argv):
@@ -834,7 +834,7 @@ class CmdLineStdoutTest(BaseCmdLineTest):
# has the `__main__.py` file's patch as the command name. Instead, the command name should
# be derived from the package name.
- fake_command_path = "lorem/ipsum/dolor/__main__.py".replace("/", os.sep)
+ fake_command_path = os_sep("lorem/ipsum/dolor/__main__.py")
expected_command_name = "dolor"
fake_argv = [fake_command_path, "sit", "amet"]
with mock.patch.object(sys, 'argv', new=fake_argv):
diff --git a/tests/test_filereporter.py b/tests/test_filereporter.py
index e2c71fa2..227cc458 100644
--- a/tests/test_filereporter.py
+++ b/tests/test_filereporter.py
@@ -3,23 +3,18 @@
"""Tests for FileReporters"""
-import os
import sys
from coverage.plugin import FileReporter
from coverage.python import PythonFileReporter
from tests.coveragetest import CoverageTest, UsingModulesMixin
+from tests.helpers import os_sep
# pylint: disable=import-error
# Unable to import 'aa' (No module named aa)
-def native(filename):
- """Make `filename` into a native form."""
- return filename.replace("/", os.sep)
-
-
class FileReporterTest(UsingModulesMixin, CoverageTest):
"""Tests for FileReporter classes."""
@@ -55,9 +50,9 @@ class FileReporterTest(UsingModulesMixin, CoverageTest):
acu = PythonFileReporter(aa)
bcu = PythonFileReporter(aa.bb)
ccu = PythonFileReporter(aa.bb.cc)
- assert acu.relative_filename() == native("aa/__init__.py")
- assert bcu.relative_filename() == native("aa/bb/__init__.py")
- assert ccu.relative_filename() == native("aa/bb/cc/__init__.py")
+ assert acu.relative_filename() == os_sep("aa/__init__.py")
+ assert bcu.relative_filename() == os_sep("aa/bb/__init__.py")
+ assert ccu.relative_filename() == os_sep("aa/bb/cc/__init__.py")
assert acu.source() == "# aa\n"
assert bcu.source() == "# bb\n"
assert ccu.source() == "" # yes, empty
@@ -70,9 +65,9 @@ class FileReporterTest(UsingModulesMixin, CoverageTest):
acu = PythonFileReporter(aa.afile)
bcu = PythonFileReporter(aa.bb.bfile)
ccu = PythonFileReporter(aa.bb.cc.cfile)
- assert acu.relative_filename() == native("aa/afile.py")
- assert bcu.relative_filename() == native("aa/bb/bfile.py")
- assert ccu.relative_filename() == native("aa/bb/cc/cfile.py")
+ assert acu.relative_filename() == os_sep("aa/afile.py")
+ assert bcu.relative_filename() == os_sep("aa/bb/bfile.py")
+ assert ccu.relative_filename() == os_sep("aa/bb/cc/cfile.py")
assert acu.source() == "# afile.py\n"
assert bcu.source() == "# bfile.py\n"
assert ccu.source() == "# cfile.py\n"
diff --git a/tests/test_process.py b/tests/test_process.py
index 29ce6071..bf16a2b2 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -23,7 +23,7 @@ from coverage.data import line_counts
from coverage.files import abs_file, python_reported_file
from tests.coveragetest import CoverageTest, TESTS_DIR
-from tests.helpers import change_dir, make_file, nice_file, re_lines, run_command
+from tests.helpers import change_dir, make_file, nice_file, os_sep, re_lines, run_command
class ProcessTest(CoverageTest):
@@ -1378,7 +1378,7 @@ class UnicodeFilePathsTest(CoverageTest):
report_expected = (
"Name Stmts Miss Cover\n" +
"-----------------------------------\n" +
- f"\xe2{os.sep}accented.py 1 0 100%\n" +
+ os_sep("\xe2/accented.py 1 0 100%\n") +
"-----------------------------------\n" +
"TOTAL 1 0 100%\n"
)
diff --git a/tests/test_python.py b/tests/test_python.py
index 5965ca06..a8288613 100644
--- a/tests/test_python.py
+++ b/tests/test_python.py
@@ -3,7 +3,6 @@
"""Tests of coverage/python.py"""
-import os
import sys
import pytest
@@ -12,6 +11,7 @@ from coverage import env
from coverage.python import get_zip_bytes, source_for_file
from tests.coveragetest import CoverageTest
+from tests.helpers import os_sep
class GetZipBytesTest(CoverageTest):
@@ -28,7 +28,7 @@ class GetZipBytesTest(CoverageTest):
zip_file = "tests/zipmods.zip"
sys.path.append(zip_file) # So we can import the files.
filename = zip_file + "/encoded_" + encoding + ".py"
- filename = filename.replace("/", os.sep)
+ filename = os_sep(filename)
zip_data = get_zip_bytes(filename)
zip_text = zip_data.decode(encoding)
assert 'All OK' in zip_text