-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_test.go
More file actions
101 lines (83 loc) · 2.2 KB
/
Copy pathmain_test.go
File metadata and controls
101 lines (83 loc) · 2.2 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
97
98
99
100
101
package main
import (
"os"
"testing"
"github.com/ersinkoc/SimpleDeploy/internal/state"
)
func captureExit(f func()) int {
code := -1
old := osExit
osExit = func(c int) { code = c }
defer func() { osExit = old }()
f()
return code
}
func TestMain_NoArgs(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
oldArgs := os.Args
os.Args = []string{"simpledeploy"}
t.Cleanup(func() { os.Args = oldArgs })
code := captureExit(main)
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
}
func TestMain_Version(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
oldArgs := os.Args
os.Args = []string{"simpledeploy", "version"}
t.Cleanup(func() { os.Args = oldArgs })
code := captureExit(main)
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
}
func TestMain_Error(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
oldArgs := os.Args
os.Args = []string{"simpledeploy", "nonexistent-command"}
t.Cleanup(func() { os.Args = oldArgs })
code := captureExit(main)
if code != 1 {
t.Errorf("expected exit code 1, got %d", code)
}
}
func TestRun_NoArgs(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
code := run([]string{"simpledeploy"})
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
}
func TestRun_Version(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
code := run([]string{"simpledeploy", "version"})
if code != 0 {
t.Errorf("expected exit code 0, got %d", code)
}
}
func TestRun_Error(t *testing.T) {
dir := t.TempDir()
os.Setenv("SIMPLEDEPLOY_DIR", dir)
t.Cleanup(func() { os.Unsetenv("SIMPLEDEPLOY_DIR") })
state.InitState(dir)
code := run([]string{"simpledeploy", "nonexistent-command"})
if code != 1 {
t.Errorf("expected exit code 1, got %d", code)
}
}