@@ -4,67 +4,197 @@ import (
44 "bytes"
55 "io"
66 "os"
7- "path/filepath"
87 "runtime"
98 "testing"
109
1110 "github.com/hexops/autogold"
12- "github.com/stretchr/testify/assert"
1311 "github.com/stretchr/testify/require"
1412)
1513
16- func TestPartialCopyWithoutExtensions (t * testing.T ) {
17- if runtime .GOOS == "windows" {
18- t .Skip ("Test doesn't work on Windows of weirdness with t.TempDir() handling" )
19- }
20-
21- // Create test data - there is no stdlib in-memory io.ReadSeeker implementation
22- src , err := os .Create (filepath .Join (t .TempDir (), t .Name ()))
14+ // createTestFile creates a temporary file with the given content for testing
15+ func createTestFile (t * testing.T , content string ) * os.File {
16+ src , err := os .CreateTemp (t .TempDir (), "test-*.sql" )
2317 require .NoError (t , err )
24- _ , err = src .WriteString (`-- Some comment
25-
26- CREATE EXTENSION foobar
27-
28- COMMENT ON EXTENSION barbaz
29-
30- CREATE TYPE asdf
31-
32- CREATE TABLE robert (
33- ...
34- )
35-
36- CREATE TABLE bobhead (
37- ...
38- )` )
18+ _ , err = src .WriteString (content )
3919 require .NoError (t , err )
4020 _ , err = src .Seek (0 , io .SeekStart )
4121 require .NoError (t , err )
22+ return src
23+ }
4224
43- // Set up target to assert against
44- var dst bytes.Buffer
45-
46- // Perform partial copy
47- _ , err = PartialCopyWithoutExtensions (& dst , src , func (i int64 ) {})
48- assert .NoError (t , err )
49-
50- // Copy rest of contents
51- _ , err = io .Copy (& dst , src )
52- assert .NoError (t , err )
53-
54- // Assert contents (update with -update)
55- autogold .Want ("partial-copy-without-extensions" , `-- Some comment
25+ func TestCommentOutInvalidLines (t * testing.T ) {
26+ if runtime .GOOS == "windows" {
27+ t .Skip ("Test doesn't work on Windows of weirdness with t.TempDir() handling" )
28+ }
5629
57- CREATE EXTENSION foobar
30+ tests := []struct {
31+ name string
32+ input string
33+ want string
34+ }{
35+
36+ {
37+ name : "EOF - input file doesn't contain any filterEndMarkers, or linesToFilter" ,
38+ input : `
39+ --
40+ -- PostgreSQL database dump
41+ --
42+ ` ,
43+ want : `
44+ --
45+ -- PostgreSQL database dump
46+ --
47+ ` ,
48+ },
49+ {
50+ name : "EOF - input file doesn't contain any filterEndMarkers, but does contain linesToFilter" ,
51+ input : `
52+ DROP DATABASE pgsql;
53+ ` ,
54+ want : `
55+ -- DROP DATABASE pgsql;
56+ ` ,
57+ },
58+ {
59+ name : "Customer-realistic dump, with extensions and schemas" ,
60+ input : `
61+ --
62+ -- PostgreSQL database dump
63+ --
64+
65+ \restrict 1e9XN4yltwkS6RMoyhkFC6hmzrkbz4fZIVvJSYP3h5B1Qvii1WnhlslcPAzK8Tb
66+
67+ -- Dumped from database version 12.22
68+ -- Dumped by pg_dump version 14.19 (Homebrew)
69+
70+ -- Started on 2025-10-29 20:49:56 IST
71+
72+ SET transaction_timeout = 10;
73+ SET statement_timeout = 0;
74+ SET lock_timeout = 0;
75+ SET idle_in_transaction_session_timeout = 0;
76+ SET client_encoding = 'UTF8';
77+ SET standard_conforming_strings = on;
78+ SELECT pg_catalog.set_config('search_path', '', false);
79+ SET check_function_bodies = false;
80+ SET xmloption = content;
81+ SET client_min_messages = warning;
82+ SET row_security = off;
83+
84+ DROP DATABASE pgsql IF EXISTS;
85+ CREATE DATABASE pgsql;
86+ COMMENT ON DATABASE pgsql IS 'database';
87+
88+ \connect pgsql
89+
90+ DROP SCHEMA public IF EXISTS;
91+ CREATE SCHEMA public;
92+ COMMENT ON SCHEMA public IS 'schema';
93+
94+ DROP EXTENSION IF EXISTS pg_stat_statements;
95+ CREATE EXTENSION pg_stat_statements;
96+ COMMENT ON EXTENSION pg_stat_statements IS 'extension';
97+
98+ ALTER TABLE IF EXISTS ONLY "public"."webhooks" DROP CONSTRAINT IF EXISTS "webhooks_updated_by_user_id_fkey";
99+ DROP TRIGGER IF EXISTS "versions_insert" ON "public"."versions";
100+ DROP INDEX IF EXISTS "public"."webhook_logs_status_code_idx";
101+ DROP SEQUENCE IF EXISTS "public"."webhooks_id_seq";
102+ DROP TABLE IF EXISTS "public"."webhooks";
103+ SET default_tablespace = '';
104+ SET default_table_access_method = "heap";
105+
106+ CREATE TABLE "public"."access_tokens" (
107+ "id" bigint NOT NULL,
108+ "subject_user_id" integer NOT NULL,
109+ "value_sha256" "bytea" NOT NULL,
110+ "note" "text" NOT NULL,
111+ "created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
112+ "last_used_at" timestamp with time zone,
113+ "deleted_at" timestamp with time zone,
114+ "creator_user_id" integer NOT NULL,
115+ "scopes" "text"[] NOT NULL,
116+ "internal" boolean DEFAULT false
117+ );
118+
119+ \unrestrict 1e9XN4yltwkS6RMoyhkFC6hmzrkbz4fZIVvJSYP3h5B1Qvii1WnhlslcPAzK8Tb
120+ ` ,
121+ want : `
122+ --
123+ -- PostgreSQL database dump
124+ --
125+
126+ \restrict 1e9XN4yltwkS6RMoyhkFC6hmzrkbz4fZIVvJSYP3h5B1Qvii1WnhlslcPAzK8Tb
127+
128+ -- Dumped from database version 12.22
129+ -- Dumped by pg_dump version 14.19 (Homebrew)
130+
131+ -- Started on 2025-10-29 20:49:56 IST
132+
133+ -- SET transaction_timeout = 10;
134+ SET statement_timeout = 0;
135+ SET lock_timeout = 0;
136+ SET idle_in_transaction_session_timeout = 0;
137+ SET client_encoding = 'UTF8';
138+ SET standard_conforming_strings = on;
139+ SELECT pg_catalog.set_config('search_path', '', false);
140+ SET check_function_bodies = false;
141+ SET xmloption = content;
142+ SET client_min_messages = warning;
143+ SET row_security = off;
144+
145+ -- DROP DATABASE pgsql IF EXISTS;
146+ -- CREATE DATABASE pgsql;
147+ -- COMMENT ON DATABASE pgsql IS 'database';
148+
149+ -- \connect pgsql
150+
151+ -- DROP SCHEMA public IF EXISTS;
152+ -- CREATE SCHEMA public;
153+ -- COMMENT ON SCHEMA public IS 'schema';
154+
155+ -- DROP EXTENSION IF EXISTS pg_stat_statements;
156+ -- CREATE EXTENSION pg_stat_statements;
157+ -- COMMENT ON EXTENSION pg_stat_statements IS 'extension';
158+
159+ ALTER TABLE IF EXISTS ONLY "public"."webhooks" DROP CONSTRAINT IF EXISTS "webhooks_updated_by_user_id_fkey";
160+ DROP TRIGGER IF EXISTS "versions_insert" ON "public"."versions";
161+ DROP INDEX IF EXISTS "public"."webhook_logs_status_code_idx";
162+ DROP SEQUENCE IF EXISTS "public"."webhooks_id_seq";
163+ DROP TABLE IF EXISTS "public"."webhooks";
164+ SET default_tablespace = '';
165+ SET default_table_access_method = "heap";
166+
167+ CREATE TABLE "public"."access_tokens" (
168+ "id" bigint NOT NULL,
169+ "subject_user_id" integer NOT NULL,
170+ "value_sha256" "bytea" NOT NULL,
171+ "note" "text" NOT NULL,
172+ "created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
173+ "last_used_at" timestamp with time zone,
174+ "deleted_at" timestamp with time zone,
175+ "creator_user_id" integer NOT NULL,
176+ "scopes" "text"[] NOT NULL,
177+ "internal" boolean DEFAULT false
178+ );
179+
180+ \unrestrict 1e9XN4yltwkS6RMoyhkFC6hmzrkbz4fZIVvJSYP3h5B1Qvii1WnhlslcPAzK8Tb
181+ ` ,
182+ },
183+ }
58184
59- -- COMMENT ON EXTENSION barbaz
185+ for _ , tt := range tests {
186+ t .Run (tt .name , func (t * testing.T ) {
187+ src := createTestFile (t , tt .input )
188+ var dst bytes.Buffer
60189
61- CREATE TYPE asdf
190+ _ , err := CommentOutInvalidLines (& dst , src , func (i int64 ) {})
191+ require .NoError (t , err )
62192
63- CREATE TABLE robert (
64- ...
65- )
193+ // Copy rest of contents
194+ _ , err = io . Copy ( & dst , src )
195+ require . NoError ( t , err )
66196
67- CREATE TABLE bobhead (
68- ...
69- )` ). Equal ( t , dst . String ())
197+ autogold . Want ( tt . name , tt . want ). Equal ( t , dst . String ())
198+ })
199+ }
70200}
0 commit comments