Parameterize e2e tests using pytest

This commit is contained in:
2021-12-23 18:33:14 +01:00
parent 54fc48b37d
commit 3ac88260b5

View File

@@ -3,80 +3,75 @@
from helpers import * from helpers import *
import git import git
import pytest
import os.path import os.path
def test_worktree_add_simple(): @pytest.mark.parametrize("remote_branch_already_exists", [True, False])
for remote_branch_already_exists in (True, False): @pytest.mark.parametrize("has_config", [True, False])
for has_config in (True, False): @pytest.mark.parametrize("has_default", [True, False])
for has_default in (True, False): @pytest.mark.parametrize("has_prefix", [True, False])
for has_prefix in (True, False): def test_worktree_add_simple(
with TempGitRepositoryWorktree() as base_dir: remote_branch_already_exists, has_config, has_default, has_prefix
if has_config: ):
with open(os.path.join(base_dir, "grm.toml"), "w") as f: with TempGitRepositoryWorktree() as base_dir:
f.write( if has_config:
f""" with open(os.path.join(base_dir, "grm.toml"), "w") as f:
[track] f.write(
default = {str(has_default).lower()} f"""
default_remote = "origin" [track]
""" default = {str(has_default).lower()}
) default_remote = "origin"
if has_prefix: """
f.write( )
""" if has_prefix:
default_remote_prefix = "myprefix" f.write(
""" """
) default_remote_prefix = "myprefix"
"""
)
if remote_branch_already_exists: if remote_branch_already_exists:
shell( shell(
f""" f"""
cd {base_dir} cd {base_dir}
git --git-dir ./.git-main-working-tree worktree add tmp git --git-dir ./.git-main-working-tree worktree add tmp
( (
cd tmp cd tmp
touch change touch change
git add change git add change
git commit -m commit git commit -m commit
git push origin HEAD:test git push origin HEAD:test
#git reset --hard 'HEAD@{1}' #git reset --hard 'HEAD@{1}'
git branch -va git branch -va
) )
git --git-dir ./.git-main-working-tree worktree remove tmp git --git-dir ./.git-main-working-tree worktree remove tmp
""" """
) )
cmd = grm(["wt", "add", "test"], cwd=base_dir) cmd = grm(["wt", "add", "test"], cwd=base_dir)
assert cmd.returncode == 0 assert cmd.returncode == 0
files = os.listdir(base_dir) files = os.listdir(base_dir)
if has_config is True: if has_config is True:
assert len(files) == 3 assert len(files) == 3
assert set(files) == { assert set(files) == {".git-main-working-tree", "grm.toml", "test"}
".git-main-working-tree", else:
"grm.toml", assert len(files) == 2
"test", assert set(files) == {".git-main-working-tree", "test"}
}
else:
assert len(files) == 2
assert set(files) == {".git-main-working-tree", "test"}
repo = git.Repo(os.path.join(base_dir, "test")) repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare assert not repo.bare
assert not repo.is_dirty() assert not repo.is_dirty()
if has_config and has_default: if has_config and has_default:
if has_prefix and not remote_branch_already_exists: if has_prefix and not remote_branch_already_exists:
assert ( assert (
str(repo.active_branch.tracking_branch()) str(repo.active_branch.tracking_branch()) == "origin/myprefix/test"
== "origin/myprefix/test" )
) else:
else: assert str(repo.active_branch.tracking_branch()) == "origin/test"
assert ( else:
str(repo.active_branch.tracking_branch()) assert repo.active_branch.tracking_branch() is None
== "origin/test"
)
else:
assert repo.active_branch.tracking_branch() is None
def test_worktree_add_into_subdirectory(): def test_worktree_add_into_subdirectory():
@@ -109,191 +104,173 @@ def test_worktree_add_into_invalid_subdirectory():
assert "dir" not in os.listdir(base_dir) assert "dir" not in os.listdir(base_dir)
def test_worktree_add_with_tracking(): @pytest.mark.parametrize("remote_branch_already_exists", [True, False])
for remote_branch_already_exists in (True, False): @pytest.mark.parametrize("has_config", [True, False])
for has_config in (True, False): @pytest.mark.parametrize("has_default", [True, False])
for has_default in (True, False): @pytest.mark.parametrize("has_prefix", [True, False])
for has_prefix in (True, False): def test_worktree_add_with_tracking(
with TempGitRepositoryWorktree() as base_dir: remote_branch_already_exists, has_config, has_default, has_prefix
if has_config: ):
with open(os.path.join(base_dir, "grm.toml"), "w") as f: with TempGitRepositoryWorktree() as base_dir:
f.write( if has_config:
f""" with open(os.path.join(base_dir, "grm.toml"), "w") as f:
[track] f.write(
default = {str(has_default).lower()} f"""
default_remote = "origin" [track]
""" default = {str(has_default).lower()}
) default_remote = "origin"
if has_prefix: """
f.write( )
""" if has_prefix:
default_remote_prefix = "myprefix" f.write(
"""
)
if remote_branch_already_exists:
shell(
f"""
cd {base_dir}
git --git-dir ./.git-main-working-tree worktree add tmp
(
cd tmp
touch change
git add change
git commit -m commit
git push origin HEAD:test
#git reset --hard 'HEAD@{1}'
git branch -va
)
git --git-dir ./.git-main-working-tree worktree remove tmp
"""
)
cmd = grm(
["wt", "add", "test", "--track", "origin/test"],
cwd=base_dir,
)
print(cmd.stderr)
assert cmd.returncode == 0
files = os.listdir(base_dir)
if has_config is True:
assert len(files) == 3
assert set(files) == {
".git-main-working-tree",
"grm.toml",
"test",
}
else:
assert len(files) == 2
assert set(files) == {".git-main-working-tree", "test"}
repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare
assert not repo.is_dirty()
assert str(repo.active_branch) == "test"
assert (
str(repo.active_branch.tracking_branch()) == "origin/test"
)
def test_worktree_add_with_explicit_no_tracking():
for has_config in (True, False):
for has_default in (True, False):
for has_prefix in (True, False):
for track in (True, False):
with TempGitRepositoryWorktree() as base_dir:
if has_config:
with open(os.path.join(base_dir, "grm.toml"), "w") as f:
f.write(
f"""
[track]
default = {str(has_default).lower()}
default_remote = "origin"
"""
)
if has_prefix:
f.write(
"""
default_remote_prefix = "myprefix"
"""
)
if track is True:
cmd = grm(
[
"wt",
"add",
"test",
"--track",
"origin/test",
"--no-track",
],
cwd=base_dir,
)
else:
cmd = grm(["wt", "add", "test", "--no-track"], cwd=base_dir)
print(cmd.stderr)
assert cmd.returncode == 0
files = os.listdir(base_dir)
if has_config is True:
assert len(files) == 3
assert set(files) == {
".git-main-working-tree",
"grm.toml",
"test",
}
else:
assert len(files) == 2
assert set(files) == {".git-main-working-tree", "test"}
repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare
assert not repo.is_dirty()
assert str(repo.active_branch) == "test"
assert repo.active_branch.tracking_branch() is None
def test_worktree_add_with_config():
for remote_branch_already_exists in (True, False):
for has_default in (True, False):
for has_prefix in (True, False):
with TempGitRepositoryWorktree() as base_dir:
with open(os.path.join(base_dir, "grm.toml"), "w") as f:
f.write(
f"""
[track]
default = {str(has_default).lower()}
default_remote = "origin"
""" """
) default_remote_prefix = "myprefix"
if has_prefix: """
f.write( )
"""
default_remote_prefix = "myprefix" if remote_branch_already_exists:
""" shell(
) f"""
if remote_branch_already_exists: cd {base_dir}
shell( git --git-dir ./.git-main-working-tree worktree add tmp
f""" (
cd {base_dir} cd tmp
git --git-dir ./.git-main-working-tree worktree add tmp touch change
( git add change
cd tmp git commit -m commit
touch change git push origin HEAD:test
git add change #git reset --hard 'HEAD@{1}'
git commit -m commit git branch -va
git push origin HEAD:test )
#git reset --hard 'HEAD@{1}' git --git-dir ./.git-main-working-tree worktree remove tmp
git branch -va """
) )
git --git-dir ./.git-main-working-tree worktree remove tmp cmd = grm(["wt", "add", "test", "--track", "origin/test"], cwd=base_dir)
print(cmd.stderr)
assert cmd.returncode == 0
files = os.listdir(base_dir)
if has_config is True:
assert len(files) == 3
assert set(files) == {".git-main-working-tree", "grm.toml", "test"}
else:
assert len(files) == 2
assert set(files) == {".git-main-working-tree", "test"}
repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare
assert not repo.is_dirty()
assert str(repo.active_branch) == "test"
assert str(repo.active_branch.tracking_branch()) == "origin/test"
@pytest.mark.parametrize("has_config", [True, False])
@pytest.mark.parametrize("has_default", [True, False])
@pytest.mark.parametrize("has_prefix", [True, False])
@pytest.mark.parametrize("track", [True, False])
def test_worktree_add_with_explicit_no_tracking(
has_config, has_default, has_prefix, track
):
with TempGitRepositoryWorktree() as base_dir:
if has_config:
with open(os.path.join(base_dir, "grm.toml"), "w") as f:
f.write(
f"""
[track]
default = {str(has_default).lower()}
default_remote = "origin"
"""
)
if has_prefix:
f.write(
""" """
) default_remote_prefix = "myprefix"
cmd = grm(["wt", "add", "test"], cwd=base_dir) """
print(cmd.stderr) )
assert cmd.returncode == 0 if track is True:
cmd = grm(
["wt", "add", "test", "--track", "origin/test", "--no-track"],
cwd=base_dir,
)
else:
cmd = grm(["wt", "add", "test", "--no-track"], cwd=base_dir)
print(cmd.stderr)
assert cmd.returncode == 0
files = os.listdir(base_dir) files = os.listdir(base_dir)
assert len(files) == 3 if has_config is True:
assert set(files) == {".git-main-working-tree", "grm.toml", "test"} assert len(files) == 3
assert set(files) == {".git-main-working-tree", "grm.toml", "test"}
else:
assert len(files) == 2
assert set(files) == {".git-main-working-tree", "test"}
repo = git.Repo(os.path.join(base_dir, "test")) repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare assert not repo.bare
assert not repo.is_dirty() assert not repo.is_dirty()
assert str(repo.active_branch) == "test" assert str(repo.active_branch) == "test"
if has_default: assert repo.active_branch.tracking_branch() is None
if has_prefix and not remote_branch_already_exists:
assert (
str(repo.active_branch.tracking_branch()) @pytest.mark.parametrize("remote_branch_already_exists", [True, False])
== "origin/myprefix/test" @pytest.mark.parametrize("has_default", [True, False])
) @pytest.mark.parametrize("has_prefix", [True, False])
else: def test_worktree_add_with_config(
assert ( remote_branch_already_exists, has_default, has_prefix
str(repo.active_branch.tracking_branch()) ):
== "origin/test" with TempGitRepositoryWorktree() as base_dir:
) with open(os.path.join(base_dir, "grm.toml"), "w") as f:
else: f.write(
assert repo.active_branch.tracking_branch() is None f"""
[track]
default = {str(has_default).lower()}
default_remote = "origin"
"""
)
if has_prefix:
f.write(
"""
default_remote_prefix = "myprefix"
"""
)
if remote_branch_already_exists:
shell(
f"""
cd {base_dir}
git --git-dir ./.git-main-working-tree worktree add tmp
(
cd tmp
touch change
git add change
git commit -m commit
git push origin HEAD:test
#git reset --hard 'HEAD@{1}'
git branch -va
)
git --git-dir ./.git-main-working-tree worktree remove tmp
"""
)
cmd = grm(["wt", "add", "test"], cwd=base_dir)
print(cmd.stderr)
assert cmd.returncode == 0
files = os.listdir(base_dir)
assert len(files) == 3
assert set(files) == {".git-main-working-tree", "grm.toml", "test"}
repo = git.Repo(os.path.join(base_dir, "test"))
assert not repo.bare
assert not repo.is_dirty()
assert str(repo.active_branch) == "test"
if has_default:
if has_prefix and not remote_branch_already_exists:
assert (
str(repo.active_branch.tracking_branch()) == "origin/myprefix/test"
)
else:
assert str(repo.active_branch.tracking_branch()) == "origin/test"
else:
assert repo.active_branch.tracking_branch() is None
def test_worktree_delete(): def test_worktree_delete():