From 9b8e73cac83abebb3fd0c5aaa4fafd802a0d2a81 Mon Sep 17 00:00:00 2001 From: Alexis Viscogliosi Date: Sun, 19 May 2024 10:30:13 +0200 Subject: [PATCH] e2e: fix problems with comments --- e2e_test.go | 3 +- pkg/utils/testutils/snapshoting.go | 73 +++++++++---------- .../20240517080505_schema_version.snap.sql | 7 -- .../20240518071740_create_user.snap.sql | 7 -- ...240518071842_add_index_user_email.snap.sql | 7 -- .../20240518071938_custom_seed.snap.sql | 7 -- .../20240517080505_schema_version.snap.sql | 7 -- .../20240518071740_create_user.snap.sql | 7 -- ...240518071842_add_index_user_email.snap.sql | 7 -- .../20240518071938_custom_seed.snap.sql | 7 -- 10 files changed, 37 insertions(+), 95 deletions(-) diff --git a/e2e_test.go b/e2e_test.go index e0b3dbf..d9cba03 100644 --- a/e2e_test.go +++ b/e2e_test.go @@ -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{ @@ -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{ diff --git a/pkg/utils/testutils/snapshoting.go b/pkg/utils/testutils/snapshoting.go index 2101907..b0feecf 100644 --- a/pkg/utils/testutils/snapshoting.go +++ b/pkg/utils/testutils/snapshoting.go @@ -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 } @@ -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, @@ -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} @@ -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 { diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql index 654489a..2b85935 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240517080505_schema_version.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql index 0614531..2c4863d 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071740_create_user.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql index 9246866..9351e07 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071842_add_index_user_email.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql index 9246866..9351e07 100644 --- a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240518071938_custom_seed.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql index f23210b..90b17f2 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240517080505_schema_version.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql index bfaf1d2..acb143c 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071740_create_user.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql index 0688349..6d36193 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071842_add_index_user_email.snap.sql @@ -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; diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql index 0688349..6d36193 100644 --- a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240518071938_custom_seed.snap.sql @@ -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;