Пример #1
0
 def test_nonascii_directory(self):
     # https://bitbucket.org/ned/coveragepy/issues/573/cant-generate-xml-report-if-some-source
     self.make_file("테스트/program.py", "a = 1")
     with change_dir("테스트"):
         cov = coverage.Coverage()
         self.start_import_stop(cov, "program")
         cov.xml_report()
Пример #2
0
def run(cmds, rundir="src", outfile=None):
    """Run a list of commands.

    `cmds` is a string, commands separated by newlines.
    `rundir` is the directory in which to run the commands.
    `outfile` is a file name to redirect stdout to.

    """
    with change_dir(rundir):
        if outfile:
            fout = open(outfile, "a+")
        try:
            for cmd in cmds.split("\n"):
                cmd = cmd.strip()
                if not cmd:
                    continue
                retcode, output = run_command(cmd)
                print(output.rstrip())
                if outfile:
                    fout.write(output)
                if retcode:
                    raise Exception("command exited abnormally")    # pragma: only failure
        finally:
            if outfile:
                fout.close()
Пример #3
0
    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>',
        )
Пример #4
0
    def __call__(self):
        """Execute the test from the run.py file."""
        if _TEST_NAME_FILE:                             # pragma: debugging
            with open(_TEST_NAME_FILE, "w") as f:
                f.write(self.description.replace("/", "_"))

        # Prepare a dictionary of globals for the run.py files to use.
        fns = """
            copy run clean skip
            compare contains contains_any doesnt_contain
            """.split()
        if self.clean_only:
            glo = dict((fn, noop) for fn in fns)
            glo['clean'] = clean
        else:
            glo = dict((fn, globals()[fn]) for fn in fns)
            if self.dont_clean:                 # pragma: debugging
                glo['clean'] = noop

        with change_dir(self.dir):
            try:
                execfile(self.runpy, glo)
            except Exception:
                self.ok = False
                raise
Пример #5
0
 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()
Пример #6
0
def run(cmds, rundir="src", outfile=None):
    """Run a list of commands.

    `cmds` is a string, commands separated by newlines.
    `rundir` is the directory in which to run the commands.
    `outfile` is a file name to redirect stdout to.

    """
    with change_dir(rundir):
        if outfile:
            fout = open(outfile, "a+")
        try:
            for cmd in cmds.split("\n"):
                cmd = cmd.strip()
                if not cmd:
                    continue
                retcode, output = run_command(cmd)
                print(output.rstrip())
                if outfile:
                    fout.write(output)
                if retcode:
                    raise Exception("command exited abnormally")
        finally:
            if outfile:
                fout.close()
Пример #7
0
    def __call__(self):
        """Execute the test from the run.py file."""
        if _TEST_NAME_FILE:                                 # pragma: debugging
            with open(_TEST_NAME_FILE, "w") as f:
                f.write(self.description.replace("/", "_"))

        # Prepare a dictionary of globals for the run.py files to use.
        fns = """
            copy run runfunc clean skip
            compare contains contains_any doesnt_contain
            """.split()
        if self.clean_only:
            glo = dict((fn, noop) for fn in fns)
            glo['clean'] = clean
        else:
            glo = dict((fn, globals()[fn]) for fn in fns)
            if self.dont_clean:                 # pragma: not covered
                glo['clean'] = noop

        with change_dir(self.dir):
            try:
                execfile(self.runpy, glo)
            except Exception:
                self.ok = False
                raise
Пример #8
0
 def test_change_dir_twice(self):
     here = os.getcwd()
     self.assert_different_file(here, self.root)
     with change_dir(self.root):
         self.assert_same_file(os.getcwd(), self.root)
         os.chdir("a_dir")
         self.assert_same_file(os.getcwd(), self.a_dir)
     self.assert_same_file(os.getcwd(), here)
Пример #9
0
 def test_change_dir_with_exception(self):
     here = os.getcwd()
     self.assert_different_file(here, self.root)
     try:
         with change_dir(self.root):
             self.assert_same_file(os.getcwd(), self.root)
             raise ValueError("hi")
     except ValueError:
         pass
     self.assert_same_file(os.getcwd(), here)
Пример #10
0
    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
Пример #11
0
def runfunc(fn, rundir="src", addtopath=None):
    """Run a function.

    `fn` is a callable.
    `rundir` is the directory in which to run the function.

    """
    with change_dir(rundir):
        with saved_sys_path():
            if addtopath is not None:
                sys.path.insert(0, addtopath)
            fn()
Пример #12
0
    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()
Пример #13
0
    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
Пример #14
0
    def __call__(self):  # pylint: disable=arguments-differ
        """Execute the test from the runpy file."""
        # Prepare a dictionary of globals for the run.py files to use.
        fns = """
            copy run clean skip
            compare contains contains_any doesnt_contain
            """.split()
        if self.clean_only:
            glo = dict((fn, noop) for fn in fns)
            glo['clean'] = clean
        else:
            glo = dict((fn, globals()[fn]) for fn in fns)
            if self.dont_clean:  # pragma: debugging
                glo['clean'] = noop

        with change_dir(self.dir):
            try:
                execfile(self.runpy, glo)
            except Exception:
                self.ok = False
                raise
Пример #15
0
 def test_change_dir(self):
     here = os.getcwd()
     self.assert_different_file(here, self.root)
     with change_dir(self.root):
         self.assert_same_file(os.getcwd(), self.root)
     self.assert_same_file(os.getcwd(), here)