|
| 1 | +package symlink |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "log/slog" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/a-h/templ/cmd/templ/generatecmd" |
| 12 | + "github.com/a-h/templ/cmd/templ/testproject" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSymlink(t *testing.T) { |
| 16 | + log := slog.New(slog.NewJSONHandler(io.Discard, nil)) |
| 17 | + t.Run("can generate if root is symlink", func(t *testing.T) { |
| 18 | + // templ generate -f templates.templ |
| 19 | + dir, err := testproject.Create("github.com/a-h/templ/cmd/templ/testproject") |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("failed to create test project: %v", err) |
| 22 | + } |
| 23 | + defer os.RemoveAll(dir) |
| 24 | + |
| 25 | + symlinkPath := dir + "-symlink" |
| 26 | + err = os.Symlink(dir, symlinkPath) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("failed to create dir symlink: %v", err) |
| 29 | + } |
| 30 | + defer os.Remove(symlinkPath) |
| 31 | + |
| 32 | + // Delete the templates_templ.go file to ensure it is generated. |
| 33 | + err = os.Remove(path.Join(symlinkPath, "templates_templ.go")) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("failed to remove templates_templ.go: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + // Run the generate command. |
| 39 | + err = generatecmd.Run(context.Background(), log, generatecmd.Arguments{ |
| 40 | + Path: symlinkPath, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("failed to run generate command: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + // Check the templates_templ.go file was created. |
| 47 | + _, err = os.Stat(path.Join(symlinkPath, "templates_templ.go")) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("templates_templ.go was not created: %v", err) |
| 50 | + } |
| 51 | + }) |
| 52 | +} |
0 commit comments