-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
90 lines (73 loc) · 3.1 KB
/
cli_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
88
89
90
package main
import (
"regexp"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_parseCliArgs(t *testing.T) {
t.Run("it prints the help if requested", func(t *testing.T) {
_, stdErr := newMockWriters()
_, exitCode := parseCliArgs(stdErr, []string{"app", "-h"})
assert.NotNil(t, exitCode)
assert.Equal(t, 0, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Contains(t, stdErr.Content(), "-v")
assert.Contains(t, stdErr.Content(), "-s")
})
t.Run("it returns an error if both arguments are missing", func(t *testing.T) {
_, stdErr := newMockWriters()
_, exitCode := parseCliArgs(stdErr, []string{"app"})
assert.NotNil(t, exitCode)
assert.Equal(t, 1, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Contains(t, stdErr.Content(), "archive-file missing")
})
t.Run("it returns an error if the archive file is missing", func(t *testing.T) {
_, stdErr := newMockWriters()
_, exitCode := parseCliArgs(stdErr, []string{"app", "archive.tar.gz"})
assert.NotNil(t, exitCode)
assert.Equal(t, 1, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Contains(t, stdErr.Content(), "source-dir missing")
})
t.Run("it returns an error if the source directory does not exists", func(t *testing.T) {
_, stdErr := newMockWriters()
_, exitCode := parseCliArgs(stdErr, []string{"app", "test.tar.gz", "non_existing"})
assert.NotNil(t, exitCode)
assert.Equal(t, 1, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Regexp(t, regexp.MustCompile("Source directory \\S+ does not exists"), stdErr.Content())
})
t.Run("it returns an error if the source directory is not a directory", func(t *testing.T) {
_, stdErr := newMockWriters()
_, filename, _, ok := runtime.Caller(0)
require.True(t, ok)
_, exitCode := parseCliArgs(stdErr, []string{"app", "test.tar.gz", filename})
assert.NotNil(t, exitCode)
assert.Equal(t, 1, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Regexp(t, regexp.MustCompile("Source directory \\S+ is not a directory"), stdErr.Content())
})
t.Run("it returns an error if max size has an invalid format", func(t *testing.T) {
_, stdErr := newMockWriters()
_, exitCode := parseCliArgs(stdErr, []string{"app", "-s", "foo", "test.tar.gz", testDir})
assert.NotNil(t, exitCode)
assert.Equal(t, 1, *exitCode)
assert.Contains(t, stdErr.Content(), "usage: app [-sv] archive-file source-dir")
assert.Contains(t, stdErr.Content(), "Invalid format for max size")
})
t.Run("it correctly parse the args", func(t *testing.T) {
_, stdErr := newMockWriters()
parsedArgs, exitCode := parseCliArgs(stdErr, []string{"app", "-s", "5.2MB", "-v", "test.tar.gz", testDir})
assert.Nil(t, exitCode)
assert.Empty(t, stdErr.Content())
assert.Equal(t, Args{
SourceDir: testDir,
ArchiveFile: "test.tar.gz",
MaxSize: 5200000,
Verbose: true,
}, parsedArgs)
})
}