Skip to content

Commit

Permalink
Improve test
Browse files Browse the repository at this point in the history
  • Loading branch information
Agent-Hellboy committed Nov 30, 2024
1 parent 998cb54 commit 36ddbe9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions tests/commands_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package shell_test

import (
"bytes"
"fmsh/commands"
"io"
"os"
"strings"
"testing"
)

func TestHandleFind(t *testing.T) {
// Redirect output for testing
var output bytes.Buffer
originalStdout := os.Stdout
defer func() { os.Stdout = originalStdout }()

// Create a pipe to capture output
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe: %v", err)
}

os.Stdout = w

// Capture the output in a separate goroutine
done := make(chan error)
go func() {
_, err := io.Copy(&output, r)
r.Close() // Close the read end after copying
done <- err
}()

// Perform the action to test
args := []string{"./test_directory", "file.txt"}
commands.HandleFind(args)

// Close the write end of the pipe
w.Close()

// Wait for the output capture to finish
if err := <-done; err != nil {
t.Fatalf("Failed to copy output: %v", err)
}

// Validate the captured output
result := output.String()
if !strings.Contains(result, "file.txt") {
t.Errorf("Expected 'file.txt' in output, but got: %s", result)
}
}
2 changes: 1 addition & 1 deletion tests/shell_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fmsh_test
package shell_test

import (
"fmsh/commands"
Expand Down

0 comments on commit 36ddbe9

Please sign in to comment.