Skip to content

Commit

Permalink
resvole review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Umang01-hash committed Nov 7, 2024
1 parent da7c876 commit c3bbc8e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
28 changes: 12 additions & 16 deletions examples/using-add-filestore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,12 @@ func lsCommandHandler(c *gofr.Context) (interface{}, error) {
path := c.Param("path")

files, err := c.File.ReadDir(path)

if err != nil {
fmt.Println(err)
} else {
printFiles(files)
return nil, err
}

printFiles(files)

return "", err
}

Expand All @@ -60,37 +59,34 @@ func grepCommandHandler(c *gofr.Context) (interface{}, error) {
files, err := c.File.ReadDir(path)

if err != nil {
fmt.Println(err)
} else {
grepFiles(files, keyword)

return nil, err
}

grepFiles(files, keyword)

return "", err
}

func createFileCommandHandler(c *gofr.Context) (interface{}, error) {
fileName := c.Param("filename")

_, err := c.File.Create(fileName)

if err == nil {
return fmt.Sprintln("Successfully created file:", fileName), nil
if err != nil {
return fmt.Sprintln("File Creation error"), err
}

return fmt.Sprintln("File Creation error"), err
return fmt.Sprintln("Successfully created file:", fileName), nil
}

func rmCommandHandler(c *gofr.Context) (interface{}, error) {
fileName := c.Param("filename")

err := c.File.Remove(fileName)

if err == nil {
return fmt.Sprintln("Successfully removed file:", fileName), nil
if err != nil {
return fmt.Sprintln("File removal error"), err
}

return fmt.Sprintln("File removal error"), err
return fmt.Sprintln("Successfully removed file:", fileName), nil
}

// This can be a common function to configure both FTP and SFTP server.
Expand Down
28 changes: 25 additions & 3 deletions examples/using-add-filestore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/cmd"
"gofr.dev/pkg/gofr/container"
Expand All @@ -28,7 +30,11 @@ func (mockFileInfo) IsDir() bool { return false }
func (mockFileInfo) Sys() interface{} { return nil }

func getContext(request gofr.Request, fileMock file.FileSystem) *gofr.Context {
return &gofr.Context{Context: context.Background(), Request: request, Container: &container.Container{File: fileMock}}
return &gofr.Context{
Context: context.Background(),
Request: request,
Container: &container.Container{File: fileMock},
}
}

func TestPwdCommandHandler(t *testing.T) {
Expand All @@ -49,6 +55,11 @@ func TestPwdCommandHandler(t *testing.T) {
}

func TestLSCommandHandler(t *testing.T) {
var (
res any
err error
)

path := "/"

logs := testutil.StdoutOutputForFunc(func() {
Expand All @@ -62,22 +73,30 @@ func TestLSCommandHandler(t *testing.T) {
mockFileInfo{name: "file1.txt"},
mockFileInfo{name: "file2.txt"},
}

return files, nil
})

r := cmd.NewRequest([]string{"command", "ls", "-path=/"})

ctx := getContext(r, mock)

lsCommandHandler(ctx)
res, err = lsCommandHandler(ctx)
})

require.NoError(t, err)
assert.Equal(t, "", res)
assert.Contains(t, logs, "file1.txt", "Test failed")
assert.Contains(t, logs, "file2.txt", "Test failed")
assert.NotContains(t, logs, "file3.txt", "Test failed")
}

func TestGrepCommandHandler(t *testing.T) {
var (
res any
err error
)

path := "/"

logs := testutil.StdoutOutputForFunc(func() {
Expand All @@ -91,15 +110,18 @@ func TestGrepCommandHandler(t *testing.T) {
mockFileInfo{name: "file1.txt"},
mockFileInfo{name: "file2.txt"},
}

return files, nil
})

r := cmd.NewRequest([]string{"command", "grep", "-keyword=fi", fmt.Sprintf("-path=%s", path)})
ctx := getContext(r, mock)

grepCommandHandler(ctx)
res, err = grepCommandHandler(ctx)
})

require.NoError(t, err)
assert.Equal(t, "", res)
assert.Contains(t, logs, "file1.txt", "Test failed")
assert.Contains(t, logs, "file2.txt", "Test failed")
assert.NotContains(t, logs, "file3.txt", "Test failed")
Expand Down

0 comments on commit c3bbc8e

Please sign in to comment.