Skip to content

Commit

Permalink
e2e: fix problems with comments
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed May 19, 2024
1 parent 8a6aaf2 commit 9b8e73c
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 95 deletions.
3 changes: 2 additions & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func Test_2e2_postgres(t *testing.T) {
)

connection, _, _ := amigo.GetConnection(conn)
defer connection.Close()

t.Run("migration_with_change", func(t *testing.T) {
t.Parallel()
s := "migrations_with_change"

base := amigo.RunMigrationOptions{
Expand All @@ -62,6 +62,7 @@ func Test_2e2_postgres(t *testing.T) {
})

t.Run("migration_with_classic", func(t *testing.T) {
t.Parallel()
s := "migrations_with_classic"

base := amigo.RunMigrationOptions{
Expand Down
73 changes: 35 additions & 38 deletions pkg/utils/testutils/snapshoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,10 @@ func MaySnapshotSavePgDump(t TestingT, schemaName string, db schema.DatabaseCred

file := path.Join("testdata", t.Name()+"_"+id) + ".snap.sql"

args := []string{
"-d", db.DB,
"-h", db.Host,
"-U", db.User,
"-p", db.Port,
"-n", schemaName,
"-s",
"--no-comments",
"--no-owner",
"--no-privileges",
"--no-tablespaces",
"--no-security-labels",
"--file", file,
}

err := os.MkdirAll(path.Dir(file), 0755)
require.NoError(t, err)

env := map[string]string{"PGPASSWORD": db.Pass}

_, stderr, err := cmdexec.Exec(getPgDumpPath(), args, env)
if err != nil {
fmt.Println(stderr)
}
require.NoError(t, err)
execPgDump(t, schemaName, db, file)

return
}
Expand All @@ -154,6 +133,30 @@ func AssertSnapshotPgDumpDiff(t TestingT, schemaName string, db schema.DatabaseC
fileOut := path.Join("testdata", t.Name()+"_"+id) + ".out.sql"
fileSnap := path.Join("testdata", t.Name()+"_"+id) + ".snap.sql"

execPgDump(t, schemaName, db, fileOut)

snap, err := os.ReadFile(fileSnap)
require.NoError(t, err)

out, err := os.ReadFile(fileOut)
require.NoError(t, err)

if string(snap) != string(out) {
absOut, err := filepath.Abs(fileOut)
require.NoError(t, err)

absSnap, err := filepath.Abs(fileSnap)
require.NoError(t, err)

out, _, err := cmdexec.Exec("bash",
[]string{"-c", fmt.Sprintf("sdiff -l %s %s | cat -n | grep -v -e '($'", absSnap, absOut)}, nil)
require.NoError(t, err)

t.Errorf("snapshots are different between %s and %s:\n%s", fileSnap, fileOut, out)
}
}

func execPgDump(t TestingT, schemaName string, db schema.DatabaseCredentials, file string) {
args := []string{
"-d", db.DB,
"-h", db.Host,
Expand All @@ -166,7 +169,7 @@ func AssertSnapshotPgDumpDiff(t TestingT, schemaName string, db schema.DatabaseC
"--no-privileges",
"--no-tablespaces",
"--no-security-labels",
"-f", fileOut,
"--file", file,
}

env := map[string]string{"PGPASSWORD": db.Pass}
Expand All @@ -177,25 +180,19 @@ func AssertSnapshotPgDumpDiff(t TestingT, schemaName string, db schema.DatabaseC
}
require.NoError(t, err)

snap, err := os.ReadFile(fileSnap)
require.NoError(t, err)
// remove the 2 first lines
/*
-- Dumped from database version X
-- Dumped by pg_dump version X (Ubuntu 16.3-1.pgdg22.04+1)
*/

out, err := os.ReadFile(fileOut)
readFile, err := os.ReadFile(file)
require.NoError(t, err)

if string(snap) != string(out) {
absOut, err := filepath.Abs(fileOut)
require.NoError(t, err)
lines := strings.Split(string(readFile), "\n")
lines = lines[7:]

absSnap, err := filepath.Abs(fileSnap)
require.NoError(t, err)

out, _, err := cmdexec.Exec("bash",
[]string{"-c", fmt.Sprintf("sdiff -l %s %s | cat -n | grep -v -e '($'", absSnap, absOut)}, nil)
require.NoError(t, err)

t.Errorf("snapshots are different between %s and %s:\n%s", fileSnap, fileOut, out)
}
err = os.WriteFile(file, []byte(strings.Join(lines, "\n")), 0644)
}

func getPgDumpPath() string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 16.0
-- Dumped by pg_dump version 16.2

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
Expand Down

0 comments on commit 9b8e73c

Please sign in to comment.