-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerated_artifacts_test.go
More file actions
311 lines (283 loc) · 9.94 KB
/
Copy pathgenerated_artifacts_test.go
File metadata and controls
311 lines (283 loc) · 9.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestRefuseGeneratedArtifact(t *testing.T) {
for _, tc := range []struct {
path string
refused bool
}{
{"db/schema.rb", true},
{"./db/schema.rb", true},
{"/db/schema.rb", true},
{"db/structure.sql", true},
{"Gemfile.lock", true},
{"db/migrate/20260811080000_add_shipping.rb", false},
{"app/models/order.rb", false},
{"config/routes.rb", false},
{"Gemfile", false},
{"docs/db/schema.rb", false}, // only the real one, not any lookalike
} {
got := refuseGeneratedArtifact(tc.path)
if tc.refused && got == "" {
t.Errorf("%s should be refused: hand-edits corrupt it and are discarded on regeneration", tc.path)
}
if !tc.refused && got != "" {
t.Errorf("%s should be writable, got refusal: %s", tc.path, got)
}
}
}
func TestRefusalNamesTheAlternative(t *testing.T) {
why := refuseGeneratedArtifact("db/schema.rb")
if why == "" {
t.Fatal("expected a refusal")
}
// A refusal that does not say what to do instead just stalls the build.
if !contains(why, "migration") {
t.Errorf("refusal must point at migrations, got: %s", why)
}
}
func contains(s, sub string) bool { return len(s) >= len(sub) && (stringIndex(s, sub) >= 0) }
func stringIndex(s, sub string) int {
for i := 0; i+len(sub) <= len(s); i++ {
if s[i:i+len(sub)] == sub {
return i
}
}
return -1
}
func TestSchemaVersionFromContent(t *testing.T) {
cases := []struct {
name string
body string
want int64
wantOK bool
}{
{
name: "underscored form",
body: "ActiveRecord::Schema[8.1].define(version: 2026_08_11_140000) do\nend\n",
want: 20260811140000,
wantOK: true,
},
{
name: "plain digits",
body: "ActiveRecord::Schema[7.2].define(version: 20260811140000) do\n",
want: 20260811140000,
wantOK: true,
},
{
name: "spaces around version",
body: "define( version: 2026_01_02_030405 )",
want: 20260102030405,
wantOK: true,
},
{
name: "absent",
body: "# no stamp here\nActiveRecord::Schema.define do\nend\n",
wantOK: false,
},
{
name: "empty file",
body: "",
wantOK: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, ok := schemaVersionFromContent(tc.body)
if ok != tc.wantOK {
t.Fatalf("ok=%v want %v (got %d)", ok, tc.wantOK, got)
}
if ok && got != tc.want {
t.Fatalf("version=%d want %d", got, tc.want)
}
})
}
}
func TestRefuseStaleMigration(t *testing.T) {
// Stamp 20260811140000, existing max 20260811135000 → floor 140000
const stamp = int64(20260811140000)
const existing = int64(20260811135000)
// at stamp → refuse, message names minimum
why := refuseStaleMigration(20260811140000, stamp, existing)
if why == "" {
t.Fatal("equal to stamp must be refused")
}
if !contains(why, "20260811140001") {
t.Errorf("refusal must state minimum acceptable timestamp, got: %s", why)
}
// below stamp → refuse
why = refuseStaleMigration(20260811130000, stamp, existing)
if why == "" || !contains(why, "20260811140001") {
t.Errorf("below stamp must refuse with min, got: %s", why)
}
// above stamp and existing → accept
if why := refuseStaleMigration(20260811140001, stamp, existing); why != "" {
t.Errorf("later timestamp must be accepted, got: %s", why)
}
if why := refuseStaleMigration(20260811150000, stamp, existing); why != "" {
t.Errorf("well later timestamp must be accepted, got: %s", why)
}
// existing higher than stamp sets the floor
why = refuseStaleMigration(20260811150000, stamp, 20260811160000)
if why == "" || !contains(why, "20260811160001") {
t.Errorf("must clear max existing, got: %s", why)
}
// no stamp, no existing → any positive version ok
if why := refuseStaleMigration(1, 0, 0); why != "" {
t.Errorf("no floor should accept, got: %s", why)
}
}
func TestMigrationVersionFromPath(t *testing.T) {
v, ok := migrationVersionFromPath("db/migrate/20260811130000_add_coffee_fields_to_storefront_products.rb")
if !ok || v != 20260811130000 {
t.Fatalf("got %d ok=%v", v, ok)
}
if _, ok := migrationVersionFromPath("app/models/x.rb"); ok {
t.Fatal("non-migration path")
}
if _, ok := migrationVersionFromPath("db/migrate/not_a_timestamp.rb"); ok {
t.Fatal("unversioned migrate file")
}
}
func TestRefuseRuntimeSchemaMutation(t *testing.T) {
call := "connection.add_column :storefront_products, :roast_level, :string\n"
for _, p := range []string{
"lib/foundation/demo_seeds.rb",
"app/models/product.rb",
"db/seeds.rb",
} {
why := refuseRuntimeSchemaMutation(p, call)
if why == "" {
t.Errorf("%s with add_column must be refused", p)
}
if !contains(why, "migration") {
t.Errorf("%s refusal must point at migrations: %s", p, why)
}
}
// allowed inside db/migrate/
if why := refuseRuntimeSchemaMutation("db/migrate/20260811150000_add_roast.rb", call); why != "" {
t.Errorf("migrate path must allow DDL, got: %s", why)
}
// comment is not a call
if why := refuseRuntimeSchemaMutation("lib/x.rb", "# connection.add_column :t, :c, :string\n"); why != "" {
t.Errorf("comment must not refuse: %s", why)
}
// string mention is not a call
if why := refuseRuntimeSchemaMutation("test/x_test.rb", "assert_includes body, \"add_column\"\n"); why != "" {
t.Errorf("string must not refuse: %s", why)
}
if why := refuseRuntimeSchemaMutation("test/x_test.rb", "expect(sql).to include('add_column')\n"); why != "" {
t.Errorf("single-quoted string must not refuse: %s", why)
}
// bare name without call syntax
if why := refuseRuntimeSchemaMutation("lib/x.rb", "methods = %w[add_column remove_column]\n"); why != "" {
t.Errorf("bare mention must not refuse: %s", why)
}
// each DDL helper is caught
for _, fn := range []string{
"add_column", "remove_column", "add_index", "remove_index",
"add_check_constraint", "add_reference", "change_column",
"create_table", "drop_table",
} {
body := fn + " :things, :x\n"
if why := refuseRuntimeSchemaMutation("lib/hack.rb", body); why == "" {
t.Errorf("%s call must be refused", fn)
}
}
// paren form
if why := refuseRuntimeSchemaMutation("app/x.rb", "create_table(:users) do |t|\nend\n"); why == "" {
t.Error("create_table( must be refused outside migrate")
}
}
func TestWritePathsMigrationTimestampGuard(t *testing.T) {
coder, ws := testCoder(t)
// Real schema stamp + one existing migration.
if err := os.MkdirAll(filepath.Join(ws, "db", "migrate"), 0o755); err != nil {
t.Fatal(err)
}
schema := "ActiveRecord::Schema[8.1].define(version: 2026_08_11_140000) do\nend\n"
if err := os.WriteFile(filepath.Join(ws, "db", "schema.rb"), []byte(schema), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(ws, "db", "migrate", "20260811120000_init.rb"), []byte("class Init < ActiveRecord::Migration[8.1]; end\n"), 0o644); err != nil {
t.Fatal(err)
}
// at/below stamp refused with minimum
out := coder.writeWorkspaceFile(
"db/migrate/20260811130000_add_coffee_fields_to_storefront_products.rb",
"class AddCoffee < ActiveRecord::Migration[8.1]\n def change\n add_column :storefront_products, :roast_level, :string\n end\nend\n",
)
if !strings.Contains(out, "write error:") || !strings.Contains(out, "20260811140001") {
t.Fatalf("stale migration must refuse with min timestamp, got: %s", out)
}
if _, err := os.Stat(filepath.Join(ws, "db", "migrate", "20260811130000_add_coffee_fields_to_storefront_products.rb")); err == nil {
t.Fatal("refused migration must not be written to disk")
}
// equal to stamp refused
out = coder.writeWorkspaceFile(
"db/migrate/20260811140000_noop.rb",
"class Noop < ActiveRecord::Migration[8.1]; def change; end; end\n",
)
if !strings.Contains(out, "20260811140001") {
t.Fatalf("equal stamp must refuse with min, got: %s", out)
}
// above both accepted; add_column inside migrate is fine
out = coder.writeWorkspaceFile(
"db/migrate/20260811150000_add_roast_level.rb",
"class AddRoastLevel < ActiveRecord::Migration[8.1]\n def change\n add_column :storefront_products, :roast_level, :string\n end\nend\n",
)
if !strings.Contains(out, "wrote ") {
t.Fatalf("valid migration must be accepted, got: %s", out)
}
}
func TestWritePathsRefuseRuntimeSchemaMutation(t *testing.T) {
coder, ws := testCoder(t)
call := "ActiveRecord::Base.connection.add_column :storefront_products, :roast_level, :string\n"
for _, p := range []string{"lib/foundation/demo_seeds.rb", "app/models/x.rb", "db/seeds.rb"} {
out := coder.writeWorkspaceFile(p, call)
if !strings.Contains(out, "write error:") || !strings.Contains(out, "migration") {
t.Fatalf("%s must refuse runtime DDL, got: %s", p, out)
}
if _, err := os.Stat(filepath.Join(ws, p)); err == nil {
t.Fatalf("%s must not be written", p)
}
}
// comment / string still writable
out := coder.writeWorkspaceFile("lib/ok.rb", "# add_column is only for migrations\ns = \"add_column\"\n")
if !strings.Contains(out, "wrote ") {
t.Fatalf("comment/string file must write, got: %s", out)
}
// ordinary non-DDL write still works
out = coder.writeWorkspaceFile("app/models/product.rb", "class Product < ApplicationRecord\nend\n")
if !strings.Contains(out, "wrote ") {
t.Fatalf("ordinary write must work, got: %s", out)
}
}
func TestApplyPatchRefusesRuntimeSchemaMutation(t *testing.T) {
coder, ws := testCoder(t)
path := "lib/foundation/demo_seeds.rb"
full := filepath.Join(ws, path)
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
t.Fatal(err)
}
initial := "module DemoSeeds\n def self.run\n puts :ok\n end\nend\n"
if err := os.WriteFile(full, []byte(initial), 0o644); err != nil {
t.Fatal(err)
}
out := coder.applyPatch(path, []patchOp{{
Op: "replace",
Find: "puts :ok",
Replace: "connection.add_column :storefront_products, :roast_level, :string",
}})
if !strings.Contains(out, "patch error:") || !strings.Contains(out, "migration") {
t.Fatalf("patch introducing DDL must refuse, got: %s", out)
}
got, _ := os.ReadFile(full)
if string(got) != initial {
t.Fatal("refused patch must leave file unchanged")
}
}