package main import ( "os" "path/filepath" "strings" "testing" ) func catalogue(t *testing.T, files map[string]string) string { t.Helper() root := t.TempDir() for p, c := range files { full := filepath.Join(root, p) if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(full, []byte(c), 0o644); err != nil { t.Fatal(err) } } return root } const owner = `{"module":"power","resources":[{"id":"d","type":"directory","path":"/run/mesh-power"}]}` func TestAModuleReadingAnothersStateIsFound(t *testing.T) { root := catalogue(t, map[string]string{ "modules/power/module.json": owner, "modules/power/cmd/power/draw.go": `const f = "/run/mesh-power/draw"`, "modules/bar/module.json": `{"module":"bar","resources":[{"id":"x","type":"file","path":"/etc/x","content":"cat /run/mesh-power/draw"}]}`, "modules/other/module.json": `{"module":"other"}`, "modules/other/files/bin/read": "#!/bin/sh\ncat /run/mesh-power\n", "modules/other/README.md": "it once read /run/mesh-power/draw", "modules/other/cmd/x/x_test.go": `"/run/mesh-power"`, "modules/near/module.json": `{"module":"near","resources":[{"id":"y","type":"file","path":"/etc/y","content":"/run/mesh-power-other/x"}]}`, }) found, err := Check(root) if err != nil { t.Fatal(err) } var got []string for _, f := range found { got = append(got, f.Module+" "+f.File) } want := []string{"bar modules/bar/module.json", "other modules/other/files/bin/read"} if strings.Join(got, "|") != strings.Join(want, "|") { t.Fatalf("found %v, want %v", got, want) } if !strings.Contains(found[0].String(), "power's") { t.Fatalf("%s", found[0]) } } func TestACatalogueWhereEachKeepsToItsOwnIsClean(t *testing.T) { root := catalogue(t, map[string]string{ "modules/power/module.json": owner, "modules/power/cmd/power/draw.go": `const f = "/run/mesh-power/draw"`, }) if found, err := Check(root); err != nil || len(found) != 0 { t.Fatalf("%v %v", found, err) } }