Skip to content

Commit

Permalink
Handling extra long snapshot file names properly.
Browse files Browse the repository at this point in the history
Back to using the old character replacement algorithm again because
filenames were getting too long. Now putting a shake_128 hash on the
end of every snapshot file name to deal with any collisions. This
ends up only being 8 characters, which seems like a small price to
pay in order to ensure there are never any conflicts.
  • Loading branch information
bslatkin committed Feb 1, 2025
1 parent 2bdbf84 commit 69be0e8
Show file tree
Hide file tree
Showing 51 changed files with 28 additions and 26 deletions.
53 changes: 28 additions & 25 deletions pytest_insta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
]


import hashlib
import math
import os
import re
Expand All @@ -21,29 +22,13 @@
from _pytest import python


def replace_non_alphanumeric_with_unicode_names(text):
result = []
for char in text:
if char.isalnum() or char in ("_", "-", "."):
result.append(char)
else:
result.append("_")

try:
name = unicodedata.name(char)
result.append(name.lower().replace(" ", "_"))
except ValueError:
result.append(f"x{ord(char):x}")

result.append("_")
return "".join(result)
def normalize_node_name(name: str) -> str:
return re.sub(
r"\W+", "_", re.sub(r"^(tests?[_/])*|([_/]tests?)*(\.\w+)?$", "", name)
).strip("_")


def normalize_node_name(name: str) -> str:
test_name = re.sub(r"^(tests?[_/])*|([_/]tests?)*(\.\w+)?$", "", name)
unescaped = bytes(test_name, "utf-8").decode("unicode_escape")
normalized = replace_non_alphanumeric_with_unicode_names(unescaped)
return normalized
MAX_NODE_NAME_LENGTH = 224


def node_path_name(node: Any) -> Tuple[Path, str]:
Expand All @@ -53,11 +38,27 @@ def node_path_name(node: Any) -> Tuple[Path, str]:
node = node.parent
hierarchy.append(normalize_node_name(node.name))

path = Path(node.fspath) # type: ignore
basename = "__".join(reversed(hierarchy))
if len(basename) > MAX_NODE_NAME_LENGTH:
truncated = basename[:MAX_NODE_NAME_LENGTH]
else:
truncated = basename

# Always put a trailing hash on snapshot file paths to make sure that when
# two tests return the same value from `normalized_node_name` that they will
# still write to different files and not stomp on each other. This is
# especially important for parameterized tests. Note that there's still a
# tiny chance that these hashes also collide, so if users want zero chance
# of a collision they should give eac parameterized test a unique ID.
trailer = hashlib.shake_128(
basename.encode("utf-8"),
usedforsecurity=False,
).hexdigest(4)
adjusted_basename = f"{truncated}__{trailer}"

return (
path.relative_to(Path(".").resolve()),
"__".join(reversed(hierarchy)),
Path(node.fspath).relative_to(Path(".").resolve()),
adjusted_basename,
)


Expand All @@ -70,7 +71,9 @@ def hexdump(data: bytes, n: int = 16) -> Iterator[str]:


def hexload(dump: str) -> bytes:
return b"".join(bytes.fromhex(line.split(" ")[1]) for line in dump.splitlines())
return b"".join(
bytes.fromhex(line.split(" ")[1]) for line in dump.splitlines()
)


def is_ci() -> bool:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 69be0e8

Please sign in to comment.