Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/core/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down
18 changes: 10 additions & 8 deletions internal/core/ls_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")

Expand All @@ -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
}
Expand All @@ -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() {
Expand All @@ -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)
}
}
}
Expand All @@ -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())
},
}

Expand Down
32 changes: 23 additions & 9 deletions tests/unit/goshell_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down