Skip to content

Commit a2881b6

Browse files
author
Your Name
committed
dynamically create test git repo
1 parent afe8aa1 commit a2881b6

File tree

4 files changed

+102
-67
lines changed

4 files changed

+102
-67
lines changed

tests/filesystem/cases/GIT-SETUP.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

tests/filesystem/conftest.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from typing import Union, Any, Iterator
2+
3+
import pytest
4+
5+
import os
6+
import subprocess
7+
import shutil
8+
import pathlib
9+
from .settings import (
10+
TEST_SAMPLES_PATH,
11+
REPO_FIXTURE_PATH,
12+
REPO_SAFE_PREFIX,
13+
REPO_GOOD_REF,
14+
)
15+
16+
17+
@pytest.fixture(scope="module", autouse=True)
18+
def repo_fixture(
19+
repo_path: Union[str, os.PathLike] = REPO_FIXTURE_PATH,
20+
samples_path: Union[str, os.PathLike] = TEST_SAMPLES_PATH,
21+
safe_prefix: str = REPO_SAFE_PREFIX,
22+
good_ref: str = REPO_GOOD_REF,
23+
) -> Iterator[Any]:
24+
"""Create a temporary git repository to test git-based filesystems.
25+
26+
Args:
27+
repo_path (str): The path at which to create the temporary repo. Defaults to REPO_FIXTURE_PATH. It is safest
28+
to create the repo outside your software project's directory tree so it does not interfere with real repos.
29+
samples_path (str): The path to the sample files, which will be added to the repo. Defaults to TEST_SAMPLES_PATH.
30+
safe_prefix (str): Helps prevent mixups between the test repo and real repos. Defaults to REPO_SAFE_PREFIX.
31+
good_ref (str): The git ref for the unmodified sample files. Later commits intentionally break the sample
32+
files so that tests will fail if the system under test doesn't correctly handle refs.
33+
Defaults to REPO_SAFE_REF.
34+
35+
Yields:
36+
Tuple[str, str]: A tuple containing the repo_path and good_ref.
37+
38+
"""
39+
repo_path = pathlib.Path(repo_path)
40+
samples_path = pathlib.Path(samples_path)
41+
42+
try:
43+
try:
44+
os.mkdir(repo_path)
45+
except FileExistsError:
46+
raise FileExistsError(
47+
f"Directory `{repo_path.absolute()}` already exists."
48+
"It should have been removed by the previous test run."
49+
)
50+
51+
# NOTE: `git init -b` option requires git 2.28 or later.
52+
subprocess.call(f"git init -b {safe_prefix}master", shell=True, cwd=repo_path)
53+
subprocess.call(
54+
"git config user.email '[email protected]'", shell=True, cwd=repo_path
55+
)
56+
subprocess.call("git config user.name 'Your Name'", shell=True, cwd=repo_path)
57+
shutil.copytree(
58+
samples_path, repo_path / f"{safe_prefix}samples", dirs_exist_ok=False
59+
)
60+
subprocess.call("git add --all", shell=True, cwd=repo_path)
61+
subprocess.call(
62+
"git commit -m 'add standard sample files for tests'",
63+
shell=True,
64+
cwd=repo_path,
65+
)
66+
subprocess.call(
67+
f"git tag -a {good_ref} -m 'The sample test files with no modifications'",
68+
shell=True,
69+
cwd=repo_path,
70+
)
71+
subprocess.call(
72+
"git mv sample.txt sample_renamed.txt",
73+
shell=True,
74+
cwd=repo_path / f"{safe_prefix}samples",
75+
)
76+
subprocess.call(
77+
"git commit -m 'rename samples.txt to make primitive test fail if at HEAD'",
78+
shell=True,
79+
cwd=repo_path,
80+
)
81+
yield repo_path, good_ref
82+
finally:
83+
shutil.rmtree(repo_path)

tests/filesystem/settings.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import os
1+
from typing import Union
2+
from os import PathLike
3+
from pathlib import Path
4+
from tempfile import gettempdir
5+
6+
7+
TEST_SAMPLES_PATH: str = "tests/filesystem/samples"
8+
REPO_FIXTURE_PATH: Union[str, PathLike] = Path(
9+
gettempdir(), "dlt_test_repo_t8hY3x"
10+
).absolute()
11+
REPO_SAFE_PREFIX: str = "test-"
12+
REPO_GOOD_REF = "good-ref"
213

314
FACTORY_ARGS = [
4-
{"bucket_url": os.path.abspath("tests/filesystem/samples")},
15+
{"bucket_url": str(Path(TEST_SAMPLES_PATH).absolute())},
516
{"bucket_url": "s3://dlt-ci-test-bucket/standard_source/samples"},
617
{"bucket_url": "gs://ci-test-bucket/standard_source/samples"},
718
{"bucket_url": "az://dlt-ci-test-bucket/standard_source/samples"},
819
{
9-
"bucket_url": "gitpythonfs://samples",
20+
"bucket_url": f"gitpythonfs://{REPO_SAFE_PREFIX}samples",
1021
"kwargs": {
11-
"repo_path": "tests/filesystem/cases/git",
12-
"ref": "unmodified-samples",
22+
"repo_path": REPO_FIXTURE_PATH,
23+
"ref": REPO_GOOD_REF,
1324
},
14-
}
25+
},
1526
]
1627

1728
GLOB_RESULTS = [

tests/filesystem/test_filesystem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def assert_sample_content(items: List[FileItem]):
6767
content = item.read_bytes()
6868
assert content == b"dlthub content"
6969
assert item["size_in_bytes"] == 14
70-
assert item["file_url"].endswith("/samples/sample.txt")
70+
assert item["file_url"].endswith("samples/sample.txt")
7171
assert item["mime_type"] == "text/plain"
7272
assert isinstance(item["modification_date"], pendulum.DateTime)
7373

@@ -93,7 +93,7 @@ def assert_csv_file(item: FileItem):
9393
# on windows when checking out, git will convert lf into cr+lf so we have more bytes (+ number of lines: 25)
9494
assert item["size_in_bytes"] in (742, 767)
9595
assert item["file_name"] == "met_csv/A801/A881_20230920.csv"
96-
assert item["file_url"].endswith("/samples/met_csv/A801/A881_20230920.csv")
96+
assert item["file_url"].endswith("samples/met_csv/A801/A881_20230920.csv")
9797
assert item["mime_type"] == "text/csv"
9898
# print(item)
9999
return item

0 commit comments

Comments
 (0)