-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlist_cmd_test.go
71 lines (58 loc) · 1.55 KB
/
list_cmd_test.go
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
package main
import (
"bytes"
"flag"
"os"
"strings"
"testing"
"github.com/skx/rss2email/configfile"
)
// TestList confirms that listing the feed-list works as expected
func TestList(t *testing.T) {
bak := out
out = &bytes.Buffer{}
defer func() { out = bak }()
// Create an instance of the command, and setup a default
// configuration file
content := `# Comment here
https://example.org/
https://example.net/index.rss
- foo: bar
`
data := []byte(content)
tmpfile, err := os.CreateTemp("", "example")
if err != nil {
t.Fatalf("Error creating temporary file")
}
if _, err := tmpfile.Write(data); err != nil {
t.Fatalf("Error writing to config file")
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Error creating temporary file")
}
list := listCmd{}
flags := flag.NewFlagSet("test", flag.ContinueOnError)
list.Arguments(flags)
config := configfile.NewWithPath(tmpfile.Name())
list.config = config
ret := list.Execute([]string{})
if ret != 0 {
t.Fatalf("unexpected error running list")
}
output := out.(*bytes.Buffer).String()
// We should have two URLs
if !strings.Contains(output, "https://example.org/") {
t.Errorf("List didn't contain expected output")
}
if !strings.Contains(output, "https://example.net/index.rss") {
t.Errorf("List didn't contain expected output")
}
// We should not have comments, or parameters
if strings.Contains(output, "foo") {
t.Errorf("We found a parameter we didn't expect")
}
if strings.Contains(output, "#") {
t.Errorf("We found a comment we didn't expect")
}
os.Remove(tmpfile.Name())
}