aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2021-04-29 19:18:41 +0300
committerJussi Pakkanen <jpakkane@gmail.com>2021-05-01 23:34:14 +0300
commitb2687e86c87a1dd2e254aeaf3355b3e98b82b730 (patch)
treef3b315d1f6e4f5b22b1e27158f4d019ce43092c2
parent53f6ef3b7be92f17f9084f87c1eee5b521f8bb47 (diff)
downloadmeson-b2687e86c87a1dd2e254aeaf3355b3e98b82b730.zip
meson-b2687e86c87a1dd2e254aeaf3355b3e98b82b730.tar.gz
meson-b2687e86c87a1dd2e254aeaf3355b3e98b82b730.tar.bz2
Do not accidentally format files when only checking if they are formatted.
-rw-r--r--mesonbuild/scripts/clangformat.py7
-rwxr-xr-xrun_unittests.py7
2 files changed, 10 insertions, 4 deletions
diff --git a/mesonbuild/scripts/clangformat.py b/mesonbuild/scripts/clangformat.py
index ceb36ac..99e1ea5 100644
--- a/mesonbuild/scripts/clangformat.py
+++ b/mesonbuild/scripts/clangformat.py
@@ -36,12 +36,17 @@ def parse_pattern_file(fname: Path) -> T.List[str]:
return patterns
def run_clang_format(exelist: T.List[str], fname: Path, check: bool) -> subprocess.CompletedProcess:
+ if check:
+ original = fname.read_bytes()
before = fname.stat().st_mtime
- ret = subprocess.run(exelist + ['-style=file', '-i', str(fname)])
+ args = ['-style=file', '-i', str(fname)]
+ ret = subprocess.run(exelist + args)
after = fname.stat().st_mtime
if before != after:
print('File reformatted: ', fname)
if check:
+ # Restore the original if only checking.
+ fname.write_bytes(original)
ret.returncode = 1
return ret
diff --git a/run_unittests.py b/run_unittests.py
index c52d13c..828c80e 100755
--- a/run_unittests.py
+++ b/run_unittests.py
@@ -5634,7 +5634,7 @@ class AllPlatformTests(BasePlatformTests):
self._run(cmd + python_command + [script])
self.assertEqual('This is text.', self._run(cmd + [app]).strip())
- def test_clang_format(self):
+ def test_clang_format_check(self):
if self.backend is not Backend.ninja:
raise unittest.SkipTest(f'Skipping clang-format tests with {self.backend.name} backend')
if not shutil.which('clang-format'):
@@ -5658,9 +5658,10 @@ class AllPlatformTests(BasePlatformTests):
output = self.build('clang-format-check')
self.assertEqual(1, output.count('File reformatted:'))
- # All code has been reformatted already, so it should be no-op now.
+ # The check format should not touch any files. Thus
+ # running format again has some work to do.
output = self.build('clang-format')
- self.assertEqual(0, output.count('File reformatted:'))
+ self.assertEqual(1, output.count('File reformatted:'))
self.build('clang-format-check')
def test_custom_target_implicit_include(self):