Skip to content

Commit aa6b3eb

Browse files
committed
Ensure yamlfmt respects trunk-managed configs
1 parent 1052a5b commit aa6b3eb

File tree

5 files changed

+84
-2
lines changed

5 files changed

+84
-2
lines changed

linters/yamlfmt/plugin.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ lint:
2424
commands:
2525
- name: format
2626
output: rewrite
27-
run: yamlfmt ${target}
28-
run_from: ${parent}
27+
run: python3 ${plugin}/linters/yamlfmt/run_yamlfmt.py --workspace ${workspace} ${target}
28+
run_from: workspace
2929
success_codes: [0, 1]
3030
cache_results: true
3131
formatter: true
3232
in_place: true
3333
batch: true
34+
environment:
35+
- name: PATH
36+
list: ["${linter}", "${env.PATH:-}"]
3437
tools: [yamlfmt]
3538
direct_configs:
3639
- .yamlfmt

linters/yamlfmt/run_yamlfmt.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
"""Helper to run yamlfmt with trunk-managed configs."""
3+
4+
from __future__ import annotations
5+
6+
import argparse
7+
import os
8+
import subprocess
9+
import sys
10+
from typing import List
11+
12+
CONFIG_BASENAMES: List[str] = [
13+
".yamlfmt",
14+
".yamlfmt.yaml",
15+
".yamlfmt.yml",
16+
"yamlfmt.yaml",
17+
"yamlfmt.yml",
18+
]
19+
20+
21+
def find_config(workspace: str) -> str | None:
22+
search_roots = [workspace, os.path.join(workspace, ".trunk", "configs")]
23+
for root in search_roots:
24+
for basename in CONFIG_BASENAMES:
25+
candidate = os.path.join(root, basename)
26+
if os.path.isfile(candidate):
27+
return candidate
28+
return None
29+
30+
31+
def main() -> None:
32+
parser = argparse.ArgumentParser()
33+
parser.add_argument(
34+
"--workspace", required=True, help="Absolute path to the repo root"
35+
)
36+
parser.add_argument("paths", nargs="+", help="Targets to pass to yamlfmt")
37+
args = parser.parse_args()
38+
39+
workspace = os.path.abspath(args.workspace)
40+
config_path = find_config(workspace)
41+
42+
cmd = ["yamlfmt"]
43+
if config_path:
44+
cmd.extend(["-conf", config_path])
45+
cmd.extend(args.paths)
46+
47+
result = subprocess.run(cmd)
48+
sys.exit(result.returncode)
49+
50+
51+
if __name__ == "__main__":
52+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: >-
2+
Builds and pushes a Docker image to a container registry.
3+
Requires authentication to be handled by the calling workflow.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Testing formatter yamlfmt test config 1`] = `
4+
"description: >-
5+
Builds and pushes a Docker image to a container registry.
6+
Requires authentication to be handled by the calling workflow.
7+
"
8+
`;

linters/yamlfmt/yamlfmt.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import { linterCheckTest, linterFmtTest } from "tests";
2+
import { TrunkLintDriver } from "tests/driver";
23
import { osTimeoutMultiplier } from "tests/utils";
34

45
// This install is quite slow on some Linux machines.
56
jest.setTimeout(600000 * osTimeoutMultiplier);
67

8+
const writeTrunkConfig = (driver: TrunkLintDriver) => {
9+
const config = `formatter:
10+
type: basic
11+
scan_folded_as_literal: true
12+
retain_line_breaks: true
13+
`;
14+
driver.writeFile(".trunk/configs/.yamlfmt.yml", config);
15+
};
16+
717
linterCheckTest({ linterName: "yamlfmt", namedTestPrefixes: ["empty"] });
818

919
linterFmtTest({ linterName: "yamlfmt", namedTestPrefixes: ["basic"] });
20+
21+
linterFmtTest({
22+
linterName: "yamlfmt",
23+
namedTestPrefixes: ["config"],
24+
preCheck: writeTrunkConfig,
25+
});

0 commit comments

Comments
 (0)