safe_test.go

 1package tools
 2
 3import (
 4	"testing"
 5
 6	"github.com/stretchr/testify/assert"
 7)
 8
 9func TestContainsCommandChaining(t *testing.T) {
10	t.Parallel()
11
12	tests := []struct {
13		name     string
14		input    string
15		expected bool
16	}{
17		{"plain ls", "ls -la", false},
18		{"plain echo", "echo hello world", false},
19		{"plain pwd", "pwd", false},
20		{"plain git status", "git status", false},
21		{"ls with redirect", "ls > /tmp/out", false},
22		{"ls with pipe", "ls | grep foo", true},
23		{"ls with double ampersand", "ls && echo done", true},
24		{"ls with semicolon", "ls; echo done", true},
25		{"ls with pipe pipe", "ls || echo fail", true},
26		{"ls with backticks", "ls `echo foo`", true},
27		{"ls with subshell", "ls $(echo foo)", true},
28		{"ls with background ampersand", "ls & echo done", false},
29		{"rm -rf with && ls (rm first)", "rm -rf / && ls", true},
30		{"redirect with ampersand gt", "ls &> /dev/null", false},
31		{"redirect with gt ampersand", "ls >& /dev/null", false},
32		{"simple kill", "kill 1234", false},
33		{"kill with pipe", "kill 1234 | echo foo", true},
34		{"git log", "git log --oneline", false},
35		{"git log with pipe", "git log | head", true},
36		{"empty string", "", false},
37		{"dollar sign in argument", "echo $HOME", false},
38	}
39
40	for _, tt := range tests {
41		t.Run(tt.name, func(t *testing.T) {
42			t.Parallel()
43			got := containsCommandChaining(tt.input)
44			assert.Equal(t, tt.expected, got, "containsCommandChaining(%q)", tt.input)
45		})
46	}
47}