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) }, } 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()) }, } 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) } }