Skip to content

Commit

Permalink
cmd/action: tests for atlas migrate validate command (#661)
Browse files Browse the repository at this point in the history
* cmd/action: tests for atlas migrate validate command

* cmd/action: linter
  • Loading branch information
masseelch authored Mar 18, 2022
1 parent 08ea585 commit c761b18
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cmd/action/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"+
Expand Down
28 changes: 27 additions & 1 deletion cmd/action/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package action
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand All @@ -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.
Expand Down Expand Up @@ -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" {
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/action/migrations/20220318104614_initial.sql
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions cmd/action/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sum h1:CZFyP06wsoDt78We3f3kGuRqhMnVBe/8mHINP2hE4ik=
20220318104614_initial.sql h1:X9aVoWzuxCUrcS3tWoxPBUEh5aDeCwAelvo73hca1Ys=

0 comments on commit c761b18

Please sign in to comment.