Skip to content

Commit

Permalink
Merge pull request #164 from LUMC/issue162
Browse files Browse the repository at this point in the history
Fix symlinks to directories fail when copying with --git-aware flag.
  • Loading branch information
rhpvorderman committed Dec 21, 2022
2 parents 714a0aa + 7db1d6f commit 97a26af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -12,6 +12,7 @@ version 2.0.0-dev
+ Python 3.6 is no longer supported. It has been removed from github actions,
as such we can no longer guarantee that pytest-workflow works properly
with python 3.6.
+ Fix an issue where symlinks in git repositories could not be properly copied.
+ Added an optional encoding key for files, stdout and stderr so the file can
be opened with the proper encoding.
+ Make content tests more efficient by reading each file only once instead of
Expand Down
4 changes: 3 additions & 1 deletion src/pytest_workflow/util.py
Expand Up @@ -173,7 +173,9 @@ def duplicate_tree(src: Filepath, dest: Filepath,
copy: Callable[[Filepath, Filepath], None] = \
functools.partial(os.symlink, target_is_directory=False)
else:
copy = shutil.copy2 # Preserves metadata, also used by shutil.copytree
# shutil.copy2 preserves metadata, also used by shutil.copytree
# follow_symlinks False to directly copy links
copy = functools.partial(shutil.copy2, follow_symlinks=False)

os.makedirs(dest, exist_ok=False)
for src_path, dest_path, is_dir in path_iter:
Expand Down
36 changes: 31 additions & 5 deletions tests/test_utils.py
Expand Up @@ -166,17 +166,25 @@ def create_git_repo(path):
os.mkdir(dir)
file = dir / "README.md"
file.write_text("# My new project\n\nHello this project is awesome!\n")
subdir = dir / "sub"
subdir.mkdir()
another_file = subdir / "subtext.md"
another_file.write_text("# Subtext\n\nSome other example text.\n")
subdir_link = dir / "gosub"
subdir_link.symlink_to(subdir.relative_to(dir), target_is_directory=True)
subprocess.run(["git", "init", "-b", "main"], cwd=dir)
subprocess.run(["git", "config", "user.name", "A U Thor"], cwd=dir)
subprocess.run(["git", "config", "user.email", "author@example.com"],
cwd=dir)
subprocess.run(["git", "add", "README.md"], cwd=dir)
subprocess.run(["git", "add", "."], cwd=dir)
subprocess.run(["git", "commit", "-m", "initial commit"], cwd=dir)


def test_git_submodule_check(tmp_path):
bird_repo = tmp_path / "bird"
nest_repo = tmp_path / "nest"
@pytest.fixture()
def git_repo_with_submodules():
repo_dir = Path(tempfile.mkdtemp())
bird_repo = repo_dir / "bird"
nest_repo = repo_dir / "nest"
create_git_repo(bird_repo)
create_git_repo(nest_repo)
# https://bugs.launchpad.net/ubuntu/+source/git/+bug/1993586
Expand All @@ -185,11 +193,16 @@ def test_git_submodule_check(tmp_path):
cwd=nest_repo.absolute())
subprocess.run(["git", "commit", "-m", "add bird repo as a submodule"],
cwd=nest_repo.absolute())
yield nest_repo
shutil.rmtree(repo_dir)


def test_git_submodule_check(git_repo_with_submodules, tmp_path):
cloned_repo = tmp_path / "cloned"
subprocess.run(
# No recursive clone
["git", "-c", "protocol.file.allow=always",
"clone", nest_repo.absolute(), cloned_repo.absolute()],
"clone", git_repo_with_submodules.absolute(), cloned_repo.absolute()],
cwd=tmp_path
)
with pytest.raises(RuntimeError) as error:
Expand All @@ -201,3 +214,16 @@ def test_git_submodule_check(tmp_path):
cwd=cloned_repo.absolute())
# Check error does not occur when issue resolved.
git_check_submodules_cloned(cloned_repo)


# https://github.com/LUMC/pytest-workflow/issues/162
def test_duplicate_git_tree_submodule_symlinks(git_repo_with_submodules):
assert (git_repo_with_submodules / ".git").exists()
dest = Path(tempfile.mkdtemp()) / "test"
duplicate_tree(git_repo_with_submodules, dest, git_aware=True)
assert dest.exists()
assert not (dest / ".git").exists()
link = dest / "bird" / "gosub"
assert link.exists()
assert link.is_symlink()
assert link.resolve() == dest / "bird" / "sub"

0 comments on commit 97a26af

Please sign in to comment.