|
| 1 | +package themes |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "testing/fstest" |
| 9 | +) |
| 10 | + |
| 11 | +func TestNew_WithEmbeddedFS(t *testing.T) { |
| 12 | + // Create a mock embedded FS with a themes directory |
| 13 | + mockFS := fstest.MapFS{ |
| 14 | + "themes/presidium-styling-base/config.yml": &fstest.MapFile{Data: []byte("name: test")}, |
| 15 | + } |
| 16 | + SetFS(mockFS) |
| 17 | + defer SetFS(nil) |
| 18 | + |
| 19 | + svc, err := New() |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("New() returned unexpected error: %v", err) |
| 22 | + } |
| 23 | + if svc.themes == nil { |
| 24 | + t.Fatal("New() returned service with nil themes FS") |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestNew_WithNilFS_NoModuleRoot(t *testing.T) { |
| 29 | + // Ensure themesFS is nil |
| 30 | + originalFS := GetThemesFS() |
| 31 | + SetFS(nil) |
| 32 | + defer func() { |
| 33 | + if originalFS != nil { |
| 34 | + SetFS(originalFS) |
| 35 | + } |
| 36 | + }() |
| 37 | + |
| 38 | + // Change to a temp dir with no go.mod above it |
| 39 | + origDir, _ := os.Getwd() |
| 40 | + tmpDir := t.TempDir() |
| 41 | + if err := os.Chdir(tmpDir); err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + defer func() { _ = os.Chdir(origDir) }() |
| 45 | + |
| 46 | + _, err := New() |
| 47 | + if err == nil { |
| 48 | + t.Fatal("New() should return error when no go.mod found and no embedded FS") |
| 49 | + } |
| 50 | + if !strings.Contains(err.Error(), "no go.mod found") { |
| 51 | + t.Errorf("expected 'no go.mod found' error, got: %v", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestExtract_WritesFilesAndProducesReplacements(t *testing.T) { |
| 56 | + // Create a mock FS with theme files including go.mod.tmpl and go.sum.tmpl |
| 57 | + mockFS := fstest.MapFS{ |
| 58 | + "presidium-styling-base/config.yml": &fstest.MapFile{Data: []byte("name: styling")}, |
| 59 | + "presidium-styling-base/go.mod.tmpl": &fstest.MapFile{Data: []byte("module github.com/spandigital/presidium-styling-base")}, |
| 60 | + "presidium-styling-base/go.sum.tmpl": &fstest.MapFile{Data: []byte("some-dep v1.0.0 h1:abc=")}, |
| 61 | + "presidium-layouts-base/config.yml": &fstest.MapFile{Data: []byte("name: layouts-base")}, |
| 62 | + "presidium-layouts-base/go.mod.tmpl": &fstest.MapFile{Data: []byte("module github.com/spandigital/presidium-layouts-base")}, |
| 63 | + "presidium-layouts-blog/config.yml": &fstest.MapFile{Data: []byte("name: layouts-blog")}, |
| 64 | + "presidium-layouts-blog/go.mod.tmpl": &fstest.MapFile{Data: []byte("module github.com/spandigital/presidium-layouts-blog")}, |
| 65 | + } |
| 66 | + |
| 67 | + svc := Service{themes: mockFS} |
| 68 | + |
| 69 | + tmpDir, replacements, err := svc.Extract() |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("Extract() returned unexpected error: %v", err) |
| 72 | + } |
| 73 | + defer os.RemoveAll(tmpDir) |
| 74 | + |
| 75 | + // Verify go.mod.tmpl was renamed to go.mod on disk |
| 76 | + goModPath := filepath.Join(tmpDir, "presidium-styling-base", "go.mod") |
| 77 | + if _, err := os.Stat(goModPath); os.IsNotExist(err) { |
| 78 | + t.Error("Expected go.mod.tmpl to be extracted as go.mod, but file does not exist") |
| 79 | + } |
| 80 | + |
| 81 | + // Verify go.sum.tmpl was renamed to go.sum on disk |
| 82 | + goSumPath := filepath.Join(tmpDir, "presidium-styling-base", "go.sum") |
| 83 | + if _, err := os.Stat(goSumPath); os.IsNotExist(err) { |
| 84 | + t.Error("Expected go.sum.tmpl to be extracted as go.sum, but file does not exist") |
| 85 | + } |
| 86 | + |
| 87 | + // Verify config.yml was extracted |
| 88 | + configPath := filepath.Join(tmpDir, "presidium-styling-base", "config.yml") |
| 89 | + if _, err := os.Stat(configPath); os.IsNotExist(err) { |
| 90 | + t.Error("Expected config.yml to be extracted, but file does not exist") |
| 91 | + } |
| 92 | + |
| 93 | + // Verify replacements string contains all three modules |
| 94 | + for _, mod := range []string{ |
| 95 | + "github.com/spandigital/presidium-styling-base", |
| 96 | + "github.com/spandigital/presidium-layouts-base", |
| 97 | + "github.com/spandigital/presidium-layouts-blog", |
| 98 | + } { |
| 99 | + if !strings.Contains(replacements, mod) { |
| 100 | + t.Errorf("Expected replacements to contain %q, got: %s", mod, replacements) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // Verify replacements are in sorted order (deterministic) |
| 105 | + parts := strings.Split(replacements, ",") |
| 106 | + if len(parts) != 3 { |
| 107 | + t.Fatalf("Expected 3 replacement pairs, got %d: %s", len(parts), replacements) |
| 108 | + } |
| 109 | + for i := 1; i < len(parts); i++ { |
| 110 | + if parts[i-1] > parts[i] { |
| 111 | + t.Errorf("Replacement pairs are not sorted: %q > %q", parts[i-1], parts[i]) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestExtract_EmptyTheme(t *testing.T) { |
| 117 | + // Test with a theme that has no files (just a directory entry) |
| 118 | + mockFS := fstest.MapFS{ |
| 119 | + "presidium-styling-base/config.yml": &fstest.MapFile{Data: []byte("name: test")}, |
| 120 | + "presidium-layouts-base/config.yml": &fstest.MapFile{Data: []byte("name: test")}, |
| 121 | + "presidium-layouts-blog/config.yml": &fstest.MapFile{Data: []byte("name: test")}, |
| 122 | + } |
| 123 | + |
| 124 | + svc := Service{themes: mockFS} |
| 125 | + |
| 126 | + tmpDir, replacements, err := svc.Extract() |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("Extract() returned unexpected error: %v", err) |
| 129 | + } |
| 130 | + defer os.RemoveAll(tmpDir) |
| 131 | + |
| 132 | + if replacements == "" { |
| 133 | + t.Error("Expected non-empty replacements string") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestFindModuleRoot(t *testing.T) { |
| 138 | + // Save and restore working directory |
| 139 | + origDir, _ := os.Getwd() |
| 140 | + defer func() { _ = os.Chdir(origDir) }() |
| 141 | + |
| 142 | + // Create a temp directory structure with go.mod |
| 143 | + tmpDir := t.TempDir() |
| 144 | + subDir := filepath.Join(tmpDir, "a", "b", "c") |
| 145 | + if err := os.MkdirAll(subDir, 0755); err != nil { |
| 146 | + t.Fatal(err) |
| 147 | + } |
| 148 | + if err := os.WriteFile(filepath.Join(tmpDir, "go.mod"), []byte("module test"), 0644); err != nil { |
| 149 | + t.Fatal(err) |
| 150 | + } |
| 151 | + |
| 152 | + // Change to the deeply nested directory |
| 153 | + if err := os.Chdir(subDir); err != nil { |
| 154 | + t.Fatal(err) |
| 155 | + } |
| 156 | + |
| 157 | + root, err := findModuleRoot() |
| 158 | + if err != nil { |
| 159 | + t.Fatalf("findModuleRoot() returned unexpected error: %v", err) |
| 160 | + } |
| 161 | + // Resolve symlinks for comparison (macOS /var -> /private/var) |
| 162 | + wantResolved, _ := filepath.EvalSymlinks(tmpDir) |
| 163 | + gotResolved, _ := filepath.EvalSymlinks(root) |
| 164 | + if gotResolved != wantResolved { |
| 165 | + t.Errorf("findModuleRoot() = %q, want %q", gotResolved, wantResolved) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestFindModuleRoot_NotFound(t *testing.T) { |
| 170 | + origDir, _ := os.Getwd() |
| 171 | + defer func() { _ = os.Chdir(origDir) }() |
| 172 | + |
| 173 | + // Use a temp directory with no go.mod anywhere above |
| 174 | + tmpDir := t.TempDir() |
| 175 | + if err := os.Chdir(tmpDir); err != nil { |
| 176 | + t.Fatal(err) |
| 177 | + } |
| 178 | + |
| 179 | + _, err := findModuleRoot() |
| 180 | + if err == nil { |
| 181 | + t.Fatal("findModuleRoot() should return error when no go.mod found") |
| 182 | + } |
| 183 | + if !strings.Contains(err.Error(), "no go.mod found") { |
| 184 | + t.Errorf("expected 'no go.mod found' error, got: %v", err) |
| 185 | + } |
| 186 | +} |
0 commit comments