summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Brunet <charles.brunet@optelgroup.com>2024-08-01 11:44:59 -0400
committerDylan Baker <dylan@pnwbakers.com>2024-08-01 12:09:58 -0700
commitfa4f2339465ce3d755e2df802ebd5aa962e2ad27 (patch)
treeae4bed889205f70c889fca85387285fd1d57b219
parentbdc2f2774c35ad2c81e3fea8621fdff6905c021b (diff)
downloadmeson-fa4f2339465ce3d755e2df802ebd5aa962e2ad27.tar.gz
mformat: fix formatting of empty build file
Running meson format multiple times on an empty file was adding a new line each time, which is bad for pre-commit checks...
-rw-r--r--mesonbuild/mformat.py2
-rw-r--r--unittests/platformagnostictests.py10
2 files changed, 10 insertions, 2 deletions
diff --git a/mesonbuild/mformat.py b/mesonbuild/mformat.py
index 33ee69d3c..bb93f47ef 100644
--- a/mesonbuild/mformat.py
+++ b/mesonbuild/mformat.py
@@ -396,6 +396,8 @@ class TrimWhitespaces(FullAstVisitor):
if node.lines:
self.move_whitespaces(node.lines[-1], node)
else:
+ node.whitespaces.value = node.pre_whitespaces.value + node.whitespaces.value
+ node.pre_whitespaces.value = ''
node.whitespaces.accept(self)
if node.condition_level == 0 and self.config.insert_final_newline:
diff --git a/unittests/platformagnostictests.py b/unittests/platformagnostictests.py
index 4ac4b7a55..c8f594d46 100644
--- a/unittests/platformagnostictests.py
+++ b/unittests/platformagnostictests.py
@@ -16,7 +16,7 @@ from pathlib import Path
from .baseplatformtests import BasePlatformTests
from .helpers import is_ci
from mesonbuild.mesonlib import EnvironmentVariables, ExecutableSerialisation, MesonException, is_linux, python_command
-from mesonbuild.mformat import match_path
+from mesonbuild.mformat import Formatter, match_path
from mesonbuild.optinterpreter import OptionInterpreter, OptionException
from mesonbuild.options import OptionStore
from run_tests import Backend
@@ -336,7 +336,13 @@ class PlatformAgnosticTests(BasePlatformTests):
for filename, pattern, expected in cases:
self.assertTrue(match_path(filename, pattern) is expected, f'{filename} -> {pattern}')
-
+
+ def test_format_empty_file(self) -> None:
+ formatter = Formatter(None, use_editor_config=False, fetch_subdirs=False)
+ for code in ('', '\n'):
+ formatted = formatter.format(code, Path())
+ self.assertEqual('\n', formatted)
+
def test_error_configuring_subdir(self):
testdir = os.path.join(self.common_test_dir, '152 index customtarget')
out = self.init(os.path.join(testdir, 'subdir'), allow_fail=True)