diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index a96edf0..1d53a79 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -26,6 +26,8 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install uv + sudo apt-get install npm + npm install --global prettier - name: Setup run: | uv venv @@ -33,6 +35,9 @@ jobs: - name: Check formatting with black run: | uv tool run black --check . + - name: Check formatting with prettier + run: | + prettier . --check - name: Run flake8 run: | uv tool run flake8 src/ --ignore=E203,W503,E722,E731 --max-complexity=100 --max-line-length=160 diff --git a/.github/workflows/update-syntax-description.yml b/.github/workflows/update-syntax-description.yml index 3156c5d..07b6817 100644 --- a/.github/workflows/update-syntax-description.yml +++ b/.github/workflows/update-syntax-description.yml @@ -31,6 +31,8 @@ jobs: run: | python -m pip install --upgrade pip python -m pip install cf-remote + sudo apt-get install npm + npm install --global prettier - name: Install cfengine run: | cf-remote --version master download --edition community ubuntu24 amd64 hub @@ -41,7 +43,7 @@ jobs: ( sudo cf-promises --syntax-description json ) > new.json - echo "" >> new.json + prettier new.json --write - name: Set Git user run: | git config user.name 'github-actions[bot]' diff --git a/src/cfengine_cli/syntax-description.json b/src/cfengine_cli/syntax-description.json index 3fbbc16..b743e3c 100644 --- a/src/cfengine_cli/syntax-description.json +++ b/src/cfengine_cli/syntax-description.json @@ -3258,13 +3258,7 @@ "status": "normal" }, "common": { - "promiseTypes": [ - "classes", - "defaults", - "meta", - "reports", - "vars" - ], + "promiseTypes": ["classes", "defaults", "meta", "reports", "vars"], "status": "normal" }, "edit_line": { diff --git a/tests/shell/004-format-check.sh b/tests/shell/004-format-check.sh new file mode 100755 index 0000000..f05acc4 --- /dev/null +++ b/tests/shell/004-format-check.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -e +set -x + +# Setup: create a temp directory for test files +tmpdir=$(mktemp -d) +trap "rm -rf $tmpdir" EXIT + +write_formatted() { + printf 'bundle agent main\n{\n vars:\n "v" string => "hello";\n}\n' > "$1" +} + +write_unformatted() { + printf 'bundle agent main { vars: "v" string => "hello"; }\n' > "$1" +} + +# Case 1: format without --check on already-formatted file -> exit 0 +write_formatted "$tmpdir/good.cf" +cfengine format "$tmpdir/good.cf" + +# Case 2: format without --check on unformatted file -> exit 0 (reformats it) +write_unformatted "$tmpdir/bad.cf" +cfengine format "$tmpdir/bad.cf" +# Verify it was actually reformatted to the correct output +write_formatted "$tmpdir/expected.cf" +diff "$tmpdir/expected.cf" "$tmpdir/bad.cf" + +# Case 3: --check on already-formatted file -> exit 0 +write_formatted "$tmpdir/good2.cf" +cfengine format --check "$tmpdir/good2.cf" + +# Case 4: --check on unformatted file -> exit 1 +write_unformatted "$tmpdir/bad2.cf" +cp "$tmpdir/bad2.cf" "$tmpdir/bad2_orig.cf" +if cfengine format --check "$tmpdir/bad2.cf"; then + echo "FAIL: expected exit code 1 for --check on unformatted file" + exit 1 +fi +# Verify the file was NOT modified +diff "$tmpdir/bad2_orig.cf" "$tmpdir/bad2.cf"