From c274f09a6251a4f89e799320aff827d4a1a4f06e Mon Sep 17 00:00:00 2001 From: georgepsarakis Date: Sun, 17 Dec 2023 09:56:56 +0200 Subject: [PATCH] Add coverage statistics in CI --- .github/workflows/test.yml | 6 ++++-- loader/loader.go | 3 +-- main_test.go | 20 ++++++-------------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1f51904..cb26abd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,9 +13,11 @@ jobs: go-version: '1.21.x' - name: Install dependencies run: go get . + - name: Set up gotestfmt + run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest - name: Build run: go build -v ./... - name: Unit Tests - run: go test -v ./... + run: go test -v -json -race -coverprofile=cover.out ./... | gotestfmt - name: CLI Integration Tests - run: go test -v main_test.go + run: go test -v -json -race main_test.go | gotestfmt diff --git a/loader/loader.go b/loader/loader.go index 1adcf2d..83ef078 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -2,7 +2,6 @@ package loader import ( "bytes" - "errors" "fmt" pg_query "github.com/pganalyze/pg_query_go/v4" migrate "github.com/rubenv/sql-migrate" @@ -76,7 +75,7 @@ func ReadStatementsFromFiles(paths ...string) ([]MigrationFile, error) { return nil, err } if fileInfo.IsDir() { - return nil, errors.New(fmt.Sprintf("expected file and %s is a directory", p)) + return nil, fmt.Errorf("expected file and %s is a directory", p) } sql, err := os.ReadFile(p) if err != nil { diff --git a/main_test.go b/main_test.go index f5499f4..aef6598 100644 --- a/main_test.go +++ b/main_test.go @@ -23,19 +23,7 @@ func TestExecutable_ListRulesCommand(t *testing.T) { } const expectedFailureOutput = ` - Rule high-availability-avoid-table-rename violation found for statement: - ALTER TABLE "movies" RENAME TO "movies_old"; - Explanation: Renaming a table can cause errors in previous application versions. - - Rule high-availability-avoid-required-column violation found for statement: - ALTER TABLE "recipe" ADD COLUMN "public" boolean NOT NULL, ADD COLUMN "private" boolean; - Explanation: Newly added columns must either define a default value or be nullable. - - Rule high-availability-alter-column-not-null-exclusive-lock violation found for statement: - ALTER TABLE movies ALTER COLUMN "public" SET NOT NULL; - Explanation: Setting a column as NOT NULL acquires an exclusive lock on the table until the constraint is validated on all table rows. - - Rule high-availability-avoid-non-concurrent-index-creation violation found for statement: +Rule high-availability-avoid-non-concurrent-index-creation violation found for statement: CREATE INDEX ON films (created_at); Explanation: Non-concurrent index creation will not allow writes while the index is being built. @@ -48,7 +36,7 @@ const expectedFailureOutput = ` Explanation: Non-concurrent index creation will not allow writes while the index is being built. Rule transactions-concurrent-index-operation-cannot-be-executed-in-transaction violation found for statement: - CREATE INDEX CONCURRENTLY "email_idx" ON "app_user" ("email"); + CREATE INDEX CONCURRENTLY "email_idx" ON "companies" ("email"); Explanation: Concurrent index operations cannot be executed inside a transaction. Rule transactions-no-nested-transactions violation found for statement: @@ -59,6 +47,10 @@ const expectedFailureOutput = ` COMMIT; Explanation: Nested transactions are not supported in PostgreSQL. + Rule high-availability-avoid-table-rename violation found for statement: + ALTER TABLE "movies" RENAME TO "movies_old"; + Explanation: Renaming a table can cause errors in previous application versions. + Rule high-availability-avoid-non-concurrent-index-drop violation found for statement: DROP INDEX IF EXISTS title_idx; Explanation: Non-concurrent index drop will not allow writes while the index is being built.