| 1 | package shellparse |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestCanMaskEarlierFailure(t *testing.T) { |
| 6 | tests := []struct { |
| 7 | command string |
| 8 | canMask bool |
| 9 | ok bool |
| 10 | }{ |
| 11 | // `&&` short-circuits: bash reports the first failing command's status. |
| 12 | {command: `go build ./... && go test ./...`, canMask: false, ok: true}, |
| 13 | {command: `a && b && c`, canMask: false, ok: true}, |
| 14 | {command: `go test ./...`, canMask: false, ok: true}, |
| 15 | {command: ``, canMask: false, ok: true}, |
| 16 | |
| 17 | // Everything else lets a later command decide the exit status. |
| 18 | {command: `go build ./... ; go test ./...`, canMask: true, ok: true}, |
| 19 | {command: "go build ./...\ngo test ./...", canMask: true, ok: true}, |
| 20 | {command: `go build ./... || true`, canMask: true, ok: true}, |
| 21 | {command: `go test ./... | tee out.txt`, canMask: true, ok: true}, |
| 22 | {command: `go test ./... |& tee out.txt`, canMask: true, ok: true}, |
| 23 | {command: `sleep 5 &`, canMask: true, ok: true}, |
| 24 | // A masking operator anywhere in the chain is enough. |
| 25 | {command: `a && b ; c`, canMask: true, ok: true}, |
| 26 | {command: `a ; b && c`, canMask: true, ok: true}, |
| 27 | {command: `a && b || c`, canMask: true, ok: true}, |
| 28 | |
| 29 | // Unanalyzable input must report ok=false rather than guess. |
| 30 | {command: `if true; then go test ./...; fi`, canMask: false, ok: false}, |
| 31 | {command: `go test ./... &&`, canMask: false, ok: false}, |
| 32 | {command: "cat <<EOF\nx\nEOF", canMask: false, ok: false}, |
| 33 | } |
| 34 | for _, tt := range tests { |
| 35 | canMask, ok := CanMaskEarlierFailure(tt.command) |
| 36 | if canMask != tt.canMask || ok != tt.ok { |
| 37 | t.Errorf("CanMaskEarlierFailure(%q) = (%v, %v), want (%v, %v)", |
| 38 | tt.command, canMask, ok, tt.canMask, tt.ok) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 |