Debian Codegraph @

internal/librariangen/build/builder_test.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package build
16
17 import (
18 "context"
19 "errors"
20 "os"
21 "path/filepath"
22 "slices"
23 "testing"
24 )
25
26 // testEnv encapsulates a temporary test environment.
27 type testEnv struct {
28 tmpDir string
29 librarianDir string
30 repoDir string
31 }
32
33 func TestBuild(t *testing.T) {
34 singleAPIRequest := `{"id": "foo", "apis": [{"path": "api/v1"}]}`
35 tests := []struct {
36 name string
37 setup func(e *testEnv, t *testing.T)
38 buildErr error
39 testErr error
40 wantErr bool
41 wantExecvCount int
42 }{
43 {
44 name: "happy path",
45 setup: func(e *testEnv, t *testing.T) {
46 e.writeRequestFile(t, singleAPIRequest)
47 },
48 wantErr: false,
49 wantExecvCount: 2,
50 },
51 {
52 name: "missing request file",
53 wantErr: true,
54 },
55 {
56 name: "go build fails",
57 setup: func(e *testEnv, t *testing.T) {
58 e.writeRequestFile(t, singleAPIRequest)
59 },
60 buildErr: errors.New("build failed"),
61 wantErr: true,
62 wantExecvCount: 1,
63 },
64 {
65 name: "go test fails",
66 setup: func(e *testEnv, t *testing.T) {
67 e.writeRequestFile(t, singleAPIRequest)
68 },
69 testErr: errors.New("test failed"),
70 wantErr: true,
71 wantExecvCount: 2,
72 },
73 }
74
75 for _, tt := range tests {
76 t.Run(tt.name, func(t *testing.T) {
77 e := newTestEnv(t)
78 defer e.cleanup(t)
79
80 if tt.setup != nil {
81 tt.setup(e, t)
82 }
83
84 var execvCount int
85 execvRun = func(ctx context.Context, args []string, dir string) error {
86 execvCount++
87 want := filepath.Join(e.repoDir, "foo")
88 if dir != want {
89 t.Errorf("execv called with wrong working directory %s; want %s", dir, want)
90 }
91 switch {
92 case slices.Equal(args, []string{"go", "build", "./..."}):
93 return tt.buildErr
94 case slices.Equal(args, []string{"go", "test", "./...", "-short"}):
95 return tt.testErr
96 default:
97 t.Errorf("execv called with unexpected args %v", args)
98 return nil
99 }
100 }
101
102 cfg := &Config{
103 LibrarianDir: e.librarianDir,
104 RepoDir: e.repoDir,
105 }
106
107 if err := Build(context.Background(), cfg); (err != nil) != tt.wantErr {
108 t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)
109 }
110
111 if execvCount != tt.wantExecvCount {
112 t.Errorf("execv called = %v; want %v", execvCount, tt.wantExecvCount)
113 }
114 })
115 }
116 }
117
118 // cleanup removes the temporary directory.
119 func (e *testEnv) cleanup(t *testing.T) {
120 t.Helper()
121 if err := os.RemoveAll(e.tmpDir); err != nil {
122 t.Fatalf("failed to remove temp dir: %v", err)
123 }
124 }
125
126 // writeRequestFile writes a builf-request.json file.
127 func (e *testEnv) writeRequestFile(t *testing.T, content string) {
128 t.Helper()
129 p := filepath.Join(e.librarianDir, "build-request.json")
130 if err := os.WriteFile(p, []byte(content), 0644); err != nil {
131 t.Fatalf("failed to write request file: %v", err)
132 }
133 }
134
135 // newTestEnv creates a new test environment.
136 func newTestEnv(t *testing.T) *testEnv {
137 t.Helper()
138 tmpDir, err := os.MkdirTemp("", "builder-test")
139 if err != nil {
140 t.Fatalf("failed to create temp dir: %v", err)
141 }
142 e := &testEnv{tmpDir: tmpDir}
143 e.librarianDir = filepath.Join(tmpDir, "librarian")
144 e.repoDir = filepath.Join(tmpDir, "repo")
145 for _, dir := range []string{e.librarianDir, e.repoDir} {
146 if err := os.Mkdir(dir, 0755); err != nil {
147 t.Fatalf("failed to create dir %s: %v", dir, err)
148 }
149 }
150
151 return e
152 }