-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
76 lines (59 loc) · 1.93 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
// main_test.go
package main
import (
"bytes"
"os"
"strings"
"testing"
"github.com/sixafter/nanoid-cli/cmd"
"github.com/stretchr/testify/assert"
)
func TestRun_GenerateCommand(t *testing.T) {
//t.Parallel()
is := assert.New(t)
// Set command-line arguments to simulate "generate" command with count 1
os.Args = []string{"nanoid", "generate", "--count", "1"}
// Capture output
var outBuf bytes.Buffer
cmd.RootCmd.SetOut(&outBuf)
cmd.RootCmd.SetErr(&outBuf)
// Execute Run and check for no errors
err := run()
is.NoError(err, "Expected no error on run with generate command")
// Check if output contains one NanoID of default length
output := strings.TrimSpace(outBuf.String())
is.Equal(21, len(output), "Expected single NanoID of default length 21")
}
func TestRun_VersionCommand(t *testing.T) {
//t.Parallel()
is := assert.New(t)
// Set command-line arguments to simulate "version" command
os.Args = []string{"nanoid", "version"}
// Capture output
var outBuf bytes.Buffer
cmd.RootCmd.SetOut(&outBuf)
cmd.RootCmd.SetErr(&outBuf)
// Execute Run and check for no errors
err := run()
is.NoError(err, "Expected no error on run with version command")
// Check if output contains "version" and "commit" information
output := strings.TrimSpace(outBuf.String())
is.Contains(output, "version:", "Expected version information in output")
is.Contains(output, "commit:", "Expected commit information in output")
}
func TestRun_InvalidCommand(t *testing.T) {
//t.Parallel()
is := assert.New(t)
// Set command-line arguments to an invalid command
os.Args = []string{"nanoid", "invalidcmd"}
// Capture output
var outBuf bytes.Buffer
cmd.RootCmd.SetOut(&outBuf)
cmd.RootCmd.SetErr(&outBuf)
// Execute Run and check for an error
err := run()
is.Error(err, "Expected an error on run with invalid command")
// Verify error message
output := outBuf.String()
is.Contains(output, "unknown command", "Expected unknown command error")
}