Repository navigation
Expand file tree
/
Copy pathintegration_test.go
More file actions
190 lines (186 loc) · 7.25 KB
/
Copy pathintegration_test.go
File metadata and controls
190 lines (186 loc) · 7.25 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* fathomry
* Copyright (C) 2026 Frost Leo
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package duckdb
import (
"context"
"debug/buildinfo"
"fmt"
"go/parser"
"go/token"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
func TestIndependentConsumers(t *testing.T) {
root, err := filepath.Abs("../../../..")
if err != nil {
t.Fatal(err)
}
sums, err := os.ReadFile(filepath.Join(root, "go.sum"))
if err != nil {
t.Fatal(err)
}
for _, variant := range []struct{ name, path, output string }{
{"direct", "testdata/consumer/main.go", "duckdb direct public consumer passed\n"},
{"framework", "testdata/framework/main.go", "duckdb Framework Fixed/Follow public consumer passed\n"},
} {
t.Run(variant.name, func(t *testing.T) {
fixture, err := os.ReadFile(variant.path)
if err != nil {
t.Fatal(err)
}
notice, _, _ := strings.Cut(string(fixture), "package main")
syntax, err := parser.ParseFile(token.NewFileSet(), "main.go", fixture, parser.ImportsOnly)
if err != nil {
t.Fatal(err)
}
for _, imported := range syntax.Imports {
path, err := strconv.Unquote(imported.Path.Value)
if err != nil || strings.Contains(path, "/internal/") || strings.Contains(path, "github.com/duckdb/") || variant.name == "direct" && strings.Contains(path, "/framework/") {
t.Fatal("consumer bypassed public boundary")
}
}
directory := consumerJob(t)
for name, content := range map[string][]byte{
"main.go": fixture,
"go.sum": sums,
"go.mod": []byte(fmt.Sprintf("module example.org/duckdb-%s-consumer\n\ngo 1.27.0\nrequire github.com/frost-leo/fathomry v0.0.0\nreplace github.com/frost-leo/fathomry => %q\n", variant.name, filepath.ToSlash(root))),
} {
if err := os.WriteFile(filepath.Join(directory, name), content, 0600); err != nil {
t.Fatal(err)
}
}
run := func(offline bool, args ...string) ([]byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
command := exec.CommandContext(ctx, "go", args...)
command.Dir = directory
command.Env = append(os.Environ(), "GOWORK=off", "GOTOOLCHAIN=local")
if offline {
command.Env = append(command.Env, "GOPROXY=off", "GOSUMDB=off")
}
output, err := command.CombinedOutput()
if ctx.Err() != nil {
t.Fatal("independent consumer build exceeded its bound")
}
return output, err
}
if output, err := run(false, "mod", "tidy"); err != nil {
t.Fatalf("consumer module preparation: %v\n%s", err, output)
}
binary := filepath.Join(directory, "consumer")
if output, err := run(true, "build", "-mod=readonly", "-race", "-o", binary, "."); err != nil {
t.Fatalf("consumer build: %v\n%s", err, output)
}
if output, err := run(true, "mod", "tidy", "-diff"); err != nil {
t.Fatalf("consumer graph unstable offline: %v\n%s", err, output)
}
output, err := run(true, "list", "-mod=readonly", "-deps", ".")
if err != nil {
t.Fatalf("consumer dependencies: %v\n%s", err, output)
}
for _, path := range strings.Fields(string(output)) {
if strings.HasPrefix(path, "github.com/frost-leo/fathomry/adapters/database/") ||
strings.HasPrefix(path, "github.com/frost-leo/fathomry/internal/sqlengine/trino/") ||
strings.HasPrefix(path, "github.com/frost-leo/fathomry/internal/sqlengine/doris/") ||
variant.name == "direct" && strings.HasPrefix(path, "github.com/frost-leo/fathomry/framework/") {
t.Fatalf("single-provider consumer acquired unrelated provider dependency: %s", path)
}
}
build, err := buildinfo.ReadFile(binary)
if err != nil {
t.Fatal(err)
}
found := false
for _, module := range build.Deps {
if module.Path == "github.com/duckdb/duckdb-go/v2" {
found = module.Version == "v2.10505.0" && module.Replace == nil && module.Sum != ""
}
}
if !found {
t.Fatal("consumer binary did not retain selected native SDK provenance")
}
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
command := exec.CommandContext(ctx, binary)
if output, err := command.CombinedOutput(); err != nil || string(output) != variant.output {
t.Fatalf("independent native consumer: %v\n%s", err, output)
}
if variant.name != "direct" {
return
}
for _, invalid := range []struct{ name, imports, source, want string }{
{"client_shutdown", "", "var _ = (*p.Client).Close", "has no field or method Close"},
{"native_connection", "", "var _ = (*p.Client).Conn", "has no field or method Conn"},
{"handle_owner", "", "var _ = p.Owner(p.Handle{})", "cannot convert"},
{"cross_provider_handle", `import mysql "github.com/frost-leo/fathomry/adapters/database/mysql/v1"`, "var _ = p.Handle(mysql.Handle{})", "cannot convert"},
} {
t.Run(invalid.name, func(t *testing.T) {
source := notice + "package main\nimport p \"github.com/frost-leo/fathomry/adapters/sqlengine/duckdb/v1\"\n" + invalid.imports + "\n" + invalid.source + "\n"
if err := os.WriteFile(filepath.Join(directory, "forbidden.go"), []byte(source), 0600); err != nil {
t.Fatal(err)
}
if output, err := run(false, "mod", "tidy"); err != nil {
t.Fatalf("authority fixture preparation: %v\n%s", err, output)
}
if output, err := run(true, "build", "-mod=readonly", "-o", filepath.Join(directory, "forbidden"), "."); err == nil || !strings.Contains(string(output), invalid.want) {
t.Fatalf("authority boundary not enforced: %v\n%s", err, output)
}
})
}
internal := "github.com/frost-leo/fathomry/internal/sqlengine/duckdb/v2"
if err := os.WriteFile(filepath.Join(directory, "forbidden.go"), []byte(notice+"package main\nimport _ "+strconv.Quote(internal)+"\n"), 0600); err != nil {
t.Fatal(err)
}
if output, err := run(true, "build", "-mod=readonly", "-o", filepath.Join(directory, "forbidden"), "."); err == nil || !strings.Contains(string(output), "use of internal package "+internal+" not allowed") {
t.Fatalf("Internal import boundary not enforced: %v\n%s", err, output)
}
})
}
}
func consumerJob(t testing.TB) string {
t.Helper()
command := exec.Command("go", "env", "GOTMPDIR")
output, err := command.Output()
if err != nil {
t.Fatal(err)
}
temporary := strings.TrimSpace(string(output))
if temporary == "" {
temporary = filepath.Join(os.TempDir(), "fathomry")
}
jobs := filepath.Join(filepath.Dir(temporary), "jobs")
if err := os.MkdirAll(jobs, 0700); err != nil {
t.Fatal(err)
}
directory, err := os.MkdirTemp(jobs, "build.")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.RemoveAll(directory); err != nil {
t.Error(err)
}
})
return directory
}