package main import ( "os" "path/filepath" "strings" "testing" ) var names = []string{"novox", "ace", "shanks", "g14"} func TestANodeNameIsRefusedWhereItIsWritten(t *testing.T) { cases := map[string]struct{ file, src string }{ "manifest string": {"modules/kernel/module.json", "{\n \"description\": \"as on Shanks\"\n}\n"}, "file line": {"modules/kernel/files/boot.sh", "#!/bin/sh\necho run on g14\n"}, "go identifier": {"modules/kernel/cmd/x/a.go", "package main\n\nfunc novox() {}\n"}, "test name": {"modules/kernel/cmd/x/a_test.go", "package main\n\nfunc TestAce(t *testing.T) {}\n\nfunc TestOnAce_x(t *testing.T) {}\n"}, } for name, c := range cases { if got := scan(c.file, c.src, names); len(got) == 0 { t.Errorf("%s: %q not refused", name, c.src) } } } func TestTheFindingNamesTheLineAndTheWord(t *testing.T) { got := scan("f.sh", "a\nb\nrun on ACE now\n", names) if len(got) != 1 || !strings.HasPrefix(got[0], "f.sh:3:") || !strings.Contains(got[0], "ace") { t.Errorf("got %v", got) } } func TestTheForgeIsNotANodeName(t *testing.T) { for _, s := range []string{ "see novox/hq issue 5", "module git.novox.be/novox/mesh-sdk/go v1.2.3", "https://git.novox.be/novox/mesh-catalog", "git.novox.be", "space trace surface grace g14x", } { if got := scan("f", s, names); len(got) != 0 { t.Errorf("%q refused: %v", s, got) } } } func TestANodeNameInAPathOrJoinedIsRefused(t *testing.T) { for _, s := range []string{ "modules/kernel/testdata/novox/meminfo", "/srv/novox/data", "testdata/shanks-meminfo.txt", "on-shanks", "shanks-desktop", "mesh-novox", "func novoxTree() {}", "aceFiles := 1", "g14Meminfo := 2", "novox_tree := 3", } { if got := scan("f", s, names); len(got) == 0 { t.Errorf("%q not refused", s) } } } func TestANodeNamedPathIsRefusedThroughRun(t *testing.T) { root := t.TempDir() p := filepath.Join(root, "modules/kernel/testdata/ace/x") os.MkdirAll(filepath.Dir(p), 0o755) os.WriteFile(p, []byte("clean\n"), 0o644) got, err := run(root, names, []string{"kernel"}) if err != nil || len(got) != 1 || !strings.HasPrefix(got[0], "modules/kernel/testdata/ace/x:0: ") { t.Errorf("got %v, %v", got, err) } } func TestCheckReturnsOneOnAFindingAndTwoOnAnUnreadableList(t *testing.T) { root := t.TempDir() write := func(p, s string) { p = filepath.Join(root, p) os.MkdirAll(filepath.Dir(p), 0o755) os.WriteFile(p, []byte(s), 0o644) } write("particulars", "ace\n") write("checks/particulars/covered", "kernel\n") write("modules/kernel/a", "clean\n") if got := check(root); got != 0 { t.Errorf("clean = %d, want 0", got) } write("modules/kernel/a", "on ace\n") if got := check(root); got != 1 { t.Errorf("finding = %d, want 1", got) } os.Remove(filepath.Join(root, "particulars")) if got := check(root); got != 2 { t.Errorf("no list = %d, want 2", got) } } func TestOnlyCoveredModulesAreRead(t *testing.T) { root := t.TempDir() write := func(p, s string) { p = filepath.Join(root, p) os.MkdirAll(filepath.Dir(p), 0o755) os.WriteFile(p, []byte(s), 0o644) } write("modules/kernel/files/a", "on novox\n") write("modules/other/files/a", "on novox\n") got, err := run(root, names, []string{"kernel"}) if err != nil { t.Fatal(err) } if len(got) != 1 || !strings.Contains(got[0], "modules/kernel/files/a:1") { t.Errorf("got %v", got) } }