Skip to content

Commit 1af821b

Browse files
committed
fixup! CI - restyle script summary and annotations in PRs
1 parent a6d0d7b commit 1af821b

File tree

4 files changed

+221
-16
lines changed

4 files changed

+221
-16
lines changed

.github/workflows/style-check.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ jobs:
2626
- name: Style check
2727
run: |
2828
sudo apt update
29+
python ./tests/test_restyle.py --quiet
2930
bash ./tests/ci/style_check.sh
3031
3132
# Validate orthography

tests/host/common/ArduinoCatch.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
std::ostream& operator<<(std::ostream&, const String&);
2626

27-
namespace Catch {
27+
namespace Catch
28+
{
2829

2930
std::string toString(const String&);
3031

3132
template<>
32-
struct StringMaker<String> {
33+
struct StringMaker<String>
34+
{
3335
static std::string convert(const String&);
3436
};
3537

36-
} // namespace Catch
38+
} // namespace Catch

tests/restyle.py

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def ls_files(patterns):
4545
return out
4646

4747

48+
def diff_lines():
49+
proc = subprocess.run(
50+
["git", "--no-pager", "diff", "--ignore-submodules"],
51+
capture_output=True,
52+
check=True,
53+
universal_newlines=True,
54+
)
55+
56+
return proc.stdout.split("\n")
57+
58+
4859
def find_files(patterns):
4960
"""Filesystem search, matches both git and non-git files"""
5061
return [
@@ -98,7 +109,7 @@ def find_arduino_files():
98109
@dataclass
99110
class Changed:
100111
file: str
101-
hunk: list[str]
112+
hunk: str
102113
lines: list[int]
103114

104115

@@ -126,28 +137,31 @@ def pop(self, out, line):
126137
self.reset_with_line(line)
127138

128139

129-
def changed_files():
140+
def changed_files_for_diff(lines: list[str] | str) -> list[Changed]:
130141
"""
131-
Naive git-diff output parser. Generates list[Changed] for every file changed after clang-format.
142+
Naive git-diff output parser. Generates list of objects for every file changed after clang-format.
132143
"""
133-
proc = subprocess.run(
134-
["git", "--no-pager", "diff"],
135-
capture_output=True,
136-
check=True,
137-
universal_newlines=True,
138-
)
144+
match lines:
145+
case str():
146+
lines = lines.split("\n")
147+
case list():
148+
pass
149+
case _:
150+
raise ValueError("Unknown 'lines' type, can be either list[str] or str")
139151

140152
ctx = Context()
141153
out = []
142154

143155
# TODO: pygit2?
144156
# ref. https://github.com/cpp-linter/cpp-linter/blob/main/cpp_linter/git/__init__.py ::parse_diff
145157
# ref. https://github.com/libgit2/pygit2/blob/master/src/diff.c ::parse_diff
146-
for line in proc.stdout.split("\n"):
158+
for line in lines:
147159
# '--- a/path/to/changed/file' most likely
148-
# '--- a/dev/null' aka created file. should be ignored, same as removed ones
160+
# '--- /dev/null' aka created file. should be ignored, same as removed ones
149161
if line.startswith("---"):
150162
ctx.pop(out, line)
163+
164+
_, file = line.split(" ")
151165
ctx.deleted = "/dev/null" in file
152166

153167
# '+++ b/path/to/changed/file' most likely
@@ -183,6 +197,10 @@ def changed_files():
183197
return out
184198

185199

200+
def changed_files() -> list[Changed]:
201+
return changed_files_for_diff(diff_lines())
202+
203+
186204
def errors_changed(changed: Changed):
187205
all_lines = ", ".join(str(x) for x in changed.lines)
188206
for line in changed.lines:
@@ -204,12 +222,14 @@ def summary_diff(changed: Changed):
204222

205223

206224
def stdout_diff():
207-
subprocess.run(["git", "--no-pager", "diff"])
225+
subprocess.run(["git", "--no-pager", "diff", "--ignore-submodules"])
208226

209227

210228
def assert_unchanged():
211229
subprocess.run(
212-
["git", "diff", "--exit-code"], check=True, stdout=subprocess.DEVNULL
230+
["git", "diff", "--ignore-submodules", "--exit-code"],
231+
check=True,
232+
stdout=subprocess.DEVNULL,
213233
)
214234

215235

tests/test_restyle.py

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import unittest
2+
3+
from restyle import changed_files_for_diff
4+
5+
# small git-diff samples from https://queirozf.com/entries/git-diff-reference-and-examples
6+
7+
8+
class BaseTest(unittest.TestCase):
9+
def testNewLine(self):
10+
diff = """
11+
diff --git a/file.txt b/file.txt
12+
index 257cc56..3bd1f0e 100644
13+
--- a/file.txt
14+
+++ b/file.txt
15+
@@ -1 +1,2 @@
16+
foo
17+
+bar
18+
"""
19+
changed = changed_files_for_diff(diff)
20+
self.assertEqual(1, len(changed))
21+
self.assertEqual("file.txt", changed[0].file)
22+
self.assertEqual(1, len(changed[0].lines))
23+
self.assertEqual(1, changed[0].lines[0])
24+
25+
expected = """
26+
--- a/file.txt
27+
+++ b/file.txt
28+
@@ -1 +1,2 @@
29+
foo
30+
+bar
31+
""".strip()
32+
self.assertEqual(expected, changed[0].hunk.strip())
33+
34+
def testNewLines(self):
35+
diff = """
36+
diff --git a/file.txt b/file.txt
37+
index 257cc56..3bd1f0e 100644
38+
--- a/file2.txt
39+
+++ b/file2.txt
40+
@@ -1 +1,2 @@
41+
foo
42+
+bar
43+
baz
44+
@@ -1 +10,2 @@
45+
222
46+
-222
47+
333
48+
@@ -1 +100,3 @@
49+
aaa
50+
+bbb
51+
+ccc
52+
ddd
53+
"""
54+
changed = changed_files_for_diff(diff)
55+
self.assertEqual(1, len(changed))
56+
self.assertEqual("file2.txt", changed[0].file)
57+
58+
lines = changed[0].lines
59+
self.assertEqual(3, len(lines))
60+
61+
first, second, third = lines
62+
self.assertEqual(1, first)
63+
self.assertEqual(10, second)
64+
self.assertEqual(100, third)
65+
66+
expected = """
67+
--- a/file2.txt
68+
+++ b/file2.txt
69+
@@ -1 +1,2 @@
70+
foo
71+
+bar
72+
baz
73+
@@ -1 +10,2 @@
74+
222
75+
-222
76+
333
77+
@@ -1 +100,3 @@
78+
aaa
79+
+bbb
80+
+ccc
81+
ddd
82+
""".strip()
83+
self.assertEqual(expected, changed[0].hunk.strip())
84+
85+
def testRemovedLineAndDeletedFile(self):
86+
diff = """
87+
diff --git a/file.txt b/file.txt
88+
index 3bd1f0e..257cc56 100644
89+
--- a/file.txt
90+
+++ b/file.txt
91+
@@ -1,2 +1 @@
92+
foo
93+
-bar
94+
diff --git a/file2.txt b/file2.txt
95+
deleted file mode 100644
96+
index 85553e8..0000000
97+
--- a/file2.txt
98+
+++ /dev/null
99+
@@ -1,2 +0,0 @@
100+
-aaaaaa
101+
-bbbbbb
102+
"""
103+
changed = changed_files_for_diff(diff)
104+
self.assertEqual(1, len(changed))
105+
self.assertEqual("file.txt", changed[0].file)
106+
self.assertEqual(1, len(changed[0].lines))
107+
self.assertEqual(1, changed[0].lines[0])
108+
109+
expected = """
110+
--- a/file.txt
111+
+++ b/file.txt
112+
@@ -1,2 +1 @@
113+
foo
114+
-bar
115+
""".strip()
116+
self.assertEqual(expected, changed[0].hunk.strip())
117+
118+
def testNewLineAndDeletedFile(self):
119+
diff = """
120+
diff --git a/file.txt b/file.txt
121+
index 3bd1f0e..86e041d 100644
122+
--- a/file.txt
123+
+++ b/file.txt
124+
@@ -1,2 +1,3 @@
125+
foo
126+
bar
127+
+baz
128+
diff --git a/file2.txt b/file2.txt
129+
deleted file mode 100644
130+
index 85553e8..0000000
131+
--- a/file2.txt
132+
+++ /dev/null
133+
@@ -1,2 +0,0 @@
134+
-aaaaaa
135+
-bbbbbb
136+
"""
137+
changed = changed_files_for_diff(diff)
138+
self.assertEqual(1, len(changed))
139+
self.assertEqual("file.txt", changed[0].file)
140+
self.assertEqual(1, len(changed[0].lines))
141+
self.assertEqual(1, changed[0].lines[0])
142+
143+
expected = """
144+
--- a/file.txt
145+
+++ b/file.txt
146+
@@ -1,2 +1,3 @@
147+
foo
148+
bar
149+
+baz
150+
""".strip()
151+
self.assertEqual(expected, changed[0].hunk.strip())
152+
153+
def testDeletedFile(self):
154+
diff = """
155+
diff --git a/file2.txt b/file2.txt
156+
deleted file mode 100644
157+
index 85553e8..0000000
158+
--- a/file2.txt
159+
+++ /dev/null
160+
@@ -1,2 +0,0 @@
161+
-aaaaaa
162+
-bbbbbb
163+
"""
164+
changed = changed_files_for_diff(diff)
165+
self.assertEqual(0, len(changed))
166+
167+
def testNewFile(self):
168+
diff = """
169+
diff --git a/file3.txt b/file3.txt
170+
new file mode 100644
171+
index 0000000..a309e46
172+
--- /dev/null
173+
+++ b/file3.txt
174+
@@ -0,0 +1 @@
175+
+this is file3
176+
"""
177+
changed = changed_files_for_diff(diff)
178+
self.assertEqual(0, len(changed))
179+
180+
181+
if __name__ == '__main__':
182+
unittest.main()

0 commit comments

Comments
 (0)