Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Save output to file 01 #27

Merged
merged 9 commits into from
Feb 26, 2024
Prev Previous commit
Next Next commit
add a simple test to the code fuction
  • Loading branch information
aguzmans committed Feb 26, 2024
commit 1e7685d805f41b6953d2bf4dda5e2c3394afdc48
31 changes: 31 additions & 0 deletions tools/file-works_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package tools

import (
"log"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -74,3 +76,32 @@ func TestIsValidPath(t *testing.T) {
}
}
}

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

// Call logOutput to redirect output to the temporary file.
deferredFunc := LogOutput(tmpfile.Name())

// Send some output to the log.
log.Print("Test log message")

// Close the log output.
deferredFunc()

// 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)
}

// 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")
}
}
Loading