Skip to content

Commit 76eb08d

Browse files
author
Frances Hocutt
committed
Add test fixtures to git-related unit tests [#22]
* Refactor tests to use a temporary folder and test fixtures for the tests involving git repositories * Remove the test repositories that had accidentally been implemented as git submodules
1 parent 599b5d5 commit 76eb08d

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

tests/fixtures/git-repo-dev-branch

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/fixtures/git-repo-one-branch

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/test_rules.py

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,42 @@
1010

1111
from openlinter.rules import *
1212

13+
14+
# Test fixtures
15+
16+
@pytest.fixture()
17+
def setup_empty_repo(tmpdir):
18+
# tmpdir is a LocalPath, git chokes unless you cast to str
19+
git.Repo.init(str(tmpdir))
20+
21+
@pytest.fixture()
22+
def setup_repo_one_br_one_commit(tmpdir):
23+
repo = git.Repo.init(str(tmpdir))
24+
# Make temporary file for the temporary repo
25+
with open(str(tmpdir.join('test_file.txt')), 'w') as f:
26+
f.write('This is a file with text.\n')
27+
repo.index.add([str(tmpdir.join('test_file.txt'))])
28+
repo.index.commit('initial commit')
29+
30+
@pytest.fixture()
31+
def setup_repo_two_br_two_commits(tmpdir):
32+
repo = git.Repo.init(str(tmpdir))
33+
# Make first temporary file and commit it
34+
with open(str(tmpdir.join('test_file.txt')), 'w') as f:
35+
f.write('This is a file with text.\n')
36+
repo.index.add([str(tmpdir.join('test_file.txt'))])
37+
repo.index.commit('initial commit')
38+
39+
# Make second temporary file and commit it
40+
with open(str(tmpdir.join('test_file2.txt')), 'w') as f:
41+
f.write('This is a second file with text.\n')
42+
repo.index.add([str(tmpdir.join('test_file2.txt'))])
43+
repo.index.commit('second commit')
44+
45+
# Make the dev branch
46+
new_branch = repo.create_head('develop')
47+
48+
1349
# Tests for openlinter.rules.check_file_presence()
1450

1551
def test_check_file_presence_nonexistent_file():
@@ -67,8 +103,8 @@ def test_get_file_text_file_exists_has_text():
67103

68104
# Tests for openlinter.rules.detect_version_control()
69105

70-
def test_detect_version_control_repository_exists():
71-
result = detect_version_control('tests/fixtures/test-git-repo')
106+
def test_detect_version_control_repository_exists(setup_empty_repo, tmpdir):
107+
result = detect_version_control(str(tmpdir))
72108
assert result == 'git'
73109

74110
def test_detect_version_control_folder_does_not_exist():
@@ -81,64 +117,64 @@ def test_detect_version_control_folder_has_no_vc():
81117

82118
# Tests for openlinter.rules.check_multiple_branches()
83119

84-
def test_check_multiple_branches_empty_repo():
85-
assert check_multiple_branches('tests/fixtures/test-git-repo') == False
120+
def test_check_multiple_branches_empty_repo(setup_empty_repo, tmpdir):
121+
assert check_multiple_branches(str(tmpdir)) == False
86122

87123
def test_check_multiple_branches_nonexistent_repo():
88124
with pytest.raises(git.InvalidGitRepositoryError):
89125
check_multiple_branches('tests/fixtures/pic-folder')
90126

91-
def test_check_multiple_branches_nonexistent_repo():
127+
def test_check_multiple_branches_nonexistent_folder():
92128
with pytest.raises(git.NoSuchPathError):
93129
check_multiple_branches('zzyzx')
94130

95-
def test_check_multiple_branches_repo_with_one_branch():
96-
result = check_multiple_branches('tests/fixtures/git-repo-one-branch')
131+
def test_check_multiple_branches_repo_one_branch(setup_repo_one_br_one_commit, tmpdir):
132+
result = check_multiple_branches(str(tmpdir))
97133
assert result == False
98134

99-
def test_check_multiple_branches_repo_with_two_branches():
100-
result = check_multiple_branches('tests/fixtures/git-repo-dev-branch')
135+
def test_check_multiple_branches_repo_with_two_branches(setup_repo_two_br_two_commits, tmpdir):
136+
result = check_multiple_branches(str(tmpdir))
101137
assert result == True
102138

103139

104140
# Tests for openlinter.rules.check_for_develop_branch()
105141

106-
def test_check_for_develop_branch_nonexistent_repo():
142+
def test_check_for_develop_branch_nonexistent_folder():
107143
with pytest.raises(git.NoSuchPathError):
108144
check_for_develop_branch('zzyzx', 'dev')
109145

110146
def test_check_for_develop_branch_nonexistent_repo():
111147
with pytest.raises(git.InvalidGitRepositoryError):
112148
check_for_develop_branch('tests/fixtures/pic-folder', 'dev')
113149

114-
def test_check_for_develop_branch_repo_without_dev_branch():
115-
result = check_for_develop_branch('tests/fixtures/test-git-repo', 'dev')
150+
def test_check_for_develop_branch_repo_without_dev_branch(setup_empty_repo, tmpdir):
151+
result = check_for_develop_branch(str(tmpdir), 'develop')
116152
assert result == False
117153

118-
def test_check_for_develop_branch_repo_has_dev_branch():
119-
result = check_for_develop_branch('tests/fixtures/git-repo-dev-branch',
154+
def test_check_for_develop_branch_repo_has_dev_branch(setup_repo_two_br_two_commits, tmpdir):
155+
result = check_for_develop_branch(str(tmpdir),
120156
'develop')
121157
assert result == True
122158

123159

124160
# Tests for openlinter.rules.check_for_multiple_commits()
125161

126-
def test_check_for_multiple_commits_nonexistent_repo():
162+
def test_check_for_multiple_commits_nonexistent_folder():
127163
with pytest.raises(git.NoSuchPathError):
128164
check_for_multiple_commits('zzyzx')
129165

130166
def test_check_for_multiple_commits_nonexistent_repo():
131167
with pytest.raises(git.InvalidGitRepositoryError):
132168
check_for_multiple_commits('tests/fixtures/pic-folder')
133169

134-
def test_check_for_multiple_commits_no_commits():
135-
result = check_for_multiple_commits('tests/fixtures/test-git-repo')
170+
def test_check_for_multiple_commits_no_commits(setup_empty_repo, tmpdir):
171+
result = check_for_multiple_commits(str(tmpdir))
136172
assert result is False
137173

138-
def test_check_for_multiple_commits_one_commit():
139-
result = check_for_multiple_commits('tests/fixtures/git-repo-one-branch')
174+
def test_check_for_multiple_commits_one_commit(setup_repo_one_br_one_commit, tmpdir):
175+
result = check_for_multiple_commits(str(tmpdir))
140176
assert result == False
141177

142-
def test_check_for_multiple_commits_two_commits():
143-
result = check_for_multiple_commits('tests/fixtures/git-repo-dev-branch')
178+
def test_check_for_multiple_commits_two_commits(setup_repo_two_br_two_commits, tmpdir):
179+
result = check_for_multiple_commits(str(tmpdir))
144180
assert result == True

0 commit comments

Comments
 (0)