def test_special_file_max_length(self):
     for max_content_length in [None, 0, 1]:
         for filename in self.specials:
             path = os.path.join(self.tmpdir_name, filename)
             content = Content.from_file(path=path)
             limited_content = Content.from_file(
                 path=path, max_content_length=max_content_length
             )
             assert limited_content == content
    def test_special_to_content(self):
        for filename in self.specials:
            path = os.path.join(self.tmpdir_name, filename)
            conv_content = Content.from_file(path=path)
            self.assertContentEqual(conv_content, self.empty_content)

        for path in ["/dev/null", "/dev/zero"]:
            path = os.path.join(self.tmpdir_name, filename)
            conv_content = Content.from_file(path=path)
            self.assertContentEqual(conv_content, self.empty_content)
 def test_symlink_max_length(self):
     for max_content_length in [4, 10]:
         for filename, symlink in self.symlinks.items():
             path = os.path.join(self.tmpdir_name, filename)
             content = Content.from_file(path=path)
             if content.data["length"] > max_content_length:
                 with pytest.raises(Exception, match="too large"):
                     Content.from_file(
                         path=path, max_content_length=max_content_length
                     )
             else:
                 limited_content = Content.from_file(
                     path=path, max_content_length=max_content_length
                 )
                 assert content == limited_content
 def test_file_max_length(self):
     for max_content_length in [2, 4]:
         for filename, content in self.contents.items():
             path = os.path.join(self.tmpdir_name, filename)
             content = Content.from_file(path=path)
             limited_content = Content.from_file(
                 path=path, max_content_length=max_content_length
             )
             assert content.data["length"] == limited_content.data["length"]
             assert content.data["status"] == "visible"
             if content.data["length"] > max_content_length:
                 assert limited_content.data["status"] == "absent"
                 assert limited_content.data["reason"] == "Content too large"
             else:
                 assert limited_content.data["status"] == "visible"
 def test_data_to_content(self):
     for filename, content in self.contents.items():
         conv_content = Content.from_bytes(
             mode=content["mode"], data=content["data"]
         )
         self.assertContentEqual(conv_content, content)
         self.assertIn(hash_to_hex(conv_content.hash), repr(conv_content))
 def test_file_to_content_with_path(self):
     for filename, content in self.contents.items():
         content_w_path = content.copy()
         path = os.path.join(self.tmpdir_name, filename)
         content_w_path["path"] = path
         conv_content = Content.from_file(path=path)
         self.assertContentEqual(conv_content, content_w_path, check_path=True)
def random_content() -> Content:
    """Create minimal content object."""
    data = str(datetime.now()).encode()
    return Content({
        "sha1_git": sha1(data).digest(),
        "perms": DentryPerms.content
    })
    def test_symlink_to_content_model(self):
        for filename, symlink in self.symlinks.items():
            path = os.path.join(self.tmpdir_name, filename)
            model_content = Content.from_file(path=path).to_model()

            right = symlink.copy()
            for key in ("perms", "path", "mode"):
                right.pop(key, None)
            right["status"] = "visible"
            assert model_content == model.Content.from_dict(right)
    def test_special_to_content_model(self):
        for filename in self.specials:
            path = os.path.join(self.tmpdir_name, filename)
            model_content = Content.from_file(path=path).to_model()

            right = self.empty_content.copy()
            for key in ("perms", "path", "mode"):
                right.pop(key, None)
            right["status"] = "visible"
            assert model_content == model.Content.from_dict(right)

        for path in ["/dev/null", "/dev/zero"]:
            model_content = Content.from_file(path=path).to_model()

            right = self.empty_content.copy()
            for key in ("perms", "path", "mode"):
                right.pop(key, None)
            right["status"] = "visible"
            assert model_content == model.Content.from_dict(right)
    def test_file_to_content_model(self):
        for filename, content in self.contents.items():
            path = os.path.join(self.tmpdir_name, filename)
            model_content = Content.from_file(path=path).to_model()

            right = content.copy()
            for key in ("perms", "mode"):
                right.pop(key, None)
            assert model_content.with_data() == model.Content.from_dict(right)

            right["path"] = path
            del right["data"]
            assert model_content == DiskBackedContent.from_dict(right)
    def test_directory_contains(self):
        d = Directory()
        d[b"a"] = Directory()
        d[b"a/b"] = Directory()
        d[b"a/b/c"] = Directory()
        d[b"a/b/c/d"] = Content()

        self.assertIn(b"a", d)
        self.assertIn(b"a/b", d)
        self.assertIn(b"a/b/c", d)
        self.assertIn(b"a/b/c/d", d)

        self.assertNotIn(b"b", d)
        self.assertNotIn(b"b/c", d)
        self.assertNotIn(b"b/c/d", d)
示例#12
0
def swhid_of_file_content(data) -> CoreSWHID:
    from swh.model.from_disk import Content

    object = Content.from_bytes(mode=644, data=data)
    return object.swhid()
示例#13
0
def swhid_of_file(path) -> CoreSWHID:
    from swh.model.from_disk import Content

    object = Content.from_file(path=path)
    return object.swhid()
 def test_content_swhid(self):
     for _, content in self.contents.items():
         content_res = Content.from_bytes(mode=content["mode"], data=content["data"])
         content_swhid = "swh:1:cnt:" + hash_to_hex(content["sha1_git"])
         assert str(content_res.swhid()) == content_swhid
 def test_symlink_to_content(self):
     for filename, symlink in self.symlinks.items():
         path = os.path.join(self.tmpdir_name, filename)
         perms = 0o120000
         conv_content = Content.from_symlink(path=path, mode=perms)
         self.assertContentEqual(conv_content, symlink)
 def test_symlink_to_content(self):
     for filename, symlink in self.symlinks.items():
         path = os.path.join(self.tmpdir_name, filename)
         conv_content = Content.from_file(path=path)
         self.assertContentEqual(conv_content, symlink)
 def test_file_to_content(self):
     for filename, content in self.contents.items():
         path = os.path.join(self.tmpdir_name, filename)
         conv_content = Content.from_file(path=path)
         self.assertContentEqual(conv_content, content)