Beispiel #1
0
class Compression(unittest.TestCase):
    compression = None
    expected_suffix = ""
    expected_mimetype = "text/plain"
    dummytext = """For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at http://www.zlib.net. There are known incompatibilities between the Python module and versions of the zlib library earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using 1.1.4 or later.

zlib’s functions have many options and often need to be used in a particular order. This documentation doesn’t attempt to cover all of the permutations; consult the zlib manual at http://www.zlib.net/manual.html for authoritative information.

For reading and writing .gz files see the gzip module.
"""

    def p(self, path):
        path = self.datadir + "/" + path
        return path.replace('/', '\\') if os.sep == '\\' else path

    def setUp(self):
        self.datadir = tempfile.mkdtemp()
        self.store = DocumentStore(self.datadir, compression=self.compression)

    def test_intermediate_path(self):
        self.assertEqual(
            self.p("intermediate/123/a.xml" + self.expected_suffix),
            self.store.intermediate_path('123/a'))

    def test_intermediate_path_selectsuffix(self):
        self.store.intermediate_suffixes = [".html", ".xhtml"]
        util.writefile(self.p("intermediate/123/a.html"), self.dummytext)
        self.assertEqual(
            self.p("intermediate/123/a.html") + self.expected_suffix,
            self.store.intermediate_path('123/a'))

    def test_open_intermediate_path(self):
        self.store.intermediate_suffixes = [".html", ".xhtml"]
        with self.store.open_intermediate("123/a", mode="w",
                                          suffix=".xhtml") as fp:
            fp.write(self.dummytext)
        filename = self.p("intermediate/123/a.xhtml" + self.expected_suffix)
        self.assertTrue(os.path.exists(filename))
        mimetype = util.runcmd("file -b --mime-type %s" % filename)[1]
        self.assertIn(mimetype.strip(), self.expected_mimetype)
        with self.store.open_intermediate("123/a") as fp:
            # note, open_intermediate should open the file with the
            # the .xhtml suffix automatically
            self.assertEqual(self.dummytext, fp.read())
Beispiel #2
0
class Compression(unittest.TestCase):
    compression = None
    expected_suffix = ""
    expected_mimetype = "text/plain"
    dummytext = """For applications that require data compression, the functions in this module allow compression and decompression, using the zlib library. The zlib library has its own home page at http://www.zlib.net. There are known incompatibilities between the Python module and versions of the zlib library earlier than 1.1.3; 1.1.3 has a security vulnerability, so we recommend using 1.1.4 or later.

zlib’s functions have many options and often need to be used in a particular order. This documentation doesn’t attempt to cover all of the permutations; consult the zlib manual at http://www.zlib.net/manual.html for authoritative information.

For reading and writing .gz files see the gzip module.
"""
    
    def p(self,path):
        path = self.datadir+"/"+path
        return path.replace('/', '\\') if os.sep == '\\' else path

    def setUp(self):
        self.datadir = tempfile.mkdtemp()
        self.store = DocumentStore(self.datadir, compression=self.compression)

    def test_intermediate_path(self):
        self.assertEqual(self.p("intermediate/123/a.xml" + self.expected_suffix),
                         self.store.intermediate_path('123/a'))

    def test_intermediate_path_selectsuffix(self):
        self.store.intermediate_suffixes = [".html", ".xhtml"]
        util.writefile(self.p("intermediate/123/a.html"), self.dummytext)
        self.assertEqual(self.p("intermediate/123/a.html") + self.expected_suffix,
                         self.store.intermediate_path('123/a'))

    def test_open_intermediate_path(self):
        self.store.intermediate_suffixes = [".html", ".xhtml"]
        with self.store.open_intermediate("123/a", mode="w", suffix=".xhtml") as fp:
            fp.write(self.dummytext)
        filename = self.p("intermediate/123/a.xhtml" + self.expected_suffix)
        self.assertTrue(os.path.exists(filename))
        mimetype = util.runcmd("file -b --mime-type %s" % filename)[1]
        self.assertIn(mimetype.strip(), self.expected_mimetype)
        with self.store.open_intermediate("123/a") as fp:
            # note, open_intermediate should open the file with the
            # the .xhtml suffix automatically
            self.assertEqual(self.dummytext, fp.read())
Beispiel #3
0
    def test_mkpatch(self):
        tempdir = tempfile.mkdtemp()
        basefile = "1"
        # Test 1: A repo which do not use any intermediate files. In
        # this case, the user edits the downloaded file, then runs
        # mkpatch, which saves the edited file, re-downloads the file,
        # and computes the diff.
        store = DocumentStore(tempdir + "/base")
        downloaded_path = store.downloaded_path(basefile)

        def my_download_single(self):
            # this function simulates downloading
            with open(downloaded_path, "wb") as fp:
                fp.write(
                    """This is a file.
It has been downloaded.
""".encode()
                )

        repo = DocumentRepository(datadir=tempdir)
        with repo.store.open_downloaded(basefile, "wb") as fp:
            fp.write(
                """This is a file.
It has been patched.
""".encode()
            )

        d = Devel()
        globalconf = LayeredConfig(
            {
                "datadir": tempdir,
                "patchdir": tempdir,
                "devel": {"class": "ferenda.Devel"},
                "base": {"class": "ferenda.DocumentRepository"},
            },
            cascade=True,
        )

        d.config = globalconf.devel
        with patch("ferenda.DocumentRepository.download_single") as mock:
            mock.side_effect = my_download_single
            patchpath = d.mkpatch("base", basefile, "Example patch")

        patchcontent = util.readfile(patchpath)
        self.assertIn("Example patch", patchcontent)
        self.assertIn("@@ -1,2 +1,2 @@", patchcontent)
        self.assertIn("-It has been downloaded.", patchcontent)
        self.assertIn("+It has been patched.", patchcontent)

        # test 2: Same, but with a multi-line desc
        with repo.store.open_downloaded(basefile, "wb") as fp:
            fp.write(
                """This is a file.
It has been patched.
""".encode()
            )
        longdesc = """A longer comment
spanning
several lines"""
        with patch("ferenda.DocumentRepository.download_single") as mock:
            mock.side_effect = my_download_single
            patchpath = d.mkpatch("base", basefile, longdesc)
        patchcontent = util.readfile(patchpath)
        desccontent = util.readfile(patchpath.replace(".patch", ".desc"))
        self.assertEqual(longdesc, desccontent)
        self.assertFalse("A longer comment" in patchcontent)
        self.assertIn("@@ -1,2 +1,2 @@", patchcontent)
        self.assertIn("-It has been downloaded.", patchcontent)
        self.assertIn("+It has been patched.", patchcontent)

        # test 3: If intermediate file exists, patch that one
        intermediate_path = store.intermediate_path(basefile)
        util.ensure_dir(intermediate_path)
        with open(intermediate_path, "wb") as fp:
            fp.write(
                """This is a intermediate file.
It has been patched.
""".encode()
            )
        intermediate_path = store.intermediate_path(basefile)

        def my_parse(self, basefile=None):
            # this function simulates downloading
            with open(intermediate_path, "wb") as fp:
                fp.write(
                    """This is a intermediate file.
It has been processed.
""".encode()
                )

        with patch("ferenda.DocumentRepository.parse") as mock:
            mock.side_effect = my_parse
            patchpath = d.mkpatch("base", basefile, "Example patch")
        patchcontent = util.readfile(patchpath)
        self.assertIn("@@ -1,2 +1,2 @@ Example patch", patchcontent)
        self.assertIn(" This is a intermediate file", patchcontent)
        self.assertIn("-It has been processed.", patchcontent)
        self.assertIn("+It has been patched.", patchcontent)