| 1 | package permission |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func bashArgs(t *testing.T, command string) json.RawMessage { |
| 9 | t.Helper() |
| 10 | raw, err := json.Marshal(map[string]string{"command": command}) |
| 11 | if err != nil { |
| 12 | t.Fatal(err) |
| 13 | } |
| 14 | return raw |
| 15 | } |
| 16 | |
| 17 | func TestGitTagIsReadOnlyOnlyWhenListing(t *testing.T) { |
| 18 | readOnly := []string{"git tag", "git tag -l", "git tag --list", "git tag -l 'v1.*'", "git tag --sort=-creatordate"} |
| 19 | for _, cmd := range readOnly { |
| 20 | if !BashCommandIsReadOnly(bashArgs(t, cmd)) { |
| 21 | t.Errorf("%q only lists tags and should stay read-only", cmd) |
| 22 | } |
| 23 | } |
| 24 | writes := []string{"git tag v1.2.3", "git tag -d v1.2.3", "git tag -a v1.2.3 -m release", "git tag --delete v1.2.3"} |
| 25 | for _, cmd := range writes { |
| 26 | if BashCommandIsReadOnly(bashArgs(t, cmd)) { |
| 27 | t.Errorf("%q writes the ref namespace and must not count as read-only", cmd) |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestDestructiveGitCommandsAreWarned(t *testing.T) { |
| 33 | for _, cmd := range []string{ |
| 34 | "git restore src/main.go", |
| 35 | "git restore --staged .", |
| 36 | "git checkout -- src/main.go", |
| 37 | "git stash drop", |
| 38 | "git stash clear", |
| 39 | } { |
| 40 | if BashDangerWarning(cmd) == "" { |
| 41 | t.Errorf("%q can destroy uncommitted work and should carry a warning", cmd) |
| 42 | } |
| 43 | } |
| 44 | for _, cmd := range []string{"git status", "git checkout -b feature", "git stash list"} { |
| 45 | if w := BashDangerWarning(cmd); w != "" { |
| 46 | t.Errorf("%q is not destructive but was labelled %q", cmd, w) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestDestructiveGitCommandsGetNoPrefixGrant(t *testing.T) { |
| 52 | if got := BashCommandPrefix("git restore src/main.go"); got != "" { |
| 53 | t.Errorf("a session grant for git restore must stay exact, got prefix %q", got) |
| 54 | } |
| 55 | if got := BashCommandPrefix("git status --short"); got == "" { |
| 56 | t.Error("ordinary read-only git commands should still earn a prefix grant") |
| 57 | } |
| 58 | } |
| 59 |