Skip to content

Commit

Permalink
Merge pull request #37 from osteensco/pipe
Browse files Browse the repository at this point in the history
Pipe
  • Loading branch information
osteensco authored Dec 18, 2024
2 parents 6bb15c7 + 6c3b51d commit 9d67ae6
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 4 deletions.
19 changes: 19 additions & 0 deletions ft/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ft

import (
"bufio"
"errors"
"fmt"
"os"
"sort"
"strings"
)
Expand Down Expand Up @@ -38,3 +40,20 @@ func printMap(hashmap map[string]string) {
fmt.Println("")

}

func PipeArgs(args *[]string) error {

// read from stdin
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
// append to args
*args = append(*args, scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}

return nil

}
68 changes: 68 additions & 0 deletions ft/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,71 @@ func TestVerifyInput(t *testing.T) {
}

}

func TestPipeArgs(t *testing.T) {
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))
}

}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ func main() {
return
}

// handle piped args
err = ft.PipeArgs(&os.Args)
if err != nil {
fmt.Println("Error: ", err)
return
}

// sanitize user input
inputCommand, err := ft.PassCmd(os.Args)
if err != nil {
Expand Down
38 changes: 34 additions & 4 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,6 +133,14 @@ func TestMainFunc(t *testing.T) {
expected: fmt.Sprintf("%v\n", cdpathtest),
wantErr: false,
},
{
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"},
// "key renamed to key2",
Expand Down Expand Up @@ -178,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 9d67ae6

Please sign in to comment.