Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/shell/cmds/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ func CmdHead(ctx CmdContext, args []string, w io.Writer, errW io.Writer, stdin i
byteCount := -1
var files []string
for i := 0; i < len(args); i++ {
if args[i] == "-n" && i+1 < len(args) {
fmt.Sscanf(args[i+1], "%d", &n)
if args[i] == "-n" {
if i+1 >= len(args) || args[i+1] == "" {
fmt.Fprintln(errW, "head: option requires an argument -- 'n'")
return 1
}
count, err := strconv.Atoi(args[i+1])
if err != nil || count < 0 {
fmt.Fprintf(errW, "head: invalid number of lines: '%s'\n", args[i+1])
return 1
}
n = count
byteCount = -1
i++
} else if args[i] == "-c" {
Expand Down
49 changes: 45 additions & 4 deletions pkg/shell/cmds/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,51 @@ import (
)

func TestHead(t *testing.T) {
out, _, _ := execCmd(t, testFS(), "head -n 2 /docs/readme.md")
lines := strings.Split(strings.TrimRight(out, "\n"), "\n")
if len(lines) != 2 {
t.Errorf("head -n 2: got %d lines, want 2", len(lines))
for _, tt := range []struct {
cmd string
want string
}{
{cmd: "head -n 2 /docs/readme.md", want: "# Hello World\nThis is a test file.\n"},
{cmd: "seq 1 15 | head -n 3", want: "1\n2\n3\n"},
{cmd: "seq 1 15 | head -c 2 -n +3", want: "1\n2\n3\n"},
} {
t.Run(tt.cmd, func(t *testing.T) {
out, errOut, code := execCmd(t, testFS(), tt.cmd)
if code != 0 || errOut != "" || out != tt.want {
t.Errorf("code=%d stdout=%q stderr=%q, want code=0 stdout=%q stderr empty", code, out, errOut, tt.want)
}
})
}
}

func TestHeadTailInvalidLineCount(t *testing.T) {
for _, command := range []string{"head", "tail"} {
for _, tt := range []struct {
args string
want string
}{
{args: "-n", want: "option requires an argument -- 'n'"},
{args: "-n ''", want: "option requires an argument -- 'n'"},
{args: "-n $X", want: "option requires an argument -- 'n'"},
{args: "-n abc", want: "invalid number of lines: 'abc'"},
{args: "-n 3abc", want: "invalid number of lines: '3abc'"},
{args: "-n -1", want: "invalid number of lines: '-1'"},
{args: "-n +", want: "invalid number of lines: '+'"},
{args: "-n +3abc", want: "invalid number of lines: '+3abc'"},
{args: "-n 999999999999999999999999", want: "invalid number of lines: '999999999999999999999999'"},
} {
t.Run(command+" "+tt.args, func(t *testing.T) {
out, errOut, code := execCmd(t, testFS(), "X=; seq 1 15 | "+command+" "+tt.args)
wantErr := command + ": " + tt.want + "\n"
if code != 1 || out != "" || errOut != wantErr {
t.Errorf("code=%d stdout=%q stderr=%q, want code=1 stdout empty stderr=%q", code, out, errOut, wantErr)
}
})
}
out, errOut, code := execCmd(t, testFS(), command+" /docs/readme.md -n abc")
if code != 1 || out != "" || !strings.Contains(errOut, "invalid number of lines") {
t.Errorf("%s file: code=%d stdout=%q stderr=%q", command, code, out, errOut)
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions pkg/shell/cmds/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ func CmdTail(ctx CmdContext, args []string, w io.Writer, errW io.Writer, stdin i
byteCount := -1
var files []string
for i := 0; i < len(args); i++ {
if args[i] == "-n" && i+1 < len(args) {
if args[i] == "-n" {
if i+1 >= len(args) || args[i+1] == "" {
fmt.Fprintln(errW, "tail: option requires an argument -- 'n'")
return 1
}
count, err := strconv.Atoi(args[i+1])
if err != nil || count < 0 {
fmt.Fprintf(errW, "tail: invalid number of lines: '%s'\n", args[i+1])
return 1
}
fromStart = strings.HasPrefix(args[i+1], "+")
fmt.Sscanf(strings.TrimPrefix(args[i+1], "+"), "%d", &n)
n = count
byteCount = -1
i++
} else if args[i] == "-c" {
Expand Down
3 changes: 3 additions & 0 deletions pkg/shell/cmds/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func TestTailLines(t *testing.T) {
{name: "single line", cmd: "printf 'a\\nb\\nc\\n' | tail -1", want: "c\n"},
{name: "from line with option", cmd: "printf 'a\\nb\\nc\\n' | tail -n +2", want: "b\nc\n"},
{name: "from line shorthand", cmd: "printf 'a\\nb\\nc\\n' | tail +2", want: "b\nc\n"},
{name: "line count", cmd: "seq 1 15 | tail -n 3", want: "13\n14\n15\n"},
{name: "from line eleven", cmd: "seq 1 15 | tail -n +11", want: "11\n12\n13\n14\n15\n"},
{name: "line count overrides bytes", cmd: "seq 1 15 | tail -c 2 -n 3", want: "13\n14\n15\n"},
}

for _, tt := range tests {
Expand Down
Loading