|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestMergeManagedSTIgnoreBlock(t *testing.T) { |
| 11 | + dir := t.TempDir() |
| 12 | + path := filepath.Join(dir, ".stignore") |
| 13 | + |
| 14 | + // Creating the block appends after existing user content. |
| 15 | + if err := os.WriteFile(path, []byte("node_modules\n// user comment\n"), 0o644); err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
| 18 | + retained, err := mergeManagedSTIgnoreBlock(dir, []string{"/results"}) |
| 19 | + if err != nil || len(retained) != 0 { |
| 20 | + t.Fatalf("merge: retained=%v err=%v", retained, err) |
| 21 | + } |
| 22 | + content, _ := os.ReadFile(path) |
| 23 | + text := string(content) |
| 24 | + if !strings.Contains(text, "node_modules\n") || !strings.Contains(text, "// user comment") { |
| 25 | + t.Fatalf("user content must be preserved, got %q", text) |
| 26 | + } |
| 27 | + if !strings.Contains(text, managedSTIgnoreBegin+"\n/results\n"+managedSTIgnoreEnd) { |
| 28 | + t.Fatalf("expected managed block with /results, got %q", text) |
| 29 | + } |
| 30 | + |
| 31 | + // Idempotent: same input, same output. |
| 32 | + if _, err := mergeManagedSTIgnoreBlock(dir, []string{"/results"}); err != nil { |
| 33 | + t.Fatal(err) |
| 34 | + } |
| 35 | + again, _ := os.ReadFile(path) |
| 36 | + if string(again) != text { |
| 37 | + t.Fatalf("merge must be idempotent:\n%q\nvs\n%q", text, again) |
| 38 | + } |
| 39 | + |
| 40 | + // Growing the active set keeps sorted patterns inside the block. |
| 41 | + if _, err := mergeManagedSTIgnoreBlock(dir, []string{"/datasets", "/results"}); err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + active, tombs := readManagedSTIgnoreBlock(dir) |
| 45 | + if strings.Join(active, ",") != "/datasets,/results" || len(tombs) != 0 { |
| 46 | + t.Fatalf("unexpected block state: active=%v tombs=%v", active, tombs) |
| 47 | + } |
| 48 | + |
| 49 | + // Dropping a mapping tombstones its pattern instead of releasing the |
| 50 | + // subtree into the primary folder. |
| 51 | + retained, err = mergeManagedSTIgnoreBlock(dir, []string{"/datasets"}) |
| 52 | + if err != nil { |
| 53 | + t.Fatal(err) |
| 54 | + } |
| 55 | + if len(retained) != 1 || retained[0] != "/results" { |
| 56 | + t.Fatalf("expected /results newly retained, got %v", retained) |
| 57 | + } |
| 58 | + active, tombs = readManagedSTIgnoreBlock(dir) |
| 59 | + if strings.Join(active, ",") != "/datasets" || strings.Join(tombs, ",") != "/results" { |
| 60 | + t.Fatalf("unexpected block state: active=%v tombs=%v", active, tombs) |
| 61 | + } |
| 62 | + content, _ = os.ReadFile(path) |
| 63 | + if !strings.Contains(string(content), managedSTIgnoreTombstone+"\n/results\n") { |
| 64 | + t.Fatalf("tombstone comment must precede the retained pattern, got %q", content) |
| 65 | + } |
| 66 | + |
| 67 | + // A tombstoned pattern re-added to the config becomes active again and |
| 68 | + // is reported as retained exactly once. |
| 69 | + retained, err = mergeManagedSTIgnoreBlock(dir, []string{"/datasets", "/results"}) |
| 70 | + if err != nil || len(retained) != 0 { |
| 71 | + t.Fatalf("re-adding must not re-report retention: retained=%v err=%v", retained, err) |
| 72 | + } |
| 73 | + active, tombs = readManagedSTIgnoreBlock(dir) |
| 74 | + if strings.Join(active, ",") != "/datasets,/results" || len(tombs) != 0 { |
| 75 | + t.Fatalf("re-added pattern must leave tombstones, got active=%v tombs=%v", active, tombs) |
| 76 | + } |
| 77 | + |
| 78 | + // User content after the block is preserved across rewrites. |
| 79 | + if err := os.WriteFile(path, []byte(string(content)+"trailing-user-pattern\n"), 0o644); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if _, err := mergeManagedSTIgnoreBlock(dir, []string{"/datasets"}); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + final, _ := os.ReadFile(path) |
| 86 | + if !strings.Contains(string(final), "trailing-user-pattern") { |
| 87 | + t.Fatalf("content after block must survive, got %q", final) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestMergeManagedSTIgnoreBlockNoopWithoutFileOrPatterns(t *testing.T) { |
| 92 | + dir := t.TempDir() |
| 93 | + if retained, err := mergeManagedSTIgnoreBlock(dir, nil); err != nil || retained != nil { |
| 94 | + t.Fatalf("no file + no patterns must be a no-op, got %v %v", retained, err) |
| 95 | + } |
| 96 | + if _, err := os.Stat(filepath.Join(dir, ".stignore")); !os.IsNotExist(err) { |
| 97 | + t.Fatal("no-op must not create .stignore") |
| 98 | + } |
| 99 | +} |
0 commit comments