Skip to content

Commit 46831e5

Browse files
authored
Use parametrization of git-sim subcommand list to simplify tests (#100)
Signed-off-by: Eric Matthes <ehmatthes@gmail.com> Signed-off-by: Jacob Stopak <jacob@initialcommit.io>
1 parent de7f4c0 commit 46831e5

File tree

1 file changed

+40
-253
lines changed

1 file changed

+40
-253
lines changed

tests/e2e_tests/test_core_commands.py

Lines changed: 40 additions & 253 deletions
Original file line numberDiff line numberDiff line change
@@ -11,271 +11,58 @@
1111

1212
from utils import get_cmd_parts, compare_images, run_git_reset
1313

14+
import pytest
1415

15-
def test_add(tmp_repo):
16-
"""Test a simple `git-sim add` command."""
17-
raw_cmd = "git-sim add"
18-
cmd_parts = get_cmd_parts(raw_cmd)
19-
20-
os.chdir(tmp_repo)
21-
output = subprocess.run(cmd_parts, capture_output=True)
22-
23-
fp_generated = Path(output.stdout.decode().strip())
24-
fp_reference = Path(__file__).parent / "reference_files/git-sim-add.png"
25-
26-
assert "git-sim-add" in str(fp_generated)
27-
compare_images(fp_generated, fp_reference)
28-
29-
30-
def test_branch(tmp_repo):
31-
"""Test a simple `git-sim branch` command."""
32-
raw_cmd = "git-sim branch new_branch"
33-
cmd_parts = get_cmd_parts(raw_cmd)
34-
35-
os.chdir(tmp_repo)
36-
output = subprocess.run(cmd_parts, capture_output=True)
37-
38-
fp_generated = Path(output.stdout.decode().strip())
39-
fp_reference = Path(__file__).parent / "reference_files/git-sim-branch.png"
40-
41-
assert "git-sim-branch" in str(fp_generated)
42-
compare_images(fp_generated, fp_reference)
43-
44-
45-
def test_checkout(tmp_repo):
46-
"""Test a simple `git-sim checkout` command."""
47-
raw_cmd = "git-sim checkout branch2"
48-
cmd_parts = get_cmd_parts(raw_cmd)
49-
50-
os.chdir(tmp_repo)
51-
output = subprocess.run(cmd_parts, capture_output=True)
52-
53-
fp_generated = Path(output.stdout.decode().strip())
54-
fp_reference = Path(__file__).parent / "reference_files/git-sim-checkout.png"
55-
56-
assert "git-sim-checkout" in str(fp_generated)
57-
compare_images(fp_generated, fp_reference)
58-
59-
60-
def test_cherrypick(tmp_repo):
61-
"""Test a simple `git-sim cherry-pick` command."""
62-
raw_cmd = "git-sim cherry-pick branch2"
63-
cmd_parts = get_cmd_parts(raw_cmd)
64-
65-
os.chdir(tmp_repo)
66-
output = subprocess.run(cmd_parts, capture_output=True)
67-
68-
fp_generated = Path(output.stdout.decode().strip())
69-
fp_reference = Path(__file__).parent / "reference_files/git-sim-cherry_pick.png"
70-
71-
assert "git-sim-cherry_pick" in str(fp_generated)
72-
compare_images(fp_generated, fp_reference)
73-
74-
75-
def test_clean(tmp_repo):
76-
"""Test a simple `git-sim clean` command."""
77-
raw_cmd = "git-sim clean"
78-
cmd_parts = get_cmd_parts(raw_cmd)
7916

80-
os.chdir(tmp_repo)
81-
output = subprocess.run(cmd_parts, capture_output=True)
17+
git_sim_commands = [
18+
# Simple commands.
19+
"git-sim add",
20+
"git-sim log",
21+
"git-sim clean",
22+
"git-sim commit",
23+
"git-sim restore",
24+
"git-sim stash",
25+
"git-sim status",
8226

83-
fp_generated = Path(output.stdout.decode().strip())
84-
fp_reference = Path(__file__).parent / "reference_files/git-sim-clean.png"
27+
# Complex commands.
28+
"git-sim branch new_branch",
29+
"git-sim checkout branch2",
30+
"git-sim cherry-pick branch2",
31+
"git-sim merge branch2",
32+
"git-sim mv main.1 main.100",
33+
"git-sim rebase branch2",
34+
"git-sim reset HEAD^",
35+
"git-sim revert HEAD^",
36+
"git-sim rm main.1",
37+
"git-sim switch branch2",
38+
"git-sim tag new_tag",
39+
]
8540

86-
assert "git-sim-clean" in str(fp_generated)
87-
compare_images(fp_generated, fp_reference)
8841

42+
@pytest.mark.parametrize("raw_cmd", git_sim_commands)
43+
def test_command(tmp_repo, raw_cmd):
44+
"""Test a git-sim command.
8945
90-
def test_commit(tmp_repo):
91-
"""Test a simple `git-sim commit` command."""
92-
raw_cmd = "git-sim commit"
93-
cmd_parts = get_cmd_parts(raw_cmd)
46+
This function works for any command of the forms
47+
`git-sim <command`
48+
`git-sim <command> <arg>`
49+
"""
9450

95-
os.chdir(tmp_repo)
96-
output = subprocess.run(cmd_parts, capture_output=True)
97-
98-
fp_generated = Path(output.stdout.decode().strip())
99-
fp_reference = Path(__file__).parent / "reference_files/git-sim-commit.png"
100-
101-
assert "git-sim-commit" in str(fp_generated)
102-
compare_images(fp_generated, fp_reference)
51+
# Generate the string to look for in the filename.
52+
# `git-sim log` -> "git-sim-log"
53+
# `git-sim cherry-pick branch2` -> "git-sim-cherry_pick""
54+
raw_cmd_parts = raw_cmd.split(" ")
55+
filename_element = f"git-sim-{raw_cmd_parts[1].replace('-', '_')}"
10356

104-
105-
def test_log(tmp_repo):
106-
"""Test a simple `git-sim log` command."""
107-
raw_cmd = "git-sim log"
57+
# Get version of the command needed for testing, and run command.
10858
cmd_parts = get_cmd_parts(raw_cmd)
109-
110-
os.chdir(tmp_repo)
111-
output = subprocess.run(cmd_parts, capture_output=True)
112-
113-
fp_generated = Path(output.stdout.decode().strip())
114-
fp_reference = Path(__file__).parent / "reference_files/git-sim-log.png"
115-
116-
assert "git-sim-log" in str(fp_generated)
117-
compare_images(fp_generated, fp_reference)
118-
119-
120-
def test_merge(tmp_repo):
121-
"""Test a simple `git-sim merge` command."""
122-
raw_cmd = "git-sim merge branch2"
123-
cmd_parts = get_cmd_parts(raw_cmd)
124-
125-
os.chdir(tmp_repo)
126-
output = subprocess.run(cmd_parts, capture_output=True)
127-
128-
fp_generated = Path(output.stdout.decode().strip())
129-
fp_reference = Path(__file__).parent / "reference_files/git-sim-merge.png"
130-
131-
assert "git-sim-merge" in str(fp_generated)
132-
compare_images(fp_generated, fp_reference)
133-
134-
135-
def test_mv(tmp_repo):
136-
"""Test a simple `git-sim mv` command."""
137-
raw_cmd = "git-sim mv main.1 main.100"
138-
cmd_parts = get_cmd_parts(raw_cmd)
139-
140-
os.chdir(tmp_repo)
141-
output = subprocess.run(cmd_parts, capture_output=True)
142-
143-
fp_generated = Path(output.stdout.decode().strip())
144-
fp_reference = Path(__file__).parent / "reference_files/git-sim-mv.png"
145-
146-
assert "git-sim-mv" in str(fp_generated)
147-
compare_images(fp_generated, fp_reference)
148-
149-
150-
def test_rebase(tmp_repo):
151-
"""Test a simple `git-sim rebase` command."""
152-
raw_cmd = "git-sim rebase branch2"
153-
cmd_parts = get_cmd_parts(raw_cmd)
154-
155-
os.chdir(tmp_repo)
156-
output = subprocess.run(cmd_parts, capture_output=True)
157-
158-
fp_generated = Path(output.stdout.decode().strip())
159-
fp_reference = Path(__file__).parent / "reference_files/git-sim-rebase.png"
160-
161-
assert "git-sim-rebase" in str(fp_generated)
162-
compare_images(fp_generated, fp_reference)
163-
164-
165-
def test_reset(tmp_repo):
166-
"""Test a simple `git-sim reset` command."""
167-
raw_cmd = "git-sim reset HEAD^"
168-
cmd_parts = get_cmd_parts(raw_cmd)
169-
170-
os.chdir(tmp_repo)
171-
output = subprocess.run(cmd_parts, capture_output=True)
172-
173-
fp_generated = Path(output.stdout.decode().strip())
174-
fp_reference = Path(__file__).parent / "reference_files/git-sim-reset.png"
175-
176-
assert "git-sim-reset" in str(fp_generated)
177-
compare_images(fp_generated, fp_reference)
178-
179-
180-
def test_restore(tmp_repo):
181-
"""Test a simple `git-sim restore` command."""
182-
raw_cmd = "git-sim restore"
183-
cmd_parts = get_cmd_parts(raw_cmd)
184-
185-
os.chdir(tmp_repo)
186-
output = subprocess.run(cmd_parts, capture_output=True)
187-
188-
fp_generated = Path(output.stdout.decode().strip())
189-
fp_reference = Path(__file__).parent / "reference_files/git-sim-restore.png"
190-
191-
assert "git-sim-restore" in str(fp_generated)
192-
compare_images(fp_generated, fp_reference)
193-
194-
195-
def test_revert(tmp_repo):
196-
"""Test a simple `git-sim revert` command."""
197-
raw_cmd = "git-sim revert HEAD^"
198-
cmd_parts = get_cmd_parts(raw_cmd)
199-
200-
os.chdir(tmp_repo)
201-
output = subprocess.run(cmd_parts, capture_output=True)
202-
203-
fp_generated = Path(output.stdout.decode().strip())
204-
fp_reference = Path(__file__).parent / "reference_files/git-sim-revert.png"
205-
206-
assert "git-sim-revert" in str(fp_generated)
207-
compare_images(fp_generated, fp_reference)
208-
209-
210-
def test_rm(tmp_repo):
211-
"""Test a simple `git-sim rm` command."""
212-
raw_cmd = "git-sim rm main.1"
213-
cmd_parts = get_cmd_parts(raw_cmd)
214-
215-
os.chdir(tmp_repo)
216-
output = subprocess.run(cmd_parts, capture_output=True)
217-
218-
fp_generated = Path(output.stdout.decode().strip())
219-
fp_reference = Path(__file__).parent / "reference_files/git-sim-rm.png"
220-
221-
assert "git-sim-rm" in str(fp_generated)
222-
compare_images(fp_generated, fp_reference)
223-
224-
225-
def test_stash(tmp_repo):
226-
"""Test a simple `git-sim stash` command."""
227-
raw_cmd = "git-sim stash"
228-
cmd_parts = get_cmd_parts(raw_cmd)
229-
230-
os.chdir(tmp_repo)
231-
output = subprocess.run(cmd_parts, capture_output=True)
232-
233-
fp_generated = Path(output.stdout.decode().strip())
234-
fp_reference = Path(__file__).parent / "reference_files/git-sim-stash.png"
235-
236-
assert "git-sim-stash" in str(fp_generated)
237-
compare_images(fp_generated, fp_reference)
238-
239-
240-
def test_status(tmp_repo):
241-
"""Test a simple `git-sim status` command."""
242-
raw_cmd = "git-sim status"
243-
cmd_parts = get_cmd_parts(raw_cmd)
244-
245-
os.chdir(tmp_repo)
246-
output = subprocess.run(cmd_parts, capture_output=True)
247-
248-
fp_generated = Path(output.stdout.decode().strip())
249-
fp_reference = Path(__file__).parent / "reference_files/git-sim-status.png"
250-
251-
assert "git-sim-status" in str(fp_generated)
252-
compare_images(fp_generated, fp_reference)
253-
254-
def test_switch(tmp_repo):
255-
"""Test a simple `git-sim switch` command."""
256-
raw_cmd = "git-sim switch branch2"
257-
cmd_parts = get_cmd_parts(raw_cmd)
258-
259-
os.chdir(tmp_repo)
260-
output = subprocess.run(cmd_parts, capture_output=True)
261-
262-
fp_generated = Path(output.stdout.decode().strip())
263-
fp_reference = Path(__file__).parent / "reference_files/git-sim-switch.png"
264-
265-
assert "git-sim-switch" in str(fp_generated)
266-
compare_images(fp_generated, fp_reference)
267-
268-
269-
def test_tag(tmp_repo):
270-
"""Test a simple `git-sim tag` command."""
271-
raw_cmd = "git-sim tag new_tag"
272-
cmd_parts = get_cmd_parts(raw_cmd)
273-
27459
os.chdir(tmp_repo)
27560
output = subprocess.run(cmd_parts, capture_output=True)
27661

62+
# Get file paths to generated and reference files.
27763
fp_generated = Path(output.stdout.decode().strip())
278-
fp_reference = Path(__file__).parent / "reference_files/git-sim-tag.png"
64+
fp_reference = Path(__file__).parent / f"reference_files/{filename_element}.png"
27965

280-
assert "git-sim-tag" in str(fp_generated)
66+
# Validate filename elements, and compare output image to reference image.
67+
assert filename_element in str(fp_generated)
28168
compare_images(fp_generated, fp_reference)

0 commit comments

Comments
 (0)