diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index fc170c7..99a0fcf 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -9,7 +9,7 @@ on: - main jobs: - test: + unit: runs-on: ubuntu-latest steps: - name: Checkout the repo @@ -18,5 +18,36 @@ jobs: - name: Install taskfile run: sudo snap install task --classic - - name: Run tests - run: task test + - name: Run unit tests + run: task test:unit + + pre-commit: + runs-on: ubuntu-latest + steps: + - name: Checkout the repo + uses: actions/checkout@v6 + with: + fetch-depth: 0 + fetch-tags: true + + - name: Install taskfile + run: sudo snap install task --classic + + - name: Install GoReleaser + run: sudo snap install --classic goreleaser + + - name: Install pre-commit dependencies + run: | + sudo snap install helm --classic + sudo npm install -g bats + sudo npm install -g https://github.com/ztombol/bats-assert + sudo npm install -g https://github.com/ztombol/bats-support + pip install pre-commit + + - name: Install the plugin + run: | + task build:plugin -- --snapshot + task reinstall + + - name: Run pre-commit hook tests + run: task test:pre-commit diff --git a/README.md b/README.md index 8159b67..2000eb8 100644 --- a/README.md +++ b/README.md @@ -514,6 +514,15 @@ Build the binaries and plugin will require providing the `--snapshot` flag if yo task build:plugin -- --snapshot ``` +bats install + +``` +brew install bats-core +brew tap kaos/shell +brew install bats-assert +``` + + ### New release Bump the changelog: diff --git a/cmd/helm-values/internal/precommit.go b/cmd/helm-values/internal/precommit.go index ce7745b..733c1fa 100644 --- a/cmd/helm-values/internal/precommit.go +++ b/cmd/helm-values/internal/precommit.go @@ -14,17 +14,17 @@ const preCommitConfigPath = ".pre-commit-config.yaml" var schemaPreCommitHook = &PreCommitHook{ ID: "helm-values-schema", Name: "Generate Helm values schema", - Entry: "helm values schema . --git-add", + Entry: "helm values schema --git-add .", Language: "system", - Files: "^.*/values\\.yaml$", + Files: "values\\.yaml$", PassFilenames: lo.ToPtr(false), } var docsPreCommitHook = &PreCommitHook{ ID: "helm-values-docs", Name: "Generate Helm values documentation", - Entry: "helm values docs . --git-add", + Entry: "helm values docs --git-add .", Language: "system", - Files: "^.*/values\\.yaml$", + Files: "values\\.yaml$", PassFilenames: lo.ToPtr(false), } diff --git a/cmd/helm-values/main.go b/cmd/helm-values/main.go index b016070..e4b6e16 100644 --- a/cmd/helm-values/main.go +++ b/cmd/helm-values/main.go @@ -113,7 +113,7 @@ func CommandModeline(logger *logrus.Logger, group *cobra.Group) *cobra.Command { cfg := config.NewModelineConfig() cmd := &cobra.Command{ - Use: "modeline [flags] chart_ref [values_file]", + Use: "modeline [flags] chart_ref values_file", Short: "Add yaml-language-server modeline to values file", GroupID: group.ID, Args: cobra.RangeArgs(1, 2), diff --git a/taskfile.yaml b/taskfile.yaml index c7a9170..cb60557 100644 --- a/taskfile.yaml +++ b/taskfile.yaml @@ -1,6 +1,12 @@ +# yaml-language-server: $schema=https://taskfile.dev/schema.json + version: '3' tasks: + run: + cmds: + - go run ./cmd/helm-values {{.CLI_ARGS}} + build:bin: desc: Build production binary cmds: @@ -20,14 +26,26 @@ tasks: desc: Reinstall the plugin locally cmds: - helm plugin uninstall values || true - - helm plugin install ./dist/values-*.tgz + - helm plugin install ./dist/values-$(git describe --tags --abbrev=0).tgz test: + desc: Run all tests + cmds: + - task: test:unit + - task: test:pre-commit + + test:unit: desc: Run unit tests cmds: - go test ./pkg/... - go test ./cmd/... + test:pre-commit: + desc: Run pre-commit hook tests + dir: ./tests/pre-commit + cmds: + - bats ./tests.bats + clean: desc: Clean build artifacts cmds: diff --git a/tests/pre-commit/tests.bats b/tests/pre-commit/tests.bats new file mode 100644 index 0000000..79e48df --- /dev/null +++ b/tests/pre-commit/tests.bats @@ -0,0 +1,32 @@ +#!/usr/bin/env bats + +setup() { + if command -v brew &> /dev/null; then + BREW_PREFIX="$(brew --prefix)" + load "${BREW_PREFIX}/lib/bats-support/load.bash" + load "${BREW_PREFIX}/lib/bats-assert/load.bash" + else + NPM_PREFIX="$(npm config get prefix)" + load "${NPM_PREFIX}/lib/node_modules/bats-support/load.bash" + load "${NPM_PREFIX}/lib/node_modules/bats-assert/load.bash" + fi + + touch values.yaml + task run -- pre-commit +} + +teardown() { + rm -f values.yaml + rm -f .pre-commit-config.yaml +} + +@test "Generate helm values schema" { + pre-commit run --files values.yaml + pre-commit run --files values.yaml | grep 'Generate Helm values schema' | grep 'Passed' + assert_success +} + +@test "Generate helm values documentation" { + pre-commit run --files values.yaml | grep 'Generate Helm values documentation' | grep Passed + assert_success +}