mirror of
https://github.com/facebook/sapling.git
synced 2024-10-09 08:18:15 +03:00
aa984b0835
Summary: **expected_sha1** was declared but not used in **xattr_test.py** file, removing this unused variable and variables which was used to creat **expected_sha1** Reviewed By: xavierd Differential Revision: D25899401 fbshipit-source-id: 26f0bb06d2c96e7f6754a4b821ffe4cf59a2f35e
73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
# Copyright (c) Facebook, Inc. and its affiliates.
|
|
#
|
|
# This software may be used and distributed according to the terms of the
|
|
# GNU General Public License version 2.
|
|
|
|
import hashlib
|
|
import os
|
|
from typing import Dict
|
|
|
|
from .lib import testcase
|
|
|
|
|
|
def getallxattrs(abspath: str) -> Dict[str, bytes]:
|
|
result = {}
|
|
for xattr in os.listxattr(abspath):
|
|
result[xattr] = os.getxattr(abspath, xattr)
|
|
return result
|
|
|
|
|
|
def sha1(value: bytes) -> bytes:
|
|
return hashlib.sha1(value).hexdigest().encode("ascii")
|
|
|
|
|
|
@testcase.eden_repo_test
|
|
class XattrTest(testcase.EdenRepoTest):
|
|
def populate_repo(self) -> None:
|
|
self.repo.write_file("hello", "hola\n")
|
|
self.repo.write_file("subdir/file", "contents")
|
|
self.repo.commit("Initial commit.")
|
|
|
|
def test_get_sha1_xattr(self) -> None:
|
|
filename = os.path.join(self.mount, "hello")
|
|
xattr = os.getxattr(filename, "user.sha1")
|
|
with open(filename, "rb") as f:
|
|
contents = f.read()
|
|
expected_sha1 = sha1(contents)
|
|
self.assertEqual(expected_sha1, xattr)
|
|
|
|
# and test what happens as we replace the file contents.
|
|
with open(filename, "w") as f:
|
|
f.write("foo")
|
|
f.flush()
|
|
self.assertEqual(sha1(b"foo"), os.getxattr(filename, "user.sha1"))
|
|
|
|
f.write("bar")
|
|
f.flush()
|
|
self.assertEqual(sha1(b"foobar"), os.getxattr(filename, "user.sha1"))
|
|
|
|
f.write("baz")
|
|
|
|
self.assertEqual(sha1(b"foobarbaz"), os.getxattr(filename, "user.sha1"))
|
|
|
|
def test_listxattr(self) -> None:
|
|
# Assert that listxattr on a directory is empty and does not break
|
|
# future listxattr calls.
|
|
self.assertEqual([], os.listxattr(os.path.join(self.mount, "subdir")))
|
|
|
|
filename = os.path.join(self.mount, "hello")
|
|
xattrs = getallxattrs(filename)
|
|
self.assertEqual({}, xattrs)
|
|
|
|
def test_get_sha1_xattr_succeeds_after_querying_xattr_on_dir(self) -> None:
|
|
with self.assertRaises(OSError):
|
|
os.getxattr(self.mount, "does_not_exist")
|
|
|
|
filename = os.path.join(self.mount, "hello")
|
|
xattr = os.getxattr(filename, "user.sha1")
|
|
with open(filename, "rb") as f:
|
|
contents = f.read()
|
|
expected_sha1 = sha1(contents)
|
|
self.assertEqual(expected_sha1, xattr)
|