Skip to content

Commit 21b1126

Browse files
committed
Gerrit: support all possible file statuses
Renamed, copied and rewritten, all treated as modifications. Closes haiku#5.
1 parent 21b9665 commit 21b1126

File tree

3 files changed

+69
-4
lines changed

3 files changed

+69
-4
lines changed

formatchecker/gerrit.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def get_change(self, change_id: str, revision_id: str = "current") -> Change:
4242
for filename in change_dict.keys():
4343
status = change_dict[filename].get("status", "M")
4444
file_get_url = urljoin(current_revision_url, "files/%s/content" % quote(filename, safe=''))
45-
if status not in ["M", "D", "A"]:
45+
if status in 'RC':
46+
old_file_get_url = urljoin(current_revision_url, "files/%s/content" % quote(change_dict[filename]['old_path'], safe=''))
47+
else:
48+
old_file_get_url = file_get_url
49+
if status not in ["M", "D", "A", "R", "C", "W"]:
4650
raise RuntimeError("Unsupported file status change")
4751
# get the contents of the current patch version of the file
4852
if status != "D":
@@ -51,7 +55,7 @@ def get_change(self, change_id: str, revision_id: str = "current") -> Change:
5155
else:
5256
patch_content = None
5357
if status != "A":
54-
base_content = self._get(file_get_url, params={"parent": "1"})
58+
base_content = self._get(old_file_get_url, params={"parent": "1"})
5559
base_content = StringIO(base_content).readlines()
5660
else:
5761
base_content = None

tests/data/test_gerrit_revision_files.json

+22-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,26 @@
1616
"lines_deleted": 51,
1717
"size_delta": -967,
1818
"size": 0
19+
},
20+
"src/file_renamed": {
21+
"status": "R",
22+
"old_path": "old/file_renamed",
23+
"lines_inserted": 2,
24+
"lines_deleted": 3,
25+
"size_delta": -30,
26+
"size": 314
27+
},
28+
"src/file_copied": {
29+
"status": "C",
30+
"old_path": "old/file_copied",
31+
"size_delta": 0,
32+
"size": 314
33+
},
34+
"src/file_rewritten": {
35+
"status": "W",
36+
"lines_inserted": 200,
37+
"lines_deleted": 300,
38+
"size_delta": -30,
39+
"size": 314
40+
}
1941
}
20-
}

tests/test_gerrit.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,53 @@ def context_get_mock(self, url: str, params=None):
4141
else:
4242
raise RuntimeError("Invalid request for src/file_implicitly_modified content")
4343
elif url =="changes/test_get_change/revisions/current/files/src%2Ffile_deleted/content":
44-
# This file is modified, so there is new and parent contents
44+
# This file is deleted, so only support getting the old contents
4545
if params is None:
4646
raise RuntimeError("The src/file_deleted file is deleted, so the patched version should not be requested")
4747
elif params == {"parent": "1"}:
4848
return "file_deleted line 1\nfile_deleted line 2\n"
4949
else:
5050
raise RuntimeError("Invalid request for src/file_implicitly_modified content")
51+
elif url =="changes/test_get_change/revisions/current/files/src%2Ffile_renamed/content":
52+
# This is the new path of a renamed file, so only support getting the new contents
53+
if params is None:
54+
return "file_renamed line 1\nfile_renamed base line 2\n"
55+
elif params == {"parent": "1"}:
56+
raise RuntimeError("The src/file_renamed file is added, so the base version should not be requested")
57+
else:
58+
raise RuntimeError("Invalid request for src/file_renamed content")
59+
elif url =="changes/test_get_change/revisions/current/files/old%2Ffile_renamed/content":
60+
# This is the old path of a renamed file, so only support getting the old contents
61+
if params is None:
62+
raise RuntimeError("The old/file_renamed file was renamed, so the patched version should not be requested")
63+
elif params == {"parent": "1"}:
64+
return "file_renamed line 1\nfile_renamed patched line 2\n"
65+
else:
66+
raise RuntimeError("Invalid request for old/file_renamed content")
67+
elif url =="changes/test_get_change/revisions/current/files/src%2Ffile_copied/content":
68+
# This is the new path of a copied file, so only support getting the new contents
69+
if params is None:
70+
return "file_copied line 1\nfile_copied base line 2\n"
71+
elif params == {"parent": "1"}:
72+
raise RuntimeError("The src/file_copied file is added, so the base version should not be requested")
73+
else:
74+
raise RuntimeError("Invalid request for src/file_copied content")
75+
elif url =="changes/test_get_change/revisions/current/files/old%2Ffile_copied/content":
76+
# This is the old path of a copied file, so only support getting the old contents
77+
if params is None:
78+
raise RuntimeError("The old/file_copied file was copied in this change, so the patched version should not be requested")
79+
elif params == {"parent": "1"}:
80+
return "file_copied line 1\nfile_copied patched line 2\n"
81+
else:
82+
raise RuntimeError("Invalid request for old/file_copied content")
83+
elif url =="changes/test_get_change/revisions/current/files/src%2Ffile_rewritten/content":
84+
# This file is modified, so there is new and parent contents
85+
if params is None:
86+
return "file_rewritten line 1\n2Ffile_rewritten base line 2\n"
87+
elif params == {"parent": "1"}:
88+
return "file_rewritten line 1\n2Ffile_rewritten patched line 2\n"
89+
else:
90+
raise RuntimeError("Invalid request for src/file_rewritten content")
5191
raise ValueError("Input URL is not mocked: %s" % url)
5292

5393

0 commit comments

Comments
 (0)