-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_test.go
171 lines (153 loc) · 3.83 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/osteensco/fastTravelCLI/ft"
)
func TestMainFunc(t *testing.T) {
// Create a temp dir to run tests in
tmpdir, err := os.MkdirTemp("", "testdata")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
tmpdir = strings.Trim(tmpdir, " ")
defer os.RemoveAll(tmpdir)
// Create a dummy binary file
exePath := filepath.Join(tmpdir, "fastTravel.bin")
file, err := os.Create(exePath)
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer file.Close()
// Move working dir to tempdir
oldGetwd, err := os.Getwd()
if err != nil {
t.Fatalf("Unable to establish working directory")
}
defer func() {
err = os.Chdir(oldGetwd)
if err != nil {
t.Fatalf("Failed to navigate to original working directory")
}
}()
err = os.Chdir(tmpdir)
if err != nil {
t.Fatalf("Failed to navigate to temp directory")
}
tests := []struct {
name string
args []string
expected string
wantErr bool
}{
{
name: "1. Check help command.",
args: []string{"ft", "-help"},
expected: fmt.Sprintf(
"\n-help: %s\n-ls: %s\n-rm: %s\n-rn: %s\n-set: %s\n[: %s\n]: %s\nkey: %s\n\n",
ft.CmdDesc["-help"],
ft.CmdDesc["-ls"],
ft.CmdDesc["-rm"],
ft.CmdDesc["-rn"],
ft.CmdDesc["-set"],
ft.CmdDesc["["],
ft.CmdDesc["]"],
ft.CmdDesc["key"],
),
},
{
name: "2. Check set command.",
args: []string{"ft", "-set", "key"},
expected: fmt.Sprintf("Added destination 'key': '%s' \n", tmpdir),
wantErr: false,
},
{
name: "3. Check cd command.",
args: []string{"ft", "key"},
expected: fmt.Sprintf("%v\n", tmpdir),
wantErr: false,
},
{
name: "4. Check cd command with bad key.",
args: []string{"ft", "badkey"},
expected: "Did not recognize key 'badkey', use 'ft -ls' to see all saved destinations. If this is a relative path use './badkey' or 'badkey/'. \n",
wantErr: false,
},
{
name: "5. Check ls command.",
args: []string{"ft", "-ls"},
expected: fmt.Sprintf("\nkey: %v\n\n", tmpdir),
wantErr: false,
},
{
name: "6. Check navigate stack.",
args: []string{"ft", "["},
expected: "[\n",
wantErr: false,
},
// {
// []string{"ft", "rn", "key", "key2"},
// "key renamed to key2",
// },
// {
// []string{"ft", "rm", "key2"},
// "Removed 'key2' destination",
// },
}
for _, tt := range tests {
// Create pipe for capturing output
stdout := os.Stdout
stderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe for testing main()")
}
er, ew, err := os.Pipe()
if err != nil {
t.Fatalf("Failed to create pipe for error testing main()")
}
os.Stdout = w
os.Stderr = ew
os.Args = tt.args
// Move output to a buffer so it can be passed to a queue
// Go routine and chan so data transfer doesn't block
outChan := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
outChan <- buf.String()
}()
errOutChan := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, er)
errOutChan <- buf.String()
}()
main()
// Capture queue contents for comparison against expected output
w.Close()
if err != nil {
t.Fatal("Failed to close Stdout pipe.")
}
ew.Close()
if err != nil {
t.Fatal("Failed to close Stderr pipe.")
}
os.Stdout = stdout
os.Stderr = stderr
actual := <-outChan
errActual := <-errOutChan
if tt.wantErr && errActual == "" {
fmt.Println(tt.name)
t.Errorf("-> ARGS: %v\nExpected Error\n____________\nGot -> %v", tt.args, actual)
} else if actual != tt.expected {
fmt.Println(tt.name)
t.Errorf("-> ARGS: %v\nExpected -> %q\n____________\nGot -> %q", tt.args, tt.expected, actual)
}
}
}