Skip to content

Commit

Permalink
Rewrite dir_hash test to test pex_code_hash too
Browse files Browse the repository at this point in the history
  • Loading branch information
huonw committed Nov 10, 2023
1 parent afeddae commit b4649b3
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from hashlib import sha1
from textwrap import dedent

import pytest

from pex.common import safe_mkdir, safe_open, temporary_dir, touch
from pex.pex import PEX
from pex.pex_builder import PEXBuilder
Expand Down Expand Up @@ -57,51 +59,48 @@ def test_hash():
assert hash_output == empty_hash.hexdigest()


def test_dir_hash():
# type: () -> None
@pytest.mark.parametrize(
("hasher", "includes_hidden_expected"),
[(CacheHelper.dir_hash, True), (CacheHelper.pex_code_hash, False)],
)
def test_directory_hasher(hasher, includes_hidden_expected):
# type: (Callable[[str], str], bool) -> None
with temporary_dir() as tmp_dir:
safe_mkdir(os.path.join(tmp_dir, "a", "b"))
with safe_open(os.path.join(tmp_dir, "c", "d", "e.py"), "w") as fp:
fp.write("contents1")
with safe_open(os.path.join(tmp_dir, "f.py"), "w") as fp:
fp.write("contents2")
hash1 = CacheHelper.dir_hash(tmp_dir)
hash1 = hasher(tmp_dir)

os.rename(os.path.join(tmp_dir, "c"), os.path.join(tmp_dir, "c-renamed"))
assert hash1 != CacheHelper.dir_hash(tmp_dir)
assert hash1 != hasher(tmp_dir)

os.rename(os.path.join(tmp_dir, "c-renamed"), os.path.join(tmp_dir, "c"))
assert hash1 == CacheHelper.dir_hash(tmp_dir)
assert hash1 == hasher(tmp_dir)

touch(os.path.join(tmp_dir, "c", "d", "e.pyc"))
assert hash1 == CacheHelper.dir_hash(tmp_dir)
assert hash1 == hasher(tmp_dir)
touch(os.path.join(tmp_dir, "c", "d", "e.pyc.123456789"))
assert hash1 == CacheHelper.dir_hash(tmp_dir)
assert hash1 == hasher(tmp_dir)

pycache_dir = os.path.join(tmp_dir, "__pycache__")
safe_mkdir(pycache_dir)
touch(os.path.join(pycache_dir, "f.pyc"))
assert hash1 == CacheHelper.dir_hash(tmp_dir)
assert hash1 == hasher(tmp_dir)
touch(os.path.join(pycache_dir, "f.pyc.123456789"))
assert hash1 == CacheHelper.dir_hash(tmp_dir)
assert hash1 == hasher(tmp_dir)

touch(os.path.join(pycache_dir, "f.py"))
assert hash1 == CacheHelper.dir_hash(
assert hash1 == hasher(
tmp_dir
), "All content under __pycache__ directories should be ignored."

with safe_open(os.path.join(tmp_dir, ".hidden"), "w") as fp:
fp.write("contents3")

def test_dir_hash_recur_issue_2285():
# type: () -> None
with temporary_dir() as tmp_dir:
# explicitly confirm that we recur into directories
hash_empty = CacheHelper.dir_hash(tmp_dir)
with safe_open(os.path.join(tmp_dir, "a", "b.py"), "w") as fp:
fp.write("contents1")

hash_full = CacheHelper.dir_hash(tmp_dir)

assert hash_full != hash_empty
includes_hidden = hash1 != hasher(tmp_dir)
assert includes_hidden == includes_hidden_expected


try:
Expand Down

0 comments on commit b4649b3

Please sign in to comment.