Skip to content

Commit 1a8fcd0

Browse files
authored
fix: Address adding duplicate repositories and conflicts with manual vs package installed repositories. (#65)
This PR fixes two bugs: 1. An issue where multiple same-path repositories could be added. 2. Test cases which failed if there were package installed repositories (in addition to manually installed ones).
1 parent dd9a60c commit 1a8fcd0

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/firewheel/control/repository_db.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def add_repository(self, repository):
9393
# No database file exists yet.
9494
entries = []
9595

96+
if any(entry["path"] == repository["path"] for entry in entries):
97+
self.log.debug("Ignoring duplicate repository: %s", repository)
98+
return
99+
96100
entries.append(repository)
97101
with self.db_file.open("w") as db:
98102
json.dump(entries, db)
@@ -133,7 +137,8 @@ def delete_repository(self, repository):
133137
return 0
134138
except FileNotFoundError:
135139
self.log.debug(
136-
"%s repository path does not exist, but was removed anyways.", repository
140+
"%s repository path does not exist, but was removed anyways.",
141+
repository,
137142
)
138143

139144
with self.db_file.open("w") as db:

src/firewheel/tests/unit/control/test_repository_db.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ class TestRepositoryDb:
3737
"""Test the ``RepositoryDb`` object."""
3838

3939
@staticmethod
40-
def _entry_matches_repo_dict(repo_entry, repo_dict):
40+
def _entry_in_repo_list(repo_entry, repo_list):
4141
path = repo_entry["path"]
42-
return path == repo_dict["path"]
42+
return any(entry["path"] == path for entry in repo_list)
4343

4444
def test_new_repository(self, repository_db_test_path):
4545
location = repository_db_test_path
@@ -70,18 +70,16 @@ def test_corrupt_repository_add(self, repository_db_test_path, repo_entry):
7070
assert location.exists() is True
7171

7272
repository_db.add_repository(repo_entry)
73-
repo_dict = list(repository_db.list_repositories()).pop()
74-
assert len(repo_dict) == 1
75-
assert self._entry_matches_repo_dict(repo_entry, repo_dict)
73+
repo_list = list(repository_db.list_repositories())
74+
assert self._entry_in_repo_list(repo_entry, repo_list)
7675

7776
location.unlink(missing_ok=True)
7877
assert location.exists() is False
7978

8079
def test_add_repository(self, repository_db, repo_entry):
8180
repository_db.add_repository(repo_entry)
82-
repo_dict = list(repository_db.list_repositories()).pop()
83-
assert len(repo_dict) == 1
84-
assert self._entry_matches_repo_dict(repo_entry, repo_dict)
81+
repo_list = list(repository_db.list_repositories())
82+
assert self._entry_in_repo_list(repo_entry, repo_list)
8583

8684
@pytest.mark.parametrize(
8785
["invalid_entry", "exception"],
@@ -104,11 +102,13 @@ def test_add_repository_invalid(self, invalid_entry, exception, repository_db):
104102
repository_db.add_repository(invalid_entry)
105103

106104
def test_duplicate_repository(self, repository_db, repo_entry):
105+
orig_entry_count = len(list(repository_db.list_repositories()))
107106
repository_db.add_repository(repo_entry)
108107
repository_db.add_repository(repo_entry)
109-
repo_dict = list(repository_db.list_repositories()).pop()
110-
assert len(repo_dict) == 1
111-
assert self._entry_matches_repo_dict(repo_entry, repo_dict)
108+
repo_list = list(repository_db.list_repositories())
109+
assert self._entry_in_repo_list(repo_entry, repo_list)
110+
# The entry should only be added once
111+
assert len(repo_list) == orig_entry_count + 1
112112

113113
def test_delete_repository(self, repository_db, repo_entry):
114114
repository_db.add_repository(repo_entry)
@@ -118,5 +118,5 @@ def test_list_repositories(self, repository_db, repo_entries):
118118
orig_entry_count = len(list(repository_db.list_repositories()))
119119
for entry in repo_entries:
120120
repository_db.add_repository(entry)
121-
repo_list = list(repository_db.list_repositories())
122-
assert len(repo_list) == orig_entry_count + len(repo_entries)
121+
curr_count = len(list(repository_db.list_repositories()))
122+
assert curr_count == orig_entry_count + len(repo_entries)

0 commit comments

Comments
 (0)