Repository navigation
Expand file tree
/
Copy pathmain_decision_test.go
More file actions
123 lines (111 loc) · 3.7 KB
/
Copy pathmain_decision_test.go
File metadata and controls
123 lines (111 loc) · 3.7 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
package server
import (
"os"
"path/filepath"
"testing"
)
func mkModule(t *testing.T, root string) {
t.Helper()
if err := os.WriteFile(filepath.Join(root, "go.mod"), []byte("module example.com/app\n\ngo 1.25\n"), 0o644); err != nil {
t.Fatal(err)
}
}
func mkRoutes(t *testing.T, root string) {
t.Helper()
dir := filepath.Join(root, "config")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "routes.go"),
[]byte("package config\nimport \"webtyp.com/router\"\nfunc Register(r router.Router) {}\n"), 0o644); err != nil {
t.Fatal(err)
}
}
func mkUserMain(t *testing.T, root string) {
t.Helper()
dir := filepath.Join(root, "web", "server")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "main.go"), []byte("package main\nfunc main() {}\n"), 0o644); err != nil {
t.Fatal(err)
}
}
func newDecisionHandler(root string) *ServerHandler {
h := New()
h.SetAppRootDir(root)
h.SetSourceDir("web/server")
h.SetMainInputFile("main.go")
h.SetHTTPS(false)
return h
}
// Test 2/4 — strategy selection is decided by config/routes.go, with an existing
// handwritten web/server/main.go as the escape hatch that wins and is never generated over.
func TestServerMainSelection(t *testing.T) {
t.Run("no routes, no user main -> internal, nothing generated", func(t *testing.T) {
root := t.TempDir()
mkModule(t, root)
h := newDecisionHandler(root)
if h.needsExternalProcess() {
t.Error("needsExternalProcess = true with no routes and no user main")
}
if err := h.ensureServerMain(false); err != nil {
t.Fatalf("ensureServerMain: %v", err)
}
if _, err := os.Stat(filepath.Join(root, GeneratedMainDir, GeneratedMainFilename)); err == nil {
t.Error("a main was generated for a project without routes")
}
})
t.Run("routes present -> external via generated main", func(t *testing.T) {
root := t.TempDir()
mkModule(t, root)
mkRoutes(t, root)
h := newDecisionHandler(root)
if !h.usesGeneratedMain() {
t.Fatal("usesGeneratedMain = false with config/routes.go present")
}
if !h.needsExternalProcess() {
t.Fatal("needsExternalProcess = false with config/routes.go present")
}
if got, want := h.serverMainRelPath(), filepath.Join(GeneratedMainDir, GeneratedMainFilename); got != want {
t.Errorf("serverMainRelPath = %q, want %q", got, want)
}
var asked []MainConfig
h.SetMainGenerator(func(rootDir string, cfg MainConfig) error {
asked = append(asked, cfg)
return nil
})
if err := h.ensureServerMain(false); err != nil {
t.Fatalf("ensureServerMain: %v", err)
}
if len(asked) != 1 {
t.Errorf("want the connected generator asked once, got %+v", asked)
}
})
t.Run("handwritten web/server/main.go is the escape hatch and wins", func(t *testing.T) {
root := t.TempDir()
mkModule(t, root)
mkRoutes(t, root) // even with routes present...
mkUserMain(t, root)
h := newDecisionHandler(root)
if !h.hasHandWrittenMain() {
t.Fatal("hasHandWrittenMain = false with handwritten web/server/main.go present")
}
if h.usesGeneratedMain() {
t.Error("usesGeneratedMain = true although handwritten web/server/main.go exists")
}
if got, want := h.serverMainRelPath(), filepath.Join("web", "server", "main.go"); got != want {
t.Errorf("serverMainRelPath = %q, want %q", got, want)
}
const before = "package main\nfunc main() {}\n"
for _, force := range []bool{false, true} {
if err := h.ensureServerMain(force); err != nil {
t.Fatalf("ensureServerMain(%v): %v", force, err)
}
}
got, _ := os.ReadFile(filepath.Join(root, "web", "server", "main.go"))
if string(got) != before {
t.Errorf("web/server/main.go was modified:\n%s", got)
}
})
}