Skip to content

Commit

Permalink
Pipe tests
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Dec 18, 2024
1 parent 2cb82bc commit 6c3b51d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 9 deletions.
65 changes: 64 additions & 1 deletion ft/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,69 @@ func TestVerifyInput(t *testing.T) {
}

func TestPipeArgs(t *testing.T) {
// TODO
tests := []struct {
name string
initialArgs []string
input string
expected []string
}{
{
name: "1. Pipe key name to args.",
initialArgs: []string{"ft"},
input: "keyname",
expected: []string{"ft", "keyname"},
},
{
name: "2. Pipe in multiple args.",
initialArgs: []string{"ft"},
input: "-set keyname",
expected: []string{"ft", "-set", "keyname"},
},
}
for _, tt := range tests {
t.Log(tt.name)

r, w, err := os.Pipe()
if err != nil {
fmt.Println(tt.name)
t.Error("Error establishing pipe.")
}
stdin := os.Stdin
os.Stdin = r

defer func() {
os.Stdin = stdin
r.Close()
w.Close()
}()

_, err = io.WriteString(w, tt.input)
if err != nil {
t.Errorf("WriteString failed: %v", err)
}
w.Close()

args := tt.initialArgs
err = PipeArgs(&args)
if err != nil {
fmt.Println(tt.name)
t.Error("PipeArgs produced an error: ", err)
}

equal := true
if len(args) == len(tt.expected) {
for i, _ := range args {
if args[i] == tt.expected[i] {
continue
}
equal = false
break
}
if equal {
continue
}
}
t.Errorf("Expected: %q length %v, got: %q length %v", tt.expected, len(tt.expected), args, len(args))
}

}
41 changes: 33 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ func TestMainFunc(t *testing.T) {

// tests
tests := []struct {
name string
args []string
expected string
wantErr bool
name string
args []string
pipedInput string
expected string
wantErr bool
}{
{
name: "1. Check help command.",
Expand Down Expand Up @@ -132,10 +133,13 @@ func TestMainFunc(t *testing.T) {
expected: fmt.Sprintf("%v\n", cdpathtest),
wantErr: false,
},

// TODO
// - add test where args are piped to ft
// - this requires mocking stdin
{
name: "8. Check command args piped to ft.",
args: []string{"ft"},
pipedInput: "key",
expected: fmt.Sprintf("%v\n", tmpdir),
wantErr: false,
},

// {
// []string{"ft", "rn", "key", "key2"},
Expand Down Expand Up @@ -183,6 +187,27 @@ func TestMainFunc(t *testing.T) {

}()

// Mock Stdin
stdinReader, stdinWriter, err := os.Pipe()
if err != nil {
fmt.Println(tt.name)
t.Error("Error establishing pipe.")
}
stdin := os.Stdin
os.Stdin = stdinReader

defer func() {
os.Stdin = stdin
stdinReader.Close()
stdinWriter.Close()
}()

_, err = io.WriteString(stdinWriter, tt.pipedInput)
if err != nil {
t.Errorf("WriteString failed: %v", err)
}
stdinWriter.Close()

main()

// Capture queue contents for comparison against expected output
Expand Down

0 comments on commit 6c3b51d

Please sign in to comment.