Repository navigation
Expand file tree
/
Copy pathtoml_without_test.go
More file actions
134 lines (121 loc) · 3.75 KB
/
Copy pathtoml_without_test.go
File metadata and controls
134 lines (121 loc) · 3.75 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
package edit
import (
"fmt"
"reflect"
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
)
func TestTOMLWithoutDropsOnlyThatTable(t *testing.T) {
src := `model_provider = "magpie" # projects = no
profile = "p"
sqlite_home = "/s"
[projects."/a"]
trust_level = "trusted"
note = """
[profiles.fake]
model = "inside a string"
"""
[ projects . "/b" . sub ]
x = 1
[profiles.p]
model = "m"
[[projects.list]]
y = 2
[features]
projects_view = true
[model_providers.magpie]
name = "projects"
`
got := string(TOMLWithout([]byte(src), "projects"))
if strings.Contains(got, "trust_level") || strings.Contains(got, "x = 1") || strings.Contains(got, "y = 2") || strings.Contains(got, "inside a string") {
t.Fatalf("a projects table was kept:\n%s", got)
}
var want, have map[string]any
if err := toml.Unmarshal([]byte(src), &want); err != nil {
t.Fatal(err)
}
if err := toml.Unmarshal([]byte(got), &have); err != nil {
t.Fatalf("%v:\n%s", err, got)
}
delete(want, "projects")
if !reflect.DeepEqual(want, have) {
t.Fatalf("\nwant %v\nhave %v", want, have)
}
}
func TestTOMLWithoutLeavesTheRestAlone(t *testing.T) {
for _, src := range []string{
"",
"model = \"m\"\n[profiles.p]\nmodel = \"x\"\n",
"[projects.\"/a\"\nbroken", // a parse error: the decode after it fails the same
"projects.\"/a\".trust_level = \"trusted\"\n", // a dotted key, not a table
} {
if got := TOMLWithout([]byte(src), "projects"); string(got) != src {
t.Fatalf("%q became %q", src, got)
}
}
}
func manyProjects(n int) []byte {
var b strings.Builder
b.WriteString("model_provider = \"magpie\"\nprofile = \"p\"\n[features]\nmulti_agent = true\n")
for i := range n {
fmt.Fprintf(&b, "\n[projects.\"/Users/someone/workspace/projects/repo-%05d\"]\ntrust_level = \"trusted\"\n", i)
}
b.WriteString("\n[profiles.p]\nmodel = \"m\"\n")
return []byte(b.String())
}
func BenchmarkCodexConfigDecode(b *testing.B) {
raw := manyProjects(10933)
type top struct {
Provider string `toml:"model_provider"`
Profiles map[string]any `toml:"profiles"`
}
b.Run("whole", func(b *testing.B) {
for b.Loop() {
var c top
toml.Unmarshal(raw, &c)
}
})
b.Run("without", func(b *testing.B) {
for b.Loop() {
var c top
toml.Unmarshal(TOMLWithout(raw, "projects"), &c)
}
})
}
// TestValidateTOMLDefinitionsInGroups: a config of many projects is checked
// project by project, and a table or key defined twice anywhere is still
// refused, with the error the whole file gives.
func TestValidateTOMLDefinitionsInGroups(t *testing.T) {
good := manyProjects(200)
if err := validateTOMLDefinitions(good); err != nil {
t.Fatal(err)
}
var whole map[string]any
for name, bad := range map[string]string{
"a project twice": "\n[projects.\"/Users/someone/workspace/projects/repo-00007\"]\ntrust_level = \"x\"\n",
"a key twice": "\n[projects.\"/n\"]\na = 1\na = 2\n",
"a subtable then its key": "\n[projects.\"/m\".s]\nb = 1\n[projects.\"/m\"]\ns = 2\n",
"the rest's table twice": "\n[features]\nz = 1\n",
"a bare projects table": "\n[projects]\n\"/Users/someone/workspace/projects/repo-00003\" = 1\n",
} {
raw := append(append([]byte{}, good...), bad...)
want := toml.Unmarshal(raw, &whole)
got := validateTOMLDefinitions(raw)
if want == nil || got == nil || got.Error() != want.Error() {
t.Errorf("%s: got %v, want %v", name, got, want)
}
}
top := append([]byte("projects.\"/Users/someone/workspace/projects/repo-00001\".trust_level = \"x\"\n"), good...)
if validateTOMLDefinitions(top) == nil {
t.Error("a dotted projects key clashing with a table passed")
}
}
func BenchmarkValidateCodexConfig(b *testing.B) {
raw := manyProjects(10933)
for b.Loop() {
if err := validateTOMLDefinitions(raw); err != nil {
b.Fatal(err)
}
}
}