forked from gmr/env-aws-params
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
87 lines (71 loc) · 1.82 KB
/
main_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"errors"
"flag"
"io/ioutil"
"testing"
"github.com/urfave/cli"
)
func NewContext(t *testing.T, testArgs []string) *cli.Context {
app := cli.NewApp()
app.Writer = ioutil.Discard
app.Flags = cliFlags()
set := flag.NewFlagSet("test", 0)
for _, f := range app.Flags {
f.Apply(set)
}
set.Parse(testArgs)
return cli.NewContext(app, set, nil)
}
func TestMissingPrefix(t *testing.T) {
var testArgs []string
testArgs = []string{"--upcase"}
code, err := validateArgs(NewContext(t, testArgs))
if code != 1 {
t.Fatalf("expected code to be 1, got %v", code)
}
if err == nil {
t.Fatalf("expected err to be set, got nil")
}
}
func TestMissingCommand(t *testing.T) {
var testArgs []string
testArgs = []string{"--prefix", "/foo"}
code, err := validateArgs(NewContext(t, testArgs))
if code != 2 {
t.Fatalf("expected code to be 2, got %v", code)
}
if err == nil {
t.Fatalf("expected err to be set, got nil")
}
}
func TestMissingStripAndSanitize(t *testing.T) {
var testArgs []string
testArgs = []string{"--prefix", "/foo", "--strip", "--sanitize", "/bin/bash"}
code, err := validateArgs(NewContext(t, testArgs))
if code != 3 {
t.Fatalf("expected code to be 3, got %v", code)
}
if err == nil {
t.Fatalf("expected err to be set, got nil")
}
}
func TestValidCLIOptions(t *testing.T) {
var testArgs []string
testArgs = []string{"--prefix", "/foo", "--strip", "/bin/bash"}
code, err := validateArgs(NewContext(t, testArgs))
if code != 0 {
t.Fatalf("expected code to be 0, got %v", code)
}
if err != nil {
t.Fatalf("expected err to be nil, got %v", code)
}
}
func TestErrorPrefix(t *testing.T) {
testError := errors.New("foo bar")
result := errorPrefix(testError)
expectation := "ERROR: foo bar"
if result != expectation {
t.Fatalf("expected \"%v\", got \"%v\"", result, expectation)
}
}