File tree Expand file tree Collapse file tree 1 file changed +88
-0
lines changed
Expand file tree Collapse file tree 1 file changed +88
-0
lines changed Original file line number Diff line number Diff line change 11package move
2+
3+ import (
4+ "bytes"
5+ "fmt"
6+ "github.com/digitalghost-dev/poke-cli/styling"
7+ "log"
8+ "os"
9+ "strings"
10+ "testing"
11+ )
12+
13+ func captureMoveOutput (f func ()) string {
14+ r , w , _ := os .Pipe ()
15+ defer func (r * os.File ) {
16+ err := r .Close ()
17+ if err != nil {
18+ log .Fatal (err )
19+ }
20+ }(r )
21+
22+ oldStdout := os .Stdout
23+ os .Stdout = w
24+ defer func () { os .Stdout = oldStdout }()
25+
26+ f ()
27+
28+ err := w .Close ()
29+ if err != nil {
30+ log .Fatal (err )
31+ }
32+
33+ var buf bytes.Buffer
34+ _ , _ = buf .ReadFrom (r )
35+ return buf .String ()
36+ }
37+
38+ func TestMoveCommand (t * testing.T ) {
39+ err := os .Setenv ("GO_TESTING" , "1" )
40+ if err != nil {
41+ log .Fatal (err )
42+ }
43+ defer func () {
44+ err := os .Unsetenv ("GO_TESTING" )
45+ if err != nil {
46+ fmt .Println (err )
47+ }
48+ }()
49+
50+ tests := []struct {
51+ name string
52+ args []string
53+ expectedOutput string
54+ expectedError bool
55+ }{
56+ {
57+ name : "Help flag" ,
58+ args : []string {"move" , "--help" },
59+ expectedOutput : "Get details about a specific move." ,
60+ expectedError : false ,
61+ },
62+ }
63+
64+ for _ , tt := range tests {
65+ t .Run (tt .name , func (t * testing.T ) {
66+ originalArgs := os .Args
67+ defer func () { os .Args = originalArgs }()
68+
69+ os .Args = append ([]string {"poke-cli" }, tt .args ... )
70+
71+ output := captureMoveOutput (func () {
72+ defer func () {
73+ if r := recover (); r != nil {
74+ if ! tt .expectedError {
75+ t .Fatalf ("Unexpected error: %v" , r )
76+ }
77+ }
78+ }()
79+ MoveCommand ()
80+ })
81+
82+ cleanOutput := styling .StripANSI (output )
83+
84+ if ! strings .Contains (cleanOutput , tt .expectedOutput ) {
85+ t .Errorf ("Output mismatch. Expected to contain:\n %s\n Got:\n %s" , tt .expectedOutput , output )
86+ }
87+ })
88+ }
89+ }
You can’t perform that action at this time.
0 commit comments