Skip to content

Commit 9eb99ac

Browse files
committed
Port tests/ changes from #181
1 parent ae990bf commit 9eb99ac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1714
-2
lines changed

tests/tools/conftest.py

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

6+
import shutil
67
from typing import TYPE_CHECKING
78

9+
import msgspec
810
import pytest
911

1012
from dda.utils.fs import Path
13+
from dda.utils.git.changeset import ChangeSet
1114

1215
if TYPE_CHECKING:
1316
from collections.abc import Callable, Generator
1417

18+
from _pytest.fixtures import SubRequest
19+
1520
from dda.cli.application import Application
1621
from dda.tools.git import Git
1722

@@ -70,3 +75,75 @@ def _create_commit_dummy_file(location: Path | str, content: str, commit_message
7075
git.run(["commit", "-m", commit_message])
7176

7277
return _create_commit_dummy_file
78+
79+
80+
@pytest.fixture
81+
def temp_repo_with_remote(app: Application, temp_repo: Path) -> Path:
82+
with temp_repo.as_cwd():
83+
app.tools.git.run(["remote", "add", "origin", "https://github.com/foo/bar"])
84+
return temp_repo
85+
86+
87+
REPO_TESTCASES = [path.name for path in (Path(__file__).parent / "fixtures" / "repo_states").iterdir()]
88+
89+
90+
def _make_repo_changes(
91+
git: Git, temp_repo: Path, base_dir: Path, changed_dir: Path, *, commit_end: bool = True
92+
) -> None:
93+
with temp_repo.as_cwd():
94+
# Create base commit
95+
# -- Copy files from base to temp_repo
96+
for file in base_dir.iterdir():
97+
shutil.copy(file, temp_repo / file.name)
98+
# -- Create commit
99+
git.run(["add", "."])
100+
git.run(["commit", "-m", "Initial commit"])
101+
# Create changed commit
102+
# -- Remove all files from temp_repo
103+
for file in temp_repo.iterdir():
104+
if file.is_file():
105+
file.unlink()
106+
# -- Copy files from changed to temp_repo
107+
for file in changed_dir.iterdir():
108+
shutil.copy(file, temp_repo / file.name)
109+
# -- Create commit if requested, otherwise leave working tree changes
110+
if commit_end:
111+
git.run(["add", "."])
112+
git.run(["commit", "-m", "Changed commit"])
113+
114+
115+
def _load_changeset(filepath: Path) -> ChangeSet:
116+
with open(filepath, encoding="utf-8") as f:
117+
return msgspec.json.decode(f.read(), type=ChangeSet, dec_hook=ChangeSet.dec_hook)
118+
119+
120+
@pytest.fixture(params=REPO_TESTCASES)
121+
def repo_setup(app: Application, temp_repo: Path, request: SubRequest) -> tuple[Path, ChangeSet]:
122+
git: Git = app.tools.git
123+
fixtures_dir = Path(__file__).parent / "fixtures" / "repo_states"
124+
base_dir: Path = fixtures_dir / request.param / "base"
125+
changed_dir: Path = fixtures_dir / request.param / "changed"
126+
127+
# Make repo changes
128+
_make_repo_changes(git, temp_repo, base_dir, changed_dir, commit_end=True)
129+
130+
# Load expected changeset
131+
expected_changeset = _load_changeset(fixtures_dir / request.param / "expected_changeset.json")
132+
133+
return temp_repo, expected_changeset
134+
135+
136+
@pytest.fixture(params=REPO_TESTCASES)
137+
def repo_setup_working_tree(app: Application, temp_repo: Path, request: SubRequest) -> tuple[Path, ChangeSet]:
138+
git: Git = app.tools.git
139+
fixtures_dir = Path(__file__).parent / "fixtures" / "repo_states"
140+
base_dir: Path = fixtures_dir / request.param / "base"
141+
changed_dir: Path = fixtures_dir / request.param / "changed"
142+
143+
# Make repo changes
144+
_make_repo_changes(git, temp_repo, base_dir, changed_dir, commit_end=False)
145+
146+
# Load expected changeset
147+
expected_changeset = _load_changeset(fixtures_dir / request.param / "expected_changeset.json")
148+
149+
return temp_repo, expected_changeset
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+
Hey I have a new name !
3+
Wow, I feel much better now.
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+
+Hey I have a new name !
56+
+Wow, I feel much better now.
57+
+New name, new me !
58+
+Or is that not how the saying goes ?

0 commit comments

Comments
 (0)