Exemple #1
0
    def setUp(self):
        super().setUp()

        # Parent class saves and restores sys.path, we can just modify it.
        sys.path.append(nice_file(TESTS_DIR, "modules"))
        sys.path.append(nice_file(TESTS_DIR, "moremodules"))
        sys.path.append(nice_file(TESTS_DIR, "zipmods.zip"))
Exemple #2
0
 def test_ambiguous_source_package_as_dir(self):
     # pkg1 is a directory and a pkg, since we cd into tests/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")
Exemple #3
0
 def test_ambiguous_source_package_as_package(self):
     # pkg1 is a directory and a pkg, since we cd into tests/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")
     # Because source= was specified, we do search for unexecuted files.
     assert lines['p1c'] == 0
Exemple #4
0
 def test_source_package_as_dir(self):
     os.chdir(nice_file(TESTS_DIR, "modules"))
     assert os.path.isdir("pkg1")
     lines = self.coverage_usepkgs(source=["pkg1"])
     self.filenames_in(lines, "p1a p1b")
     self.filenames_not_in(lines, "p2a p2b othera otherb osa osb")
     # Because source= was specified, we do search for unexecuted files.
     assert lines['p1c'] == 0
Exemple #5
0
    def test_source_package_part_omitted(self):
        # https://github.com/nedbat/coveragepy/issues/218
        # Used to be if you omitted something executed and inside the source,
        # then after it was executed but not recorded, it would be found in
        # 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(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")
        assert lines['p1c'] == 0
Exemple #6
0
 def working_root(self):
     """Where is the root of the coverage.py working tree?"""
     return os.path.dirname(nice_file(coverage.__file__, ".."))
Exemple #7
0
    def run_command_status(self, cmd):
        """Run the command-line `cmd` in a sub-process, and print its output.

        Use this when you need to test the process behavior of coverage.

        Compare with `command_line`.

        Handles the following command names specially:

        * "python" is replaced with the command name of the current
            Python interpreter.

        * "coverage" is replaced with the command name for the main
            coverage.py program.

        Returns a pair: the process' exit status and its stdout/stderr text,
        which are also stored as `self.last_command_status` and
        `self.last_command_output`.

        """
        # Make sure "python" and "coverage" mean specifically what we want
        # them to mean.
        split_commandline = cmd.split()
        command_name = split_commandline[0]
        command_args = split_commandline[1:]

        if command_name == "python":
            # Running a Python interpreter in a sub-processes can be tricky.
            # Use the real name of our own executable. So "python foo.py" might
            # get executed as "python3.3 foo.py". This is important because
            # Python 3.x doesn't install as "python", so you might get a Python
            # 2 executable instead if you don't use the executable's basename.
            command_words = [os.path.basename(sys.executable)]

        elif command_name == "coverage":
            if env.JYTHON:                  # pragma: only jython
                # Jython can't do reporting, so let's skip the test now.
                if command_args and command_args[0] in ('report', 'html', 'xml', 'annotate'):
                    pytest.skip("Can't run reporting commands in Jython")
                # Jython can't run "coverage" as a command because the shebang
                # refers to another shebang'd Python script. So run them as
                # modules.
                command_words = "jython -m coverage".split()
            else:
                # The invocation requests the coverage.py program.  Substitute the
                # actual coverage.py main command name.
                command_words = [self.coverage_command]

        else:
            command_words = [command_name]

        cmd = " ".join([shlex.quote(w) for w in command_words] + command_args)

        # Add our test modules directory to PYTHONPATH.  I'm sure there's too
        # much path munging here, but...
        pythonpath_name = "PYTHONPATH"
        if env.JYTHON:
            pythonpath_name = "JYTHONPATH"          # pragma: only jython

        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
        pypath += testmods + os.pathsep + zipfile
        self.set_environ(pythonpath_name, pypath)

        self.last_command_status, self.last_command_output = run_command(cmd)
        print(self.last_command_output)
        return self.last_command_status, self.last_command_output
Exemple #8
0
 def assert_same_files(self, flist1, flist2):
     """Assert that `flist1` and `flist2` are the same set of file names."""
     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)
Exemple #9
0
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
Exemple #10
0
    def setup_test(self):
        super(UsingModulesMixin, self).setup_test()

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