package main import ( "os" "path/filepath" "reflect" "testing" ) // firstCount is how many lines the list held when it was first written: it only shrinks. const firstCount = 63 func module(t *testing.T, root, name, manifest string, files map[string]string) { t.Helper() dir := filepath.Join(root, "modules", name) for path, text := range map[string]string{"module.json": manifest} { files[path] = text } for path, text := range files { full := filepath.Join(dir, filepath.FromSlash(path)) if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(full, []byte(text), 0o644); err != nil { t.Fatal(err) } } } // A word read and set by nothing is found, in Go (a literal, a constant of the same directory) and in TypeScript; one // the manifest sets (an env, an environment's variables, an env-file line it ships) or the mesh gives every process is // not; tests, node_modules and dist are not read. func TestAWordReadThatNothingSetsIsFound(t *testing.T) { root := t.TempDir() module(t, root, "reader", `{"module":"reader","build":{"artifacts":[{"name":"tools","env":{"MESH_SET":"x"}}]},`+ `"resources":[{"id":"p","type":"process","environment":{"variables":{"MESH_VARIABLE":"y"}}}]}`, map[string]string{ "cmd/a/main.go": `package main; const reach = "MESH_REACH"; var _ = os.Getenv(reach); var _ = os.Getenv("MESH_SET")`, "cmd/a/other.go": `package main; var _, _ = os.LookupEnv("MESH_LITERAL"); var _ = os.Getenv("MESH_MODULE")`, "cmd/a/write.go": `package main; var _ = []string{"MESH_WRITTEN_FOR_A_CHILD=1"}`, "cmd/a/a_test.go": `package main; var _ = os.Getenv("MESH_IN_A_TEST")`, "client.ts": `const a = process.env.MESH_TS ?? "d"; const b = env["MESH_BRACKET"]; const c = env.MESH_VARIABLE;`, "files/env": "MESH_FROM_FILE=${config:x}\n", "index.ts": `const f = env.MESH_FROM_FILE;`, "test/x.test.ts": `env.MESH_IN_A_TEST_DIR`, "node_modules/x.ts": `env.MESH_VENDORED`, }) module(t, root, "quiet", `{"module":"quiet"}`, map[string]string{"main.go": `package main; var _ = os.Getenv("MESH_NODE")`}) got, err := Check(root) if err != nil { t.Fatal(err) } want := map[string]map[string][]string{"reader": { "MESH_REACH": {"cmd/a/main.go"}, "MESH_LITERAL": {"cmd/a/other.go"}, "MESH_TS": {"client.ts"}, "MESH_BRACKET": {"client.ts"}, }} if !reflect.DeepEqual(got, want) { t.Fatalf("found %v, want %v", got, want) } refused, stale := Judge(got, map[string]bool{"reader MESH_REACH": true, "reader MESH_GONE": true}) if !reflect.DeepEqual(refused, []string{"reader MESH_BRACKET", "reader MESH_LITERAL", "reader MESH_TS"}) || !reflect.DeepEqual(stale, []string{"reader MESH_GONE"}) { t.Errorf("refused %v, stale %v", refused, stale) } } // The catalogue itself: every word a module reads is set or given, but the list's, and the list only shrinks. func TestTheCatalogueReadsOnlyWordsTheMeshSets(t *testing.T) { found, err := Check(filepath.Join("..", "..")) if err != nil { t.Fatal(err) } listed, err := Listed(listFile) if err != nil { t.Fatal(err) } refused, stale := Judge(found, listed) for _, line := range refused { t.Errorf("%s: read, set by nothing, and not on checks/environment/unset", line) } for _, line := range stale { t.Errorf("checks/environment/unset names %q, which no code reads unset any more", line) } if len(listed) > firstCount { t.Errorf("checks/environment/unset holds %d lines, more than its first %d: it only shrinks", len(listed), firstCount) } }