diff options
author | Samson <16504129+sagudev@users.noreply.github.com> | 2023-09-19 10:57:27 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-19 08:57:27 +0000 |
commit | 7caac9790d772776a036ef239d87e426a2312b00 (patch) | |
tree | 46ac54a1bc2d4d419a2e47f952ce25ce810ab47b /python/servo | |
parent | 576887907c17d99ab56d52e40c505f0db3def20d (diff) | |
download | servo-7caac9790d772776a036ef239d87e426a2312b00.tar.gz servo-7caac9790d772776a036ef239d87e426a2312b00.zip |
Enforce formatting of TOML files (#30128)
* Fmt all toml files
* bootstrap taplo
* enforce toml formatting with taplo
* Install taplo in CI using cargo-install action
Diffstat (limited to 'python/servo')
-rw-r--r-- | python/servo/platform/base.py | 15 | ||||
-rw-r--r-- | python/servo/testing_commands.py | 23 |
2 files changed, 35 insertions, 3 deletions
diff --git a/python/servo/platform/base.py b/python/servo/platform/base.py index 7fde2b60c97..8af3e2056eb 100644 --- a/python/servo/platform/base.py +++ b/python/servo/platform/base.py @@ -8,6 +8,7 @@ # except according to those terms. import os +import shutil import subprocess from typing import Dict, Optional @@ -96,9 +97,21 @@ class Base: ) def bootstrap(self, force: bool): - if not self._platform_bootstrap(force): + installed_something = self._platform_bootstrap(force) + installed_something |= self.install_taplo(force) + if not installed_something: print("Dependencies were already installed!") + def install_taplo(self, force: bool) -> bool: + if not force and shutil.which("taplo") is not None: + return False + + if subprocess.call(["cargo", "install", "taplo-cli", "--locked"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) != 0: + raise EnvironmentError("Installation of taplo failed.") + + return True + def passive_bootstrap(self) -> bool: """A bootstrap method that is called without explicitly invoking `./mach bootstrap` but that is executed in the process of other `./mach` commands. This should be diff --git a/python/servo/testing_commands.py b/python/servo/testing_commands.py index 328a82a7a23..a0e60cb2e72 100644 --- a/python/servo/testing_commands.py +++ b/python/servo/testing_commands.py @@ -57,6 +57,20 @@ TEST_SUITES = OrderedDict([ TEST_SUITES_BY_PREFIX = {path: k for k, v in TEST_SUITES.items() if "paths" in v for path in v["paths"]} +def format_toml_files_with_taplo(check_only: bool = True) -> int: + taplo = shutil.which("taplo") + if taplo is None: + print("Taplo is not installed.") + print("It should be installed using `./mach bootstrap`, \ + but it can be installed manually using `cargo install taplo-cli --locked`") + return 1 + + if check_only: + return call([taplo, "fmt", "--check"], env={'RUST_LOG': 'error'}) + else: + return call([taplo, "fmt"], env={'RUST_LOG': 'error'}) + + @CommandProvider class MachCommands(CommandBase): DEFAULT_RENDER_MODE = "cpu" @@ -279,7 +293,9 @@ class MachCommands(CommandBase): if rustfmt_failed: print("Run `./mach fmt` to fix the formatting") - return tidy_failed or manifest_dirty or rustfmt_failed + taplo_failed = format_toml_files_with_taplo() + + return tidy_failed or manifest_dirty or rustfmt_failed or taplo_failed @Command('test-scripts', description='Run tests for all build and support scripts.', @@ -370,9 +386,12 @@ class MachCommands(CommandBase): return wpt.manifestupdate.update(check_clean=False) @Command('fmt', - description='Format the Rust and CPP source files with rustfmt', + description='Format Rust and TOML files', category='testing') def format_code(self): + result = format_toml_files_with_taplo(check_only=False) + if result != 0: + return result return call(["cargo", "fmt"]) @Command('update-wpt', |