Repository navigation
Expand file tree
/
Copy pathcmd_default_test.go
More file actions
56 lines (50 loc) · 1.7 KB
/
Copy pathcmd_default_test.go
File metadata and controls
56 lines (50 loc) · 1.7 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
package main
import (
"os"
"strings"
"testing"
"github.com/spf13/pflag"
)
// With no address named, the null device gets the help as a terminal does: it
// is the standard input of a service, a `docker run` without -i and nohup,
// none of which handed the command anything. A pipe did, so an empty one is an
// empty input.
func TestBareRunHelpsOnTheNullDevice(t *testing.T) {
null, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = null.Close() })
pipe, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
_ = w.Close()
t.Cleanup(func() { _ = pipe.Close() })
out, err := bareRun(t, null)
if err != nil || !strings.HasPrefix(out, "Usage: ") {
t.Errorf("stdin from %s: printed %q, err %v; want the help", os.DevNull, out, err)
}
out, err = bareRun(t, pipe)
if err == nil || !strings.Contains(err.Error(), "no addresses found in the input") {
t.Errorf("stdin from an empty pipe: printed %q, err %v; want the empty-input error", out, err)
}
}
// bareRun runs the default command with no arguments and stdin from a file, and
// returns what it printed.
func bareRun(t *testing.T, stdin *os.File) (string, error) {
t.Helper()
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("INTERNETDATA_API_KEY", "")
t.Setenv("INTERNETDATA_SESSION", "")
savedArgs, savedFlags, savedStdin, savedConfig := os.Args, pflag.CommandLine, os.Stdin, gConfig
t.Cleanup(func() {
os.Args, pflag.CommandLine, os.Stdin, gConfig = savedArgs, savedFlags, savedStdin, savedConfig
})
os.Args = []string{"internetdata"}
pflag.CommandLine = pflag.NewFlagSet("internetdata", pflag.ContinueOnError)
os.Stdin = stdin
gConfig = NewConfig()
gConfig.CacheEnabled = false
return captureStdout(t, cmdDefault)
}