Exemple #1
0
 def save_mismatch(f):
     """Save a mismatched result to tests/actual."""
     save_path = expected_dir.replace(os_sep("/gold/"), os_sep("/actual/"))
     os.makedirs(save_path, exist_ok=True)
     with open(os.path.join(save_path, f), "w") as savef:
         with open(os.path.join(actual_dir, f)) as readf:
             savef.write(readf.read())
    def test_module_files(self):
        import aa.afile
        import aa.bb.bfile
        import aa.bb.cc.cfile

        acu = PythonFileReporter(aa.afile)
        bcu = PythonFileReporter(aa.bb.bfile)
        ccu = PythonFileReporter(aa.bb.cc.cfile)
        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"
    def test_modules(self):
        import aa
        import aa.bb
        import aa.bb.cc

        acu = PythonFileReporter(aa)
        bcu = PythonFileReporter(aa.bb)
        ccu = PythonFileReporter(aa.bb.cc)
        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
Exemple #4
0
 def test_help_contains_command_name(self):
     # Command name should be present in help output.
     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):
         self.command_line("help")
     out = self.stdout()
     assert expected_command_name in out
Exemple #5
0
 def test_get_encoded_zip_files(self, encoding):
     # See igor.py, do_zipmods, for the text of these files.
     zip_file = "tests/zipmods.zip"
     sys.path.append(zip_file)       # So we can import the files.
     filename = zip_file + "/encoded_" + encoding + ".py"
     filename = os_sep(filename)
     zip_data = get_zip_bytes(filename)
     zip_text = zip_data.decode(encoding)
     assert 'All OK' in zip_text
     # Run the code to see that we really got it encoded properly.
     mod = __import__("encoded_"+encoding)
     assert mod.encoding == encoding
Exemple #6
0
    def test_help_contains_command_name_from_package(self):
        # Command package name should be present in help output.
        #
        # When the main module is actually a package's `__main__` module, the resulting command line
        # 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 = 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):
            self.command_line("help")
        out = self.stdout()
        assert expected_command_name in out
Exemple #7
0
def compare(
    expected_dir,
    actual_dir,
    file_pattern=None,
    actual_extra=False,
    scrubs=None,
):
    """Compare files matching `file_pattern` in `expected_dir` and `actual_dir`.

    `actual_extra` true means `actual_dir` can have extra files in it
    without triggering an assertion.

    `scrubs` is a list of pairs: regexes to find and replace to scrub the
    files of unimportant differences.

    If a comparison fails, a message will be written to stdout, the original
    unscrubbed output of the test will be written to an "/actual/" directory
    alongside the "/gold/" directory, and an assertion will be raised.

    """
    __tracebackhide__ = True  # pytest, please don't show me this function.
    assert os_sep("/gold/") in expected_dir

    dc = filecmp.dircmp(expected_dir, actual_dir)
    diff_files = fnmatch_list(dc.diff_files, file_pattern)
    expected_only = fnmatch_list(dc.left_only, file_pattern)
    actual_only = fnmatch_list(dc.right_only, file_pattern)

    def save_mismatch(f):
        """Save a mismatched result to tests/actual."""
        save_path = expected_dir.replace(os_sep("/gold/"), os_sep("/actual/"))
        os.makedirs(save_path, exist_ok=True)
        with open(os.path.join(save_path, f), "w") as savef:
            with open(os.path.join(actual_dir, f)) as readf:
                savef.write(readf.read())

    # filecmp only compares in binary mode, but we want text mode.  So
    # look through the list of different files, and compare them
    # ourselves.
    text_diff = []
    for f in diff_files:
        expected_file = os.path.join(expected_dir, f)
        with open(expected_file) as fobj:
            expected = fobj.read()
        if expected_file.endswith(".xml"):
            expected = canonicalize_xml(expected)

        actual_file = os.path.join(actual_dir, f)
        with open(actual_file) as fobj:
            actual = fobj.read()
        if actual_file.endswith(".xml"):
            actual = canonicalize_xml(actual)

        if scrubs:
            expected = scrub(expected, scrubs)
            actual = scrub(actual, scrubs)
        if expected != actual:
            text_diff.append(f'{expected_file} != {actual_file}')
            expected = expected.splitlines()
            actual = actual.splitlines()
            print(f":::: diff '{expected_file}' and '{actual_file}'")
            print("\n".join(difflib.Differ().compare(expected, actual)))
            print(f":::: end diff '{expected_file}' and '{actual_file}'")
            save_mismatch(f)

    if not actual_extra:
        for f in actual_only:
            save_mismatch(f)

    assert not text_diff, "Files differ: " + "\n".join(text_diff)

    assert not expected_only, f"Files in {expected_dir} only: {expected_only}"
    if not actual_extra:
        assert not actual_only, f"Files in {actual_dir} only: {actual_only}"