oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package colophon
2
3import (
4 "os"
5 "path/filepath"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/estilo.git/credits"
10)
11
12func write(t *testing.T, path, text string) {
13 t.Helper()
14 if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
15 t.Fatal(err)
16 }
17 if err := os.WriteFile(path, []byte(text), 0o600); err != nil {
18 t.Fatal(err)
19 }
20}
21
22// cargo metadata's shape, cut to what Cargo reads: the root's normal and
23// build dependencies ship (a build dependency's licence travels with
24// what it generated), a dev-dependency does not, and nothing reachable
25// only through a dev-dependency does either.
26func TestCargoWalksNormalAndBuildDependenciesOnly(t *testing.T) {
27 root := t.TempDir()
28 for _, crate := range []string{"yrs", "serde_derive", "wasmparser", "only-via-dev"} {
29 write(t, filepath.Join(root, crate, "Cargo.toml"), "")
30 write(t, filepath.Join(root, crate, "LICENSE-MIT"), crate+" licence\n")
31 write(t, filepath.Join(root, crate, "license.rs"), "// source, not a notice")
32 }
33 write(t, filepath.Join(root, "bare", "Cargo.toml"), "")
34 manifest := func(c string) string { return filepath.Join(root, c, "Cargo.toml") }
35 meta := `{"packages":[
36 {"id":"app","name":"docs-core","version":"0.1.0","manifest_path":"` + filepath.Join(root, "app", "Cargo.toml") + `"},
37 {"id":"yrs","name":"yrs","version":"0.23.5","manifest_path":"` + manifest("yrs") + `"},
38 {"id":"sd","name":"serde_derive","version":"1.0.229","manifest_path":"` + manifest("serde_derive") + `"},
39 {"id":"wp","name":"wasmparser","version":"0.258.0","manifest_path":"` + manifest("wasmparser") + `"},
40 {"id":"dv","name":"only-via-dev","version":"1.0.0","manifest_path":"` + manifest("only-via-dev") + `"},
41 {"id":"bare","name":"bare","version":"2.0.0","manifest_path":"` + manifest("bare") + `"}],
42 "resolve":{"root":"app","nodes":[
43 {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"sd","dep_kinds":[{"kind":"build"}]},{"pkg":"wp","dep_kinds":[{"kind":"dev"}]}]},
44 {"id":"yrs","deps":[{"pkg":"bare","dep_kinds":[{"kind":null}]}]},
45 {"id":"sd","deps":[]},{"id":"bare","deps":[]},
46 {"id":"wp","deps":[{"pkg":"dv","dep_kinds":[{"kind":null}]}]},{"id":"dv","deps":[]}]}}`
47 got, err := CollectCargo(strings.NewReader(meta))
48 if err != nil {
49 t.Fatal(err)
50 }
51 var names []string
52 for _, p := range got {
53 names = append(names, p.Name+"@"+p.Version)
54 if p.Ecosystem != Cargo {
55 t.Errorf("%s is %s", p.Name, p.Ecosystem)
56 }
57 }
58 if strings.Join(names, " ") != "bare@2.0.0 serde_derive@1.0.229 yrs@0.23.5" {
59 t.Fatalf("crates = %v", names)
60 }
61 if len(got[2].Notices) != 1 || got[2].Notices[0].Name != "LICENSE-MIT" || got[2].Notices[0].Text != "yrs licence\n" {
62 t.Fatalf("yrs notices = %+v (a .rs file is source, not a notice)", got[2].Notices)
63 }
64 if len(got[0].Notices) != 0 {
65 t.Fatal("a crate with no licence file was given one")
66 }
67 if _, err := CollectCargo(strings.NewReader(`{"packages":[],"resolve":null}`)); err == nil {
68 t.Fatal("metadata without a resolve graph was accepted (run without --locked?)")
69 }
70}
71
72// esbuild's metafile names every input file it bundled; the packages are
73// the node_modules directories those files are in, with their own
74// package.json and licence.
75func TestNPMNamesThePackagesTheBundleRead(t *testing.T) {
76 root := t.TempDir()
77 write(t, filepath.Join(root, "node_modules/yjs/package.json"), `{"name":"yjs","version":"13.6.27"}`)
78 write(t, filepath.Join(root, "node_modules/yjs/LICENSE"), "yjs licence")
79 write(t, filepath.Join(root, "node_modules/yjs/dist/yjs.mjs"), "")
80 write(t, filepath.Join(root, "node_modules/@scope/pkg/package.json"), `{"name":"@scope/pkg","version":"1.0.0"}`)
81 write(t, filepath.Join(root, "node_modules/@scope/pkg/LICENSE.md"), "scoped licence")
82 write(t, filepath.Join(root, "node_modules/@scope/pkg/lib/a.js"), "")
83 // A package nested under another's node_modules is its own package.
84 write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/package.json"), `{"name":"lib0","version":"0.2.117"}`)
85 write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/LICENSE"), "lib0 licence")
86 write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/index.js"), "")
87 inputs := []string{"node_modules/yjs/dist/yjs.mjs", "editor-entry.js", "node_modules/@scope/pkg/lib/a.js", "node_modules/yjs/node_modules/lib0/index.js", "node_modules/yjs/dist/yjs.mjs"}
88 got, err := CollectNPM(root, inputs)
89 if err != nil {
90 t.Fatal(err)
91 }
92 var names []string
93 for _, p := range got {
94 names = append(names, p.Name+"@"+p.Version+":"+p.Notices[0].Text)
95 }
96 if strings.Join(names, " ") != "@scope/pkg@1.0.0:scoped licence lib0@0.2.117:lib0 licence yjs@13.6.27:yjs licence" {
97 t.Fatalf("packages = %v", names)
98 }
99 write(t, filepath.Join(root, "node_modules/liar/package.json"), `{"name":"someone-else","version":"1.0.0"}`)
100 write(t, filepath.Join(root, "node_modules/liar/x.js"), "")
101 if _, err := CollectNPM(root, []string{"node_modules/liar/x.js"}); err == nil {
102 t.Fatal("a package.json naming a different package was accepted")
103 }
104 if _, err := CollectNPM(root, []string{"node_modules/../../etc/passwd"}); err == nil {
105 t.Fatal("an input outside node_modules was accepted")
106 }
107}
108
109func TestFromComponentsKeepsNamesVersionsAndNotices(t *testing.T) {
110 got := FromComponents(Go, []credits.Component{{Name: "github.com/go-chi/chi/v5", Version: "v5.3.2", Notices: notice("chi")}})
111 if len(got) != 1 || got[0].Ecosystem != Go || got[0].Name != "github.com/go-chi/chi/v5" || got[0].Version != "v5.3.2" || got[0].Notices[0].Text != "chi" {
112 t.Fatalf("%+v", got)
113 }
114}
115
116func TestDirNoticesRefusesSymlinksAndEmptyFiles(t *testing.T) {
117 dir := t.TempDir()
118 write(t, filepath.Join(dir, "COPYING"), " \n")
119 if _, err := DirNotices(dir); err == nil {
120 t.Fatal("an empty notice was accepted")
121 }
122 dir = t.TempDir()
123 write(t, filepath.Join(dir, "real"), "text")
124 if err := os.Symlink(filepath.Join(dir, "real"), filepath.Join(dir, "LICENSE")); err != nil {
125 t.Fatal(err)
126 }
127 if _, err := DirNotices(dir); err == nil {
128 t.Fatal("a symlinked notice was accepted")
129 }
130}
131
132// A crate's tools are its direct dev-dependencies: what its tests run
133// on, named once. What they in turn depend on is theirs, not the app's.
134func TestCollectCargoToolsIsTheDirectDevDependencies(t *testing.T) {
135 root := t.TempDir()
136 write(t, filepath.Join(root, "wp", "LICENSE-APACHE"), "wasmparser licence")
137 meta := `{"packages":[
138 {"id":"app","name":"docs-core","version":"0.1.0","manifest_path":"` + filepath.Join(root, "app", "Cargo.toml") + `"},
139 {"id":"yrs","name":"yrs","version":"0.23.5","manifest_path":"` + filepath.Join(root, "yrs", "Cargo.toml") + `"},
140 {"id":"wp","name":"wasmparser","version":"0.258.0","manifest_path":"` + filepath.Join(root, "wp", "Cargo.toml") + `"},
141 {"id":"both","name":"serde_json","version":"1.0.151","manifest_path":"` + filepath.Join(root, "both", "Cargo.toml") + `"},
142 {"id":"dv","name":"only-via-dev","version":"1.0.0","manifest_path":"` + filepath.Join(root, "dv", "Cargo.toml") + `"}],
143 "resolve":{"root":"app","nodes":[
144 {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"wp","dep_kinds":[{"kind":"dev"}]},{"pkg":"both","dep_kinds":[{"kind":"dev"},{"kind":null}]}]},
145 {"id":"yrs","deps":[]},{"id":"both","deps":[]},
146 {"id":"wp","deps":[{"pkg":"dv","dep_kinds":[{"kind":null}]}]},{"id":"dv","deps":[]}]}}`
147 got, err := CollectCargoTools(strings.NewReader(meta))
148 if err != nil {
149 t.Fatal(err)
150 }
151 if len(got) != 1 || got[0].Name != "wasmparser" || !got[0].Tool || got[0].Notices[0].Text != "wasmparser licence" {
152 t.Fatalf("tools = %+v", got)
153 }
154}
155
156// Proc-macros and build dependencies run while the crate is compiled and
157// are not linked into what ships, nor is anything reached only through
158// them; CollectCargo marks them tools. A crate linked as well as used by a
159// macro ships.
160func TestCollectCargoMarksMacroAndBuildOnlyCratesAsTools(t *testing.T) {
161 root := t.TempDir()
162 crates := []string{"yrs", "serde", "serde_derive", "syn", "unicode-ident", "autocfg", "itoa"}
163 var pkgs []string
164 for _, c := range crates {
165 write(t, filepath.Join(root, c, "LICENSE"), c+" licence")
166 kind := `"lib"`
167 if c == "serde_derive" {
168 kind = `"proc-macro"`
169 }
170 pkgs = append(pkgs, `{"id":"`+c+`","name":"`+c+`","version":"1.0.0","manifest_path":"`+filepath.Join(root, c, "Cargo.toml")+`","targets":[{"kind":[`+kind+`]}]}`)
171 }
172 pkgs = append(pkgs, `{"id":"app","name":"app","version":"0.1.0","manifest_path":"`+filepath.Join(root, "app", "Cargo.toml")+`","targets":[{"kind":["cdylib"]}]}`)
173 meta := `{"packages":[` + strings.Join(pkgs, ",") + `],"resolve":{"root":"app","nodes":[
174 {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"autocfg","dep_kinds":[{"kind":"build"}]}]},
175 {"id":"yrs","deps":[{"pkg":"serde","dep_kinds":[{"kind":null}]},{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]},
176 {"id":"serde","deps":[{"pkg":"serde_derive","dep_kinds":[{"kind":null}]},{"pkg":"itoa","dep_kinds":[{"kind":null}]}]},
177 {"id":"serde_derive","deps":[{"pkg":"syn","dep_kinds":[{"kind":null}]},{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]},
178 {"id":"syn","deps":[{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]},
179 {"id":"unicode-ident","deps":[]},{"id":"autocfg","deps":[]},{"id":"itoa","deps":[]}]}}`
180 got, err := CollectCargo(strings.NewReader(meta))
181 if err != nil {
182 t.Fatal(err)
183 }
184 tool := map[string]bool{}
185 for _, p := range got {
186 tool[p.Name] = p.Tool
187 }
188 want := map[string]bool{"yrs": false, "serde": false, "itoa": false, "unicode-ident": false, "serde_derive": true, "syn": true, "autocfg": true}
189 for name, w := range want {
190 if v, ok := tool[name]; !ok || v != w {
191 t.Errorf("%s: tool=%v (present %v), want %v", name, v, ok, w)
192 }
193 }
194}
195
196// CollectCargo records each crate's direct dependents in the walked graph,
197// so a default usage note can be checked against what actually uses it.
198func TestCollectCargoRecordsWhatUsesEachCrate(t *testing.T) {
199 root := t.TempDir()
200 var pkgs []string
201 for _, c := range []string{"yrs", "serde_json", "itoa", "smallvec"} {
202 write(t, filepath.Join(root, c, "LICENSE"), c)
203 pkgs = append(pkgs, `{"id":"`+c+`","name":"`+c+`","version":"1.0.0","manifest_path":"`+filepath.Join(root, c, "Cargo.toml")+`","targets":[{"kind":["lib"]}]}`)
204 }
205 pkgs = append(pkgs, `{"id":"app","name":"app","version":"0.1.0","manifest_path":"`+filepath.Join(root, "app", "Cargo.toml")+`","targets":[{"kind":["cdylib"]}]}`)
206 meta := `{"packages":[` + strings.Join(pkgs, ",") + `],"resolve":{"root":"app","nodes":[
207 {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"smallvec","dep_kinds":[{"kind":null}]}]},
208 {"id":"yrs","deps":[{"pkg":"serde_json","dep_kinds":[{"kind":null}]},{"pkg":"smallvec","dep_kinds":[{"kind":null}]}]},
209 {"id":"serde_json","deps":[{"pkg":"itoa","dep_kinds":[{"kind":null}]}]},
210 {"id":"itoa","deps":[]},{"id":"smallvec","deps":[]}]}}`
211 got, err := CollectCargo(strings.NewReader(meta))
212 if err != nil {
213 t.Fatal(err)
214 }
215 via := map[string]string{}
216 for _, p := range got {
217 via[p.Name] = strings.Join(p.Via, ",")
218 }
219 // The root is the app itself, not a dependent to name.
220 if via["itoa"] != "serde_json" || via["serde_json"] != "yrs" || via["smallvec"] != "yrs" || via["yrs"] != "" {
221 t.Fatalf("via = %v", via)
222 }
223}
224