From c761b18c7d0406ad8e2bb963c4a7d8b8f78cf983 Mon Sep 17 00:00:00 2001 From: MasseElch <12862103+masseelch@users.noreply.github.com> Date: Fri, 18 Mar 2022 15:44:44 +0100 Subject: [PATCH] cmd/action: tests for atlas migrate validate command (#661) * cmd/action: tests for atlas migrate validate command * cmd/action: linter --- cmd/action/migrate.go | 4 +-- cmd/action/migrate_test.go | 28 ++++++++++++++++++- .../migrations/20220318104614_initial.sql | 4 +++ cmd/action/migrations/atlas.sum | 2 ++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 cmd/action/migrations/20220318104614_initial.sql create mode 100644 cmd/action/migrations/atlas.sum diff --git a/cmd/action/migrate.go b/cmd/action/migrate.go index aaba3a3a17b..c11f6a68e4b 100644 --- a/cmd/action/migrate.go +++ b/cmd/action/migrate.go @@ -43,7 +43,7 @@ var ( Use: "migrate", Short: "Manage versioned migration files", Long: "'atlas migrate' wraps several sub-commands for migration management.", - PersistentPreRunE: func(*cobra.Command, []string) error { + PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { // Migrate commands will not run on a broken migration directory, unless the force flag is given. if !MigrateFlags.Force { dir, err := dir() @@ -52,7 +52,7 @@ var ( } if err := migrate.Validate(dir); err != nil { fmt.Fprintf( - os.Stderr, + cmd.ErrOrStderr(), "Error: %s\n\nYou have a checksum error in your migration directory.\n"+ "This happens if you manually create or edit a migration file.\n"+ "Please check your migration files and run\n\n"+ diff --git a/cmd/action/migrate_test.go b/cmd/action/migrate_test.go index ba50be435ad..b11b09dcd26 100644 --- a/cmd/action/migrate_test.go +++ b/cmd/action/migrate_test.go @@ -7,6 +7,7 @@ package action import ( "fmt" "os" + "os/exec" "path/filepath" "strings" "testing" @@ -15,9 +16,12 @@ import ( "github.com/stretchr/testify/require" ) -func TestMigrate_Diff(t *testing.T) { +func TestMigrate(t *testing.T) { _, err := runCmd(RootCmd, "migrate") require.NoError(t, err) +} + +func TestMigrate_Diff(t *testing.T) { p := t.TempDir() // Expect no clean dev error. @@ -45,6 +49,28 @@ func TestMigrate_Diff(t *testing.T) { require.FileExists(t, filepath.Join(p, "atlas.sum")) } +func TestMigrate_Validate(t *testing.T) { + s, err := runCmd(RootCmd, "migrate", "validate", "--dir", "file://migrations") + require.Zero(t, s) + require.NoError(t, err) +} + +func TestMigrate_ValidateError(t *testing.T) { + if os.Getenv("DO_VALIDATE") == "1" { + runCmd(RootCmd, "migrate", "validate", "--dir", "file://migrations") + return + } + require.NoError(t, os.WriteFile("migrations/new.sql", []byte("contents"), 0600)) + defer os.Remove("migrations/new.sql") + cmd := exec.Command(os.Args[0], "-test.run=TestMigrate_ValidateError") //nolint:gosec + cmd.Env = append(os.Environ(), "DO_VALIDATE=1") + err := cmd.Run() + if err, ok := err.(*exec.ExitError); ok && !err.Success() { + return + } + t.Fatalf("process ran with err %v, want exist status 1", err) +} + const hcl = ` schema "main" { } diff --git a/cmd/action/migrations/20220318104614_initial.sql b/cmd/action/migrations/20220318104614_initial.sql new file mode 100644 index 00000000000..ff83d3904c1 --- /dev/null +++ b/cmd/action/migrations/20220318104614_initial.sql @@ -0,0 +1,4 @@ +-- add new schema named "atlantis" +CREATE DATABASE `atlantis`; +-- create "tbl" table +CREATE TABLE `atlantis`.`tbl` (`col` int NOT NULL) CHARSET utf8mb4 COLLATE utf8mb4_general_ci; diff --git a/cmd/action/migrations/atlas.sum b/cmd/action/migrations/atlas.sum new file mode 100644 index 00000000000..c8d3cb5fc3a --- /dev/null +++ b/cmd/action/migrations/atlas.sum @@ -0,0 +1,2 @@ +sum h1:CZFyP06wsoDt78We3f3kGuRqhMnVBe/8mHINP2hE4ik= +20220318104614_initial.sql h1:X9aVoWzuxCUrcS3tWoxPBUEh5aDeCwAelvo73hca1Ys=