package codescan import ( "errors" "testing" "gitbay.org/gitbay/internal/store" ) func TestSplitLines(t *testing.T) { for _, tc := range []struct { name, src string want bool }{ {"arithmetic in double quotes", `text() { printf '×'; printf "%$(($1 - 1))s" '' | tr ' ' x; }`, false}, {"bare arithmetic", `echo $(( $n + 1 ))`, false}, {"nested arithmetic parens", `echo "$(( ($a + $b) * 2 ))"`, false}, {"double quoted", `echo "$x ${y:-a b}"`, false}, {"single quoted", `echo '$x'`, false}, {"ansi-c quoted", `echo $'a\'$x'`, false}, {"escaped dollar", `echo \$x`, false}, {"special parameters", `echo $# $? $$ $! $-`, false}, {"comment", `echo ok # $x`, false}, {"comment line", "# the repo's own\necho \"$x\"", false}, {"quoted heredoc", "cat <<'EOF'\n$(id) $x\nEOF", false}, {"unquoted", `echo $x`, true}, {"unquoted braces", `echo ${x}`, true}, {"positional", `rm $1`, true}, {"all arguments", `f $@`, true}, {"command substitution in quotes", `echo "$(ls $dir)"`, true}, {"command substitution in arithmetic", `echo $(( $(wc -l <$f) + 1 ))`, true}, {"backticks in quotes", "echo \"`ls $dir`\"", true}, {"after quoted arithmetic", `printf "%$(($1 - 1))s" $x`, true}, } { lines, ok := splitLines([]byte(tc.src)) if !ok { t.Errorf("%s: not read", tc.name) continue } if lines[1] != tc.want { t.Errorf("%s: split = %v, want %v", tc.name, lines[1], tc.want) } } } func TestSplitLinesAcrossLines(t *testing.T) { src := "a=\"$(($n\n+ 1))\"\necho \\\n $x\ncat <