Skip to content

Commit b2687e8

Browse files
committed
Do not accidentally format files when only checking if they are formatted.
1 parent 53f6ef3 commit b2687e8

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

mesonbuild/scripts/clangformat.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@ def parse_pattern_file(fname: Path) -> T.List[str]:
3636
return patterns
3737

3838
def run_clang_format(exelist: T.List[str], fname: Path, check: bool) -> subprocess.CompletedProcess:
39+
if check:
40+
original = fname.read_bytes()
3941
before = fname.stat().st_mtime
40-
ret = subprocess.run(exelist + ['-style=file', '-i', str(fname)])
42+
args = ['-style=file', '-i', str(fname)]
43+
ret = subprocess.run(exelist + args)
4144
after = fname.stat().st_mtime
4245
if before != after:
4346
print('File reformatted: ', fname)
4447
if check:
48+
# Restore the original if only checking.
49+
fname.write_bytes(original)
4550
ret.returncode = 1
4651
return ret
4752

run_unittests.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5634,7 +5634,7 @@ def test_devenv(self):
56345634
self._run(cmd + python_command + [script])
56355635
self.assertEqual('This is text.', self._run(cmd + [app]).strip())
56365636

5637-
def test_clang_format(self):
5637+
def test_clang_format_check(self):
56385638
if self.backend is not Backend.ninja:
56395639
raise unittest.SkipTest(f'Skipping clang-format tests with {self.backend.name} backend')
56405640
if not shutil.which('clang-format'):
@@ -5658,9 +5658,10 @@ def test_clang_format(self):
56585658
output = self.build('clang-format-check')
56595659
self.assertEqual(1, output.count('File reformatted:'))
56605660

5661-
# All code has been reformatted already, so it should be no-op now.
5661+
# The check format should not touch any files. Thus
5662+
# running format again has some work to do.
56625663
output = self.build('clang-format')
5663-
self.assertEqual(0, output.count('File reformatted:'))
5664+
self.assertEqual(1, output.count('File reformatted:'))
56645665
self.build('clang-format-check')
56655666

56665667
def test_custom_target_implicit_include(self):

0 commit comments

Comments
 (0)