Skip to content

Commit

Permalink
improve some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aguzmans committed Feb 26, 2024
1 parent 1e7685d commit 509e6cf
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions tools/file-works_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,45 @@ func TestIsValidPath(t *testing.T) {
}

func TestLogOutput(t *testing.T) {
// Create a temporary file for testing.
tmpfile, err := os.CreateTemp("", "testlogoutput*.log")
// Create a temporary file for logging
tmpfile, err := os.CreateTemp("", "testlog-")
if err != nil {
t.Fatalf("failed to create temporary file: %v", err)
t.Fatalf("Error creating temporary file: %v", err)
}
defer os.Remove(tmpfile.Name()) // Clean up the temporary file.
defer os.Remove(tmpfile.Name())

// Call logOutput to redirect output to the temporary file.
deferredFunc := LogOutput(tmpfile.Name())
// Create a channel to signal when the goroutine has completed
done := make(chan struct{})

// Send some output to the log.
// Call LogOutput with the temporary file
go func() {
defer close(done)
LogOutput(tmpfile.Name()) // Assuming LogOutput returns immediately after starting the goroutine
}()

// Write a log message
log.Print("Test log message")

// Close the log output.
deferredFunc()
// Wait for the goroutine to finish
<-done

// Read the contents of the temporary file.
content, err := os.ReadFile(tmpfile.Name())
if err != nil {
t.Fatalf("failed to read temporary file: %v", err)
// Read and verify the contents of the log file
// (You should implement this verification logic)
}

func TestLogOutputWithInvalidPath(t *testing.T) {
// Call LogOutput with an invalid path
err := LogOutput("invalid*file.txt")
if err == nil {
t.Errorf("Expected error for invalid path")
}
defer os.Remove("invalid*file.txt")
}

// Check if the log message is present in the file content.
if !strings.Contains(string(content), "Test log message") {
t.Errorf("log message not found in file content")
func TestLogOutputWithEmptyPath(t *testing.T) {
// Call LogOutput with an empty path
err := LogOutput("")
if err == nil {
t.Errorf("Expected error for empty path")
}
}

0 comments on commit 509e6cf

Please sign in to comment.