forked from projectdiscovery/gozero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource_test.go
More file actions
96 lines (84 loc) · 2.47 KB
/
source_test.go
File metadata and controls
96 lines (84 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package gozero
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
)
func TestNewSourceWithFile(t *testing.T) {
tempFile, err := os.CreateTemp("", "testsource")
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer func() {
// clean up
_ = os.Remove(tempFile.Name())
}()
content := []byte("temporary file's content")
if _, err := tempFile.Write(content); err != nil {
t.Fatalf("Failed to write to temporary file: %v", err)
}
if err := tempFile.Close(); err != nil {
t.Fatalf("Failed to close temporary file: %v", err)
}
source, err := NewSourceWithFile(tempFile.Name())
if err != nil {
t.Fatalf("Failed to create new source with file: %v", err)
}
if source == nil {
t.Fatal("NewSourceWithFile returned nil source")
return
}
if source.Filename != tempFile.Name() {
t.Errorf("Expected filename to be %v, got %v", tempFile.Name(), source.Filename)
}
if source.File == nil {
t.Error("Expected non-nil File in new source")
}
if source.Temporary {
t.Error("Expected new source not to be temporary")
}
readContent, err := os.ReadFile(source.Filename)
if err != nil {
t.Fatalf("Failed to read from source file: %v", err)
}
if !bytes.Equal(content, readContent) {
t.Errorf("Read content does not match written content")
}
// Clean up
if err := source.Cleanup(); err != nil {
t.Errorf("Failed to cleanup new source with file: %v", err)
}
}
func TestNewSourceWithReader(t *testing.T) {
content := []byte("content from reader")
buffer := bytes.NewBuffer(content)
pattern := "testsource-*"
tempDir := t.TempDir()
source, err := NewSourceWithReader(buffer, pattern, tempDir)
if err != nil {
t.Fatalf("Failed to create new source with reader: %v", err)
}
defer func() {
if err := source.Cleanup(); err != nil {
t.Errorf("Failed to cleanup new source with reader: %v", err)
}
}()
if !source.Temporary {
t.Error("Expected source to be marked as temporary")
}
if !strings.HasPrefix(filepath.Base(source.Filename), "testsource-") {
t.Errorf("Expected file to have prefix 'testsource-', got %s", filepath.Base(source.Filename))
}
if !strings.Contains(source.Filename, tempDir) {
t.Errorf("Expected file to be created in directory %s, got %s", tempDir, filepath.Dir(source.Filename))
}
readContent, err := os.ReadFile(source.Filename)
if err != nil {
t.Fatalf("Failed to read from source file: %v", err)
}
if !bytes.Equal(content, readContent) {
t.Errorf("Read content does not match content from reader")
}
}