package colophon import ( "os" "path/filepath" "strings" "testing" "amadan.net/oficina/estilo.git/credits" ) func write(t *testing.T, path, text string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { t.Fatal(err) } if err := os.WriteFile(path, []byte(text), 0o600); err != nil { t.Fatal(err) } } // cargo metadata's shape, cut to what Cargo reads: the root's normal and // build dependencies ship (a build dependency's licence travels with // what it generated), a dev-dependency does not, and nothing reachable // only through a dev-dependency does either. func TestCargoWalksNormalAndBuildDependenciesOnly(t *testing.T) { root := t.TempDir() for _, crate := range []string{"yrs", "serde_derive", "wasmparser", "only-via-dev"} { write(t, filepath.Join(root, crate, "Cargo.toml"), "") write(t, filepath.Join(root, crate, "LICENSE-MIT"), crate+" licence\n") write(t, filepath.Join(root, crate, "license.rs"), "// source, not a notice") } write(t, filepath.Join(root, "bare", "Cargo.toml"), "") manifest := func(c string) string { return filepath.Join(root, c, "Cargo.toml") } meta := `{"packages":[ {"id":"app","name":"docs-core","version":"0.1.0","manifest_path":"` + filepath.Join(root, "app", "Cargo.toml") + `"}, {"id":"yrs","name":"yrs","version":"0.23.5","manifest_path":"` + manifest("yrs") + `"}, {"id":"sd","name":"serde_derive","version":"1.0.229","manifest_path":"` + manifest("serde_derive") + `"}, {"id":"wp","name":"wasmparser","version":"0.258.0","manifest_path":"` + manifest("wasmparser") + `"}, {"id":"dv","name":"only-via-dev","version":"1.0.0","manifest_path":"` + manifest("only-via-dev") + `"}, {"id":"bare","name":"bare","version":"2.0.0","manifest_path":"` + manifest("bare") + `"}], "resolve":{"root":"app","nodes":[ {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"sd","dep_kinds":[{"kind":"build"}]},{"pkg":"wp","dep_kinds":[{"kind":"dev"}]}]}, {"id":"yrs","deps":[{"pkg":"bare","dep_kinds":[{"kind":null}]}]}, {"id":"sd","deps":[]},{"id":"bare","deps":[]}, {"id":"wp","deps":[{"pkg":"dv","dep_kinds":[{"kind":null}]}]},{"id":"dv","deps":[]}]}}` got, err := CollectCargo(strings.NewReader(meta)) if err != nil { t.Fatal(err) } var names []string for _, p := range got { names = append(names, p.Name+"@"+p.Version) if p.Ecosystem != Cargo { t.Errorf("%s is %s", p.Name, p.Ecosystem) } } if strings.Join(names, " ") != "bare@2.0.0 serde_derive@1.0.229 yrs@0.23.5" { t.Fatalf("crates = %v", names) } if len(got[2].Notices) != 1 || got[2].Notices[0].Name != "LICENSE-MIT" || got[2].Notices[0].Text != "yrs licence\n" { t.Fatalf("yrs notices = %+v (a .rs file is source, not a notice)", got[2].Notices) } if len(got[0].Notices) != 0 { t.Fatal("a crate with no licence file was given one") } if _, err := CollectCargo(strings.NewReader(`{"packages":[],"resolve":null}`)); err == nil { t.Fatal("metadata without a resolve graph was accepted (run without --locked?)") } } // esbuild's metafile names every input file it bundled; the packages are // the node_modules directories those files are in, with their own // package.json and licence. func TestNPMNamesThePackagesTheBundleRead(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "node_modules/yjs/package.json"), `{"name":"yjs","version":"13.6.27"}`) write(t, filepath.Join(root, "node_modules/yjs/LICENSE"), "yjs licence") write(t, filepath.Join(root, "node_modules/yjs/dist/yjs.mjs"), "") write(t, filepath.Join(root, "node_modules/@scope/pkg/package.json"), `{"name":"@scope/pkg","version":"1.0.0"}`) write(t, filepath.Join(root, "node_modules/@scope/pkg/LICENSE.md"), "scoped licence") write(t, filepath.Join(root, "node_modules/@scope/pkg/lib/a.js"), "") // A package nested under another's node_modules is its own package. write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/package.json"), `{"name":"lib0","version":"0.2.117"}`) write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/LICENSE"), "lib0 licence") write(t, filepath.Join(root, "node_modules/yjs/node_modules/lib0/index.js"), "") 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"} got, err := CollectNPM(root, inputs) if err != nil { t.Fatal(err) } var names []string for _, p := range got { names = append(names, p.Name+"@"+p.Version+":"+p.Notices[0].Text) } if strings.Join(names, " ") != "@scope/pkg@1.0.0:scoped licence lib0@0.2.117:lib0 licence yjs@13.6.27:yjs licence" { t.Fatalf("packages = %v", names) } write(t, filepath.Join(root, "node_modules/liar/package.json"), `{"name":"someone-else","version":"1.0.0"}`) write(t, filepath.Join(root, "node_modules/liar/x.js"), "") if _, err := CollectNPM(root, []string{"node_modules/liar/x.js"}); err == nil { t.Fatal("a package.json naming a different package was accepted") } if _, err := CollectNPM(root, []string{"node_modules/../../etc/passwd"}); err == nil { t.Fatal("an input outside node_modules was accepted") } } func TestFromComponentsKeepsNamesVersionsAndNotices(t *testing.T) { got := FromComponents(Go, []credits.Component{{Name: "github.com/go-chi/chi/v5", Version: "v5.3.2", Notices: notice("chi")}}) 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" { t.Fatalf("%+v", got) } } func TestDirNoticesRefusesSymlinksAndEmptyFiles(t *testing.T) { dir := t.TempDir() write(t, filepath.Join(dir, "COPYING"), " \n") if _, err := DirNotices(dir); err == nil { t.Fatal("an empty notice was accepted") } dir = t.TempDir() write(t, filepath.Join(dir, "real"), "text") if err := os.Symlink(filepath.Join(dir, "real"), filepath.Join(dir, "LICENSE")); err != nil { t.Fatal(err) } if _, err := DirNotices(dir); err == nil { t.Fatal("a symlinked notice was accepted") } } // A crate's tools are its direct dev-dependencies: what its tests run // on, named once. What they in turn depend on is theirs, not the app's. func TestCollectCargoToolsIsTheDirectDevDependencies(t *testing.T) { root := t.TempDir() write(t, filepath.Join(root, "wp", "LICENSE-APACHE"), "wasmparser licence") meta := `{"packages":[ {"id":"app","name":"docs-core","version":"0.1.0","manifest_path":"` + filepath.Join(root, "app", "Cargo.toml") + `"}, {"id":"yrs","name":"yrs","version":"0.23.5","manifest_path":"` + filepath.Join(root, "yrs", "Cargo.toml") + `"}, {"id":"wp","name":"wasmparser","version":"0.258.0","manifest_path":"` + filepath.Join(root, "wp", "Cargo.toml") + `"}, {"id":"both","name":"serde_json","version":"1.0.151","manifest_path":"` + filepath.Join(root, "both", "Cargo.toml") + `"}, {"id":"dv","name":"only-via-dev","version":"1.0.0","manifest_path":"` + filepath.Join(root, "dv", "Cargo.toml") + `"}], "resolve":{"root":"app","nodes":[ {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"wp","dep_kinds":[{"kind":"dev"}]},{"pkg":"both","dep_kinds":[{"kind":"dev"},{"kind":null}]}]}, {"id":"yrs","deps":[]},{"id":"both","deps":[]}, {"id":"wp","deps":[{"pkg":"dv","dep_kinds":[{"kind":null}]}]},{"id":"dv","deps":[]}]}}` got, err := CollectCargoTools(strings.NewReader(meta)) if err != nil { t.Fatal(err) } if len(got) != 1 || got[0].Name != "wasmparser" || !got[0].Tool || got[0].Notices[0].Text != "wasmparser licence" { t.Fatalf("tools = %+v", got) } } // Proc-macros and build dependencies run while the crate is compiled and // are not linked into what ships, nor is anything reached only through // them; CollectCargo marks them tools. A crate linked as well as used by a // macro ships. func TestCollectCargoMarksMacroAndBuildOnlyCratesAsTools(t *testing.T) { root := t.TempDir() crates := []string{"yrs", "serde", "serde_derive", "syn", "unicode-ident", "autocfg", "itoa"} var pkgs []string for _, c := range crates { write(t, filepath.Join(root, c, "LICENSE"), c+" licence") kind := `"lib"` if c == "serde_derive" { kind = `"proc-macro"` } pkgs = append(pkgs, `{"id":"`+c+`","name":"`+c+`","version":"1.0.0","manifest_path":"`+filepath.Join(root, c, "Cargo.toml")+`","targets":[{"kind":[`+kind+`]}]}`) } pkgs = append(pkgs, `{"id":"app","name":"app","version":"0.1.0","manifest_path":"`+filepath.Join(root, "app", "Cargo.toml")+`","targets":[{"kind":["cdylib"]}]}`) meta := `{"packages":[` + strings.Join(pkgs, ",") + `],"resolve":{"root":"app","nodes":[ {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"autocfg","dep_kinds":[{"kind":"build"}]}]}, {"id":"yrs","deps":[{"pkg":"serde","dep_kinds":[{"kind":null}]},{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]}, {"id":"serde","deps":[{"pkg":"serde_derive","dep_kinds":[{"kind":null}]},{"pkg":"itoa","dep_kinds":[{"kind":null}]}]}, {"id":"serde_derive","deps":[{"pkg":"syn","dep_kinds":[{"kind":null}]},{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]}, {"id":"syn","deps":[{"pkg":"unicode-ident","dep_kinds":[{"kind":null}]}]}, {"id":"unicode-ident","deps":[]},{"id":"autocfg","deps":[]},{"id":"itoa","deps":[]}]}}` got, err := CollectCargo(strings.NewReader(meta)) if err != nil { t.Fatal(err) } tool := map[string]bool{} for _, p := range got { tool[p.Name] = p.Tool } want := map[string]bool{"yrs": false, "serde": false, "itoa": false, "unicode-ident": false, "serde_derive": true, "syn": true, "autocfg": true} for name, w := range want { if v, ok := tool[name]; !ok || v != w { t.Errorf("%s: tool=%v (present %v), want %v", name, v, ok, w) } } } // CollectCargo records each crate's direct dependents in the walked graph, // so a default usage note can be checked against what actually uses it. func TestCollectCargoRecordsWhatUsesEachCrate(t *testing.T) { root := t.TempDir() var pkgs []string for _, c := range []string{"yrs", "serde_json", "itoa", "smallvec"} { write(t, filepath.Join(root, c, "LICENSE"), c) pkgs = append(pkgs, `{"id":"`+c+`","name":"`+c+`","version":"1.0.0","manifest_path":"`+filepath.Join(root, c, "Cargo.toml")+`","targets":[{"kind":["lib"]}]}`) } pkgs = append(pkgs, `{"id":"app","name":"app","version":"0.1.0","manifest_path":"`+filepath.Join(root, "app", "Cargo.toml")+`","targets":[{"kind":["cdylib"]}]}`) meta := `{"packages":[` + strings.Join(pkgs, ",") + `],"resolve":{"root":"app","nodes":[ {"id":"app","deps":[{"pkg":"yrs","dep_kinds":[{"kind":null}]},{"pkg":"smallvec","dep_kinds":[{"kind":null}]}]}, {"id":"yrs","deps":[{"pkg":"serde_json","dep_kinds":[{"kind":null}]},{"pkg":"smallvec","dep_kinds":[{"kind":null}]}]}, {"id":"serde_json","deps":[{"pkg":"itoa","dep_kinds":[{"kind":null}]}]}, {"id":"itoa","deps":[]},{"id":"smallvec","deps":[]}]}}` got, err := CollectCargo(strings.NewReader(meta)) if err != nil { t.Fatal(err) } via := map[string]string{} for _, p := range got { via[p.Name] = strings.Join(p.Via, ",") } // The root is the app itself, not a dependent to name. if via["itoa"] != "serde_json" || via["serde_json"] != "yrs" || via["smallvec"] != "yrs" || via["yrs"] != "" { t.Fatalf("via = %v", via) } }