mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-01-26 03:24:05 +00:00
55 lines
998 B
Go
55 lines
998 B
Go
package fileproc
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestProcessFile(t *testing.T) {
|
|
// Create a temporary file with known content.
|
|
tmpFile, err := os.CreateTemp("", "testfile")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func(name string) {
|
|
err := os.Remove(name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}(tmpFile.Name())
|
|
|
|
content := "Test content"
|
|
if _, err := tmpFile.WriteString(content); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
errTmpFile := tmpFile.Close()
|
|
if errTmpFile != nil {
|
|
t.Fatal(errTmpFile)
|
|
return
|
|
}
|
|
|
|
ch := make(chan WriteRequest, 1)
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
ProcessFile(tmpFile.Name(), ch, "")
|
|
}()
|
|
wg.Wait()
|
|
close(ch)
|
|
|
|
var result string
|
|
for req := range ch {
|
|
result = req.Content
|
|
}
|
|
|
|
if !strings.Contains(result, tmpFile.Name()) {
|
|
t.Errorf("Output does not contain file path: %s", tmpFile.Name())
|
|
}
|
|
if !strings.Contains(result, content) {
|
|
t.Errorf("Output does not contain file content: %s", content)
|
|
}
|
|
}
|