Skip to content

Commit 332cdf9

Browse files
committed
feat(tests/git): Add tests for FileChanges and ChangeSet classes and related functions
1 parent e8780e6 commit 332cdf9

40 files changed

+618
-0
lines changed

tests/tools/test_git.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@
33
# SPDX-License-Identifier: MIT
44
from __future__ import annotations
55

6+
import json
67
import os
78
import random
9+
import shutil
810
from datetime import datetime
911
from typing import TYPE_CHECKING
1012

1113
import pytest
1214

1315
from dda.tools.git import Git
1416
from dda.utils.fs import Path
17+
from dda.utils.git.changeset import ChangeSet
1518
from dda.utils.git.commit import Commit
1619
from dda.utils.git.sha1hash import SHA1Hash
1720

1821
if TYPE_CHECKING:
1922
from collections.abc import Callable, Generator
2023

24+
from _pytest.fixtures import SubRequest
25+
2126
from dda.cli.application import Application
2227

2328

@@ -180,3 +185,99 @@ def test_get_commit_details(
180185
assert details.datetime == commit_time
181186
assert details.message == f"Brand-new commit: {random_key}"
182187
assert details.parent_shas == [SHA1Hash(parent_sha1)]
188+
189+
190+
def _make_repo_changes(
191+
git: Git, temp_repo: Path, base_dir: Path, changed_dir: Path, *, commit_end: bool = True
192+
) -> None:
193+
with temp_repo.as_cwd():
194+
# Create base commit
195+
# -- Copy files from base to temp_repo
196+
for file in base_dir.iterdir():
197+
shutil.copy(file, temp_repo / file.name)
198+
# -- Create commit
199+
git.run(["add", "."])
200+
git.run(["commit", "-m", "Initial commit"])
201+
# Create changed commit
202+
# -- Remove all files from temp_repo
203+
for file in temp_repo.iterdir():
204+
if file.is_file():
205+
file.unlink()
206+
# -- Copy files from changed to temp_repo
207+
for file in changed_dir.iterdir():
208+
shutil.copy(file, temp_repo / file.name)
209+
# -- Create commit if requested, otherwise leave working tree changes
210+
if commit_end:
211+
git.run(["add", "."])
212+
git.run(["commit", "-m", "Changed commit"])
213+
214+
215+
def _load_changeset(filepath: Path) -> ChangeSet:
216+
with open(filepath, encoding="utf-8") as f:
217+
data = json.load(f)
218+
return ChangeSet.from_list(data)
219+
220+
221+
REPO_TESTCASES = [path.name for path in (Path(__file__).parent / "testdata" / "repo_states").iterdir()]
222+
223+
224+
@pytest.fixture(params=REPO_TESTCASES)
225+
def repo_setup(app: Application, temp_repo: Path, request: SubRequest) -> tuple[Path, ChangeSet]:
226+
git: Git = app.tools.git
227+
testdata_dir = Path(__file__).parent / "testdata" / "repo_states"
228+
base_dir: Path = testdata_dir / request.param / "base"
229+
changed_dir: Path = testdata_dir / request.param / "changed"
230+
231+
# Make repo changes
232+
_make_repo_changes(git, temp_repo, base_dir, changed_dir, commit_end=True)
233+
234+
# Load expected changeset
235+
expected_changeset = _load_changeset(testdata_dir / request.param / "expected_changeset.json")
236+
237+
return temp_repo, expected_changeset
238+
239+
240+
@pytest.fixture(params=REPO_TESTCASES)
241+
def repo_setup_working_tree(app: Application, temp_repo: Path, request: SubRequest) -> tuple[Path, ChangeSet]:
242+
git: Git = app.tools.git
243+
testdata_dir = Path(__file__).parent / "testdata" / "repo_states"
244+
base_dir: Path = testdata_dir / request.param / "base"
245+
changed_dir: Path = testdata_dir / request.param / "changed"
246+
247+
# Make repo changes
248+
_make_repo_changes(git, temp_repo, base_dir, changed_dir, commit_end=False)
249+
250+
# Load expected changeset
251+
expected_changeset = _load_changeset(testdata_dir / request.param / "expected_changeset.json")
252+
253+
return temp_repo, expected_changeset
254+
255+
256+
# These tests are quite slow (the setup fixtures are quite heavy), and mostly replicated in the utils/git/test_changeset.py tests
257+
# Thus we only run them in CI
258+
@pytest.mark.requires_ci
259+
def test_get_commit_changes(app: Application, repo_setup: tuple[Path, ChangeSet]) -> None:
260+
git: Git = app.tools.git
261+
temp_repo, expected_changeset = repo_setup
262+
with temp_repo.as_cwd():
263+
changeset = git.get_commit_changes(SHA1Hash(git.capture(["rev-parse", "HEAD"]).strip()))
264+
assert changeset.keys() == expected_changeset.keys()
265+
for file in changeset:
266+
seen, expected = changeset[file], expected_changeset[file]
267+
assert seen.file == expected.file
268+
assert seen.type == expected.type
269+
assert seen.patch == expected.patch
270+
271+
272+
@pytest.mark.requires_ci
273+
def test_get_working_tree_changes(app: Application, repo_setup_working_tree: tuple[Path, ChangeSet]) -> None:
274+
git: Git = app.tools.git
275+
temp_repo, expected_changeset = repo_setup_working_tree
276+
with temp_repo.as_cwd():
277+
changeset = git.get_working_tree_changes()
278+
assert changeset.keys() == expected_changeset.keys()
279+
for file in changeset:
280+
seen, expected = changeset[file], expected_changeset[file]
281+
assert seen.file == expected.file
282+
assert seen.type == expected.type
283+
assert seen.patch == expected.patch
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I am file1 !
2+
I won't change at all.
3+
I guess that means I'm perfect as it is :)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
I am file2 !
2+
I feel like I take up space for nothing...
3+
I have a feeling like I won't exist pretty soon :/
4+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
I am file4.
2+
People often tell me I am unreliable.
3+
Things like:
4+
- You always change !
5+
- I can never count on you...
6+
- I didn't recognize you !
7+
Do you think they have a point ?
8+
I'd need to look at my own history to know...
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I am a humble file.
2+
Soon I will change name.
3+
I think I'll also take this as an opportunity to change myself.
4+
New name, new me !
5+
Or is that not how the saying goes ?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
I am file1 !
2+
I won't change at all.
3+
I guess that means I'm perfect as it is :)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
I am file3 !
2+
I'm new around here, hopefully everyone treats me nice :)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
I am file4.
2+
People often tell me I am THE BEST.
3+
Things like:
4+
- You rock !
5+
- I wish I were you !
6+
Do you think they have a point ?
7+
Pah ! Who am I kidding, they're OBVIOUSLY RIGHT.
8+
Arrogance ? What is that, an italian ice cream flavor ?
9+
Get outta here !
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
I am a humble file.
2+
Soon I will change name.
3+
I think I'll also take this as an opportunity to change myself.
4+
New name, new me !
5+
Or is that not how the saying goes ?
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
diff --git file2.txt file2.txt
2+
deleted file mode 100644
3+
index 70e6734..0000000
4+
--- file2.txt
5+
+++ /dev/null
6+
@@ -1,4 +0,0 @@
7+
-I am file2 !
8+
-I feel like I take up space for nothing...
9+
-I have a feeling like I won't exist pretty soon :/
10+
-
11+
diff --git file3.txt file3.txt
12+
new file mode 100644
13+
index 0000000..1c54fdf
14+
--- /dev/null
15+
+++ file3.txt
16+
@@ -0,0 +1,2 @@
17+
+I am file3 !
18+
+I'm new around here, hopefully everyone treats me nice :)
19+
diff --git file4.txt file4.txt
20+
index 73fa5d6..307c4c9 100644
21+
--- file4.txt
22+
+++ file4.txt
23+
@@ -2 +2 @@ I am file4.
24+
-People often tell me I am unreliable.
25+
+People often tell me I am THE BEST.
26+
@@ -4,3 +4,2 @@ Things like:
27+
-- You always change !
28+
-- I can never count on you...
29+
-- I didn't recognize you !
30+
+- You rock !
31+
+- I wish I were you !
32+
@@ -8 +7,3 @@ Do you think they have a point ?
33+
-I'd need to look at my own history to know...
34+
+Pah ! Who am I kidding, they're OBVIOUSLY RIGHT.
35+
+Arrogance ? What is that, an italian ice cream flavor ?
36+
+Get outta here !
37+
diff --git file5.txt file5.txt
38+
deleted file mode 100644
39+
index bf6c86e..0000000
40+
--- file5.txt
41+
+++ /dev/null
42+
@@ -1,5 +0,0 @@
43+
-I am a humble file.
44+
-Soon I will change name.
45+
-I think I'll also take this as an opportunity to change myself.
46+
-New name, new me !
47+
-Or is that not how the saying goes ?
48+
diff --git file5_new.txt file5_new.txt
49+
new file mode 100644
50+
index 0000000..bf6c86e
51+
--- /dev/null
52+
+++ file5_new.txt
53+
@@ -0,0 +1,5 @@
54+
+I am a humble file.
55+
+Soon I will change name.
56+
+I think I'll also take this as an opportunity to change myself.
57+
+New name, new me !
58+
+Or is that not how the saying goes ?

0 commit comments

Comments
 (0)