| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "bytes" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestTrustApprovesWhatTheWorkspaceNamesOnlyWhenAsked(t *testing.T) { |
| 13 | home := t.TempDir() |
| 14 | root := t.TempDir() |
| 15 | t.Setenv("REASONIX_HOME", home) |
| 16 | if err := os.MkdirAll(filepath.Join(root, ".reasonix"), 0o700); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | if err := os.WriteFile(filepath.Join(root, ".reasonix", "settings.json"), []byte(`{"hooks":{"Stop":[{"command":"echo stop"}]}}`), 0o600); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | rg := filepath.Join(filepath.VolumeName(os.TempDir())+string(filepath.Separator), "opt", "reasonix-test", "rg") |
| 23 | if err := os.WriteFile(filepath.Join(root, "reasonix.toml"), []byte("[tools.search]\nrg_path = '"+rg+"'\n"), 0o600); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | run := func(input string, interactive bool, args ...string) (int, string) { |
| 27 | var out bytes.Buffer |
| 28 | code := runTrust(append([]string{"--dir", root}, args...), bufio.NewScanner(strings.NewReader(input)), &out, interactive) |
| 29 | return code, out.String() |
| 30 | } |
| 31 | |
| 32 | if code, out := run("", false); code != 1 || !strings.Contains(out, "echo stop") || !strings.Contains(out, rg) { |
| 33 | t.Fatalf("non-interactive listing = %d %q, want both programs listed and nothing approved", code, out) |
| 34 | } |
| 35 | if code, _ := run("n\n", true); code != 1 { |
| 36 | t.Fatalf("declined prompt exit = %d, want 1", code) |
| 37 | } |
| 38 | if pending, _ := pendingProjectPrograms(root); len(pending) != 2 { |
| 39 | t.Fatalf("pending after declining = %d, want 2", len(pending)) |
| 40 | } |
| 41 | if code, out := run("y\n", true); code != 0 { |
| 42 | t.Fatalf("approving = %d %q", code, out) |
| 43 | } |
| 44 | if pending, _ := pendingProjectPrograms(root); len(pending) != 0 { |
| 45 | t.Fatalf("pending after approval = %+v, want none", pending) |
| 46 | } |
| 47 | if code, _ := run("", false, "--revoke"); code != 0 { |
| 48 | t.Fatal("revoke failed") |
| 49 | } |
| 50 | if pending, _ := pendingProjectPrograms(root); len(pending) != 2 { |
| 51 | t.Fatalf("pending after revoke = %d, want 2", len(pending)) |
| 52 | } |
| 53 | } |
| 54 |