From e5bd4a44e7a2d503883c5af4828cdd82564c418b Mon Sep 17 00:00:00 2001 From: WheeskyJack <32392032+WheeskyJack@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:47:08 +0530 Subject: [PATCH 1/3] Update pwd cmd to use io.Writer from Cmd. use io.Writer from pwd cmd so that it can default to stdout and can be set to other writer in unit test to capture the print. --- internal/core/commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/core/commands.go b/internal/core/commands.go index 6b55577..9f83d16 100644 --- a/internal/core/commands.go +++ b/internal/core/commands.go @@ -14,7 +14,7 @@ var PwdCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { dir, err := os.Getwd() checkError(err, "getting current working directory") - fmt.Println(dir) + fmt.Fprintln(cmd.OutOrStdout(), dir) }, } From 3b2fcb1ab4bf1213b78850a1ba47d667cbea11e0 Mon Sep 17 00:00:00 2001 From: WheeskyJack <32392032+WheeskyJack@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:48:29 +0530 Subject: [PATCH 2/3] Update ls cmd to use io.Writer from Cmd use io.Writer from ls cmd so that it can default to stdout and can be set to other writer in unit test to capture the print. --- internal/core/ls_commands.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/core/ls_commands.go b/internal/core/ls_commands.go index 16dde8a..253268b 100644 --- a/internal/core/ls_commands.go +++ b/internal/core/ls_commands.go @@ -2,13 +2,15 @@ package core import ( "fmt" - "github.com/dustin/go-humanize" - "github.com/spf13/cobra" + "io" "log" "os" "sort" "syscall" "time" + + "github.com/dustin/go-humanize" + "github.com/spf13/cobra" ) type FileTimeStruct struct { @@ -21,7 +23,7 @@ func checkError(err error, context string) { log.Fatalf("Error %s: %v", context, err) } } -func listFiles(dir string, showHidden bool, appendSlashToDir bool, sortByTime bool, listInode bool, humanReadable bool) { +func listFiles(dir string, showHidden bool, appendSlashToDir bool, sortByTime bool, listInode bool, humanReadable bool, out io.Writer) { files, err := os.ReadDir(dir) checkError(err, "reading directory") @@ -36,7 +38,7 @@ func listFiles(dir string, showHidden bool, appendSlashToDir bool, sortByTime bo return fileTimes[i].Ftime.Before(fileTimes[j].Ftime) }) for _, file := range fileTimes { - fmt.Println(file.Fname) + fmt.Fprintln(out, file.Fname) } return } @@ -52,7 +54,7 @@ func listFiles(dir string, showHidden bool, appendSlashToDir bool, sortByTime bo info, err := os.Stat(file.Name()) checkError(err, "getting file stat") stat := info.Sys().(*syscall.Stat_t) - fmt.Printf("%d ", stat.Ino) + fmt.Fprintf(out, "%d ", stat.Ino) } if appendSlashToDir && file.IsDir() { @@ -61,9 +63,9 @@ func listFiles(dir string, showHidden bool, appendSlashToDir bool, sortByTime bo if humanReadable { info, err := os.Stat(file.Name()) checkError(err, "getting file info") - fmt.Printf("%s %s\n", humanize.Bytes(uint64(info.Size())), name) + fmt.Fprintf(out, "%s %s\n", humanize.Bytes(uint64(info.Size())), name) } else { - fmt.Println(name) + fmt.Fprintln(out, name) } } } @@ -79,7 +81,7 @@ var LsCmd = &cobra.Command{ listInode, _ := cmd.Flags().GetBool("i") humanReadable, _ := cmd.Flags().GetBool("V") - listFiles(dir, showHidden, appendSlashToDir, sortByTime, listInode, humanReadable) + listFiles(dir, showHidden, appendSlashToDir, sortByTime, listInode, humanReadable, cmd.OutOrStdout()) }, } From e33b916b3995a9b4f483bd139283ff62b12950cc Mon Sep 17 00:00:00 2001 From: WheeskyJack <32392032+WheeskyJack@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:49:57 +0530 Subject: [PATCH 3/3] Update test function with cmd.SetOut to capture the output Update test function with cmd.SetOut to capture the output and fix failing tests --- tests/unit/goshell_test.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/unit/goshell_test.go b/tests/unit/goshell_test.go index 678e4f8..9f94cd5 100644 --- a/tests/unit/goshell_test.go +++ b/tests/unit/goshell_test.go @@ -1,21 +1,27 @@ package unit import ( - "github.com/IshaanNene/GoShell/internal/core" - "github.com/spf13/cobra" + "bytes" "os" "path/filepath" "testing" + + "github.com/IshaanNene/GoShell/internal/core" + "github.com/spf13/cobra" ) // Helper function to execute a Cobra command with arguments and return the output func executeCommand(cmd *cobra.Command, args ...string) (string, error) { cmd.SetArgs(args) - output, err := cmd.ExecuteC() + // Capture the output + var buf bytes.Buffer + cmd.SetOut(&buf) + + _, err := cmd.ExecuteC() if err != nil { return "", err } - return output.UsageString(), nil + return buf.String(), nil } func TestTouchCommand(t *testing.T) { @@ -37,16 +43,24 @@ func TestTouchCommand(t *testing.T) { func TestLsCommand(t *testing.T) { // Setup: Create some files to list - file1 := "file1.txt" - file2 := "file2.txt" - os.Create(file1) - os.Create(file2) + + // Create a temporary directory for testing + dir := t.TempDir() + + file1 := dir + "/" + "file1.txt" + file2 := dir + "/" + "file2.txt" + + // Create some test files and directories + os.WriteFile(file1, []byte("content1"), 0644) + os.WriteFile(file2, []byte("content2"), 0644) + defer os.Remove(file1) defer os.Remove(file2) // Expected output should be the filenames listed expected := "file1.txt\nfile2.txt\n" + core.LsCmd.Flags().Set("directory", dir) // Call the ls command got, err := executeCommand(core.LsCmd) if err != nil { @@ -72,7 +86,7 @@ func TestPwdCommand(t *testing.T) { } // Check if the output matches the expected directory - if got != expected { + if got != expected+"\n" { t.Errorf("Expected %q but got %q", expected, got) } }