Skip to content

Commit

Permalink
main() test improved
Browse files Browse the repository at this point in the history
  • Loading branch information
osteensco committed Aug 9, 2024
1 parent 84f18b0 commit 56dff99
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 61 deletions.
28 changes: 15 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,20 @@ func readMap(file *os.File) map[string]string {

}

func sanitizeDir(path string) string {
distro := os.Getenv("WSL_DISTRO_NAME")
if len(distro) == 0 {

prefix := "/mnt/"
drive := strings.Split(path, ":")[0]
path = strings.Replace(path, drive, strings.ToLower(drive), 1)
path = strings.Replace(path, ":", "", 1)
path = strings.Replace(path, "\\", "/", -1)
path = prefix + path

}
return path
}

func ensureData(filepath string) *os.File {

Expand Down Expand Up @@ -220,19 +233,8 @@ func changeDirectory(data cmdArgs) {
os.Exit(1)
}

path := data.allPaths[data.cmd[1]]
distro := os.Getenv("WSL_DISTRO_NAME")
if len(distro) == 0 {

prefix := "/mnt/"
drive := strings.Split(path, ":")[0]
path = strings.Replace(path, drive, strings.ToLower(drive), 1)
path = strings.Replace(path, ":", "", 1)
path = strings.Replace(path, "\\", "/", -1)
path = prefix + path

}

p := data.allPaths[data.cmd[1]]
path := sanitizeDir(p)
fmt.Println(path)

}
Expand Down
141 changes: 93 additions & 48 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"testing"
)

Expand All @@ -17,7 +18,10 @@ func TestPrintMap(t *testing.T) {

// Redirect stdout to capture the output
old := os.Stdout
r, w, _ := os.Pipe()
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe for testing printMap()")
}
os.Stdout = w

printMap(hashmap)
Expand Down Expand Up @@ -61,11 +65,11 @@ func TestPassCmd(t *testing.T) {
for _, tt := range tests {
got, err := passCmd(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("passCmd() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("passCmd err %v, want err: %v", err, tt.wantErr)
return
}
if !tt.wantErr && !equalSlices(got, tt.want) {
t.Errorf("passCmd() = %v, want %v", got, tt.want)
t.Errorf("passCmd Args: %v\nexpected: %v\ngot:%v\n_________\n", tt.args, tt.want, got)
return
}

Expand Down Expand Up @@ -157,8 +161,12 @@ func TestSetDirectoryVar(t *testing.T) {
file: tmpfile,
}

stdout := os.Stdout
_,w,_ := os.Pipe()
os.Stdout = w
setDirectoryVar(data)
fmt.Println("")
os.Stdout = stdout

expected, _ := os.Getwd()
if data.allPaths["testKey"] != expected {
t.Errorf("Expected key 'testKey' to have value %s, got %s", expected, data.allPaths["testKey"])
Expand Down Expand Up @@ -235,9 +243,12 @@ func TestRemoveKey(t *testing.T) {
},
file: tmpfile,
}

stdout := os.Stdout
_,w,_ := os.Pipe()
os.Stdout = w
removeKey(data)
fmt.Println("")
os.Stdout = stdout
// fmt.Println("")
if _, ok := data.allPaths["key1"]; ok {
t.Errorf("Expected key 'key1' to be removed")
fail = true
Expand Down Expand Up @@ -277,8 +288,12 @@ func TestRenameKey(t *testing.T) {
file: tmpfile,
}

stdout := os.Stdout
_,w,_ := os.Pipe()
os.Stdout = w
renameKey(data)
fmt.Println("")
os.Stdout = stdout

if _, ok := data.allPaths["key1"]; ok {
t.Errorf("Expected key 'key1' to be renamed")
fail = true
Expand Down Expand Up @@ -342,9 +357,10 @@ func TestShowHelp(t *testing.T) {
func TestMainFunc(t *testing.T) {
// Create a temp dir to run tests in
tmpdir, err := os.MkdirTemp("", "testdata")
if err != nil {
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
tmpdir = strings.Trim(tmpdir, " ")
defer os.RemoveAll(tmpdir)

// Create a dummy binary file
Expand All @@ -370,51 +386,80 @@ func TestMainFunc(t *testing.T) {
if err != nil {
t.Fatalf("Failed to navigate to temp directory")
}

tests := []struct {
args []string
expected string
}{
{
[]string{"ft", "help"},
"\nhelp: you are here :) - Usage: ft help\nls: display all current key value pairs - Usage: ft ls\nrm: deletes provided key - Usage: ft rm [key]\nrn: renames key to new key - Usage: ft rn [key] [new key]\nset: set current directory path to provided key - Usage: ft set [key]\nto: change directory to provided key's path - Usage: ft to [key]\n\n",
},
{
[]string{"ft", "set", "key"},
"Added destination key",
},
{
[]string{"ft", "to", "key"},
fmt.Sprintf("%v\n",sanitizeDir(tmpdir)),
},
{
[]string{"ft", "ls"},
fmt.Sprintf("\nkey: %v\n\n", tmpdir),
},
{
[]string{"ft", "rn", "key", "key2"},
"key renamed to key2",
},
{
[]string{"ft", "rm", "key2"},
"Removed 'key2' destination",
},
}


// Create pipe for capturing output
old := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe for testing main()")
for _, tt := range tests {

// Create pipe for capturing output
stdout := os.Stdout
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe for testing main()")
}
os.Stdout = w

os.Args = tt.args
main()

// Move output to a buffer so it can be passed to a queue
// Go routine and chan so data transfer doesn't block
outChan := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

// Capture queue contents for comparison against expected output
w.Close()
os.Stdout = stdout
actual := <- outChan


if actual != tt.expected {
t.Errorf("-> ARGS: %v\nExpected --> %v\n____________\nGot --> %v", tt.args, tt.expected, actual)
}


}
os.Stdout = w

// this should probably be a loop or go routines
os.Args = []string{"ft", "help"}
main()
os.Args = []string{"ft", "set", "key"}
main()
os.Args = []string{"ft", "to", "key"}
main()
os.Args = []string{"ft", "ls"}
main()
os.Args = []string{"ft", "rn", "key", "key2"}
main()
os.Args = []string{"ft", "rm", "key2"}
main()

// Move output to a buffer so it can be passed to a queue
// Go routine and chan so data transfer doesn't block
outChan := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()

}()

// Capture queue contents for comparison against expected output
w.Close()
os.Stdout = old
actual := <- outChan

//stackoverflow.com/questions/7933460/how-do-you-write-multiline-strings-in-go
expected := "\nhelp: you are here :) - Usage: ft help\nls: display all current key value pairs - Usage: ft ls\nrm: deletes provided key - Usage: ft rm [key]\nrn: renames key to new key - Usage: ft rn [key] [new key]\nset: set current directory path to provided key - Usage: ft set [key]\nto: change directory to provided key's path - Usage: ft to [key]\n\n"
if actual != expected {
t.Errorf("Expected --> %s\n____________\nGot --> %s", expected, actual)
} else {
if !t.Failed() {
fmt.Println("main: Success")
}



}


Expand Down

0 comments on commit 56dff99

Please sign in to comment.