-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli_integration_test.go
152 lines (112 loc) · 3.41 KB
/
cli_integration_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
package hype_test
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func buildBin(binPath string) {
buildCmd := exec.Command("go", "build", "-o", binPath, "./cmd/hype")
if err := buildCmd.Run(); err != nil {
log.Fatal(err)
}
}
func getProjectWd() string {
_, filename, _, _ := runtime.Caller(0)
return filepath.Dir(filename)
}
func Test_Cli_Error(t *testing.T) {
r := require.New(t)
binPath := filepath.Join(t.TempDir(), "hype")
buildBin(binPath)
root := filepath.Join(getProjectWd(), "testdata", "to_md", "source_code", "broken")
t.Chdir(root)
args := []string{"export", "-format=markdown", "-f", "hype.md"}
t.Logf("Running command: %s %s, root: %s", binPath, strings.Join(args, " "), root)
hypeCmd, stdout, _ := setupCmd(binPath, args)
err := hypeCmd.Run()
r.Error(err)
r.Equal(0, stdout.Len())
var exitErr *exec.ExitError
r.True(errors.As(err, &exitErr), "expected exec.ExitError, got %T", err)
exitCode := hypeCmd.ProcessState.ExitCode()
r.Equal(1, exitCode)
}
func setupCmd(binPath string, args []string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer) {
cmd := exec.Command(binPath, args...)
var stderr bytes.Buffer
var stdout bytes.Buffer
cmd.Stderr = &stderr
cmd.Stdout = &stdout
return cmd, &stdout, &stderr
}
func Test_Cli_Success(t *testing.T) {
r := require.New(t)
binPath := filepath.Join(t.TempDir(), "hype")
buildBin(binPath)
root := filepath.Join(getProjectWd(), "testdata", "to_md", "source_code", "full")
t.Chdir(root)
args := []string{"export", "-format=markdown", "-f", "hype.md"}
t.Logf("Running command: %s %s", binPath, strings.Join(args, " "))
hypeCmd, stdout, stderr := setupCmd(binPath, args)
err := hypeCmd.Run()
if err != nil {
fmt.Printf("%s\n", stderr.String())
}
r.NoError(err)
r.Equal(0, stderr.Len())
r.True(stdout.Len() > 0)
exitCode := hypeCmd.ProcessState.ExitCode()
r.Equal(0, exitCode)
b, err := os.ReadFile(filepath.Join(root, "hype.gold"))
r.NoError(err)
exp := strings.TrimSpace(string(b))
act := strings.TrimSpace(stdout.String())
r.Equal(exp, act)
}
func Test_Output_Flag_Error(t *testing.T) {
r := require.New(t)
binPath := filepath.Join(t.TempDir(), "hype")
buildBin(binPath)
root := filepath.Join(getProjectWd(), "testdata", "to_md", "source_code", "broken")
t.Chdir(root)
inputFile := "hype.md"
outputFile := inputFile
args := []string{"export", "-format=markdown", "-f", inputFile, "-o", outputFile}
hypeCmd, _, _ := setupCmd(binPath, args)
err := hypeCmd.Run()
r.Error(err)
info, err := os.Stat(filepath.Join(root, inputFile))
r.NoError(err)
r.True(info.Size() > 0)
}
func Test_Output_Flag_Success(t *testing.T) {
r := require.New(t)
binPath := filepath.Join(t.TempDir(), "hype")
buildBin(binPath)
root := filepath.Join(getProjectWd(), "testdata", "to_md", "source_code", "full")
t.Chdir(root)
inputFile := "hype.md"
outputFile := filepath.Join(t.TempDir(), "output.md")
args := []string{"export", "-format=markdown", "-f", inputFile, "-o", outputFile}
hypeCmd, _, _ := setupCmd(binPath, args)
err := hypeCmd.Run()
r.NoError(err)
info, err := os.Stat(outputFile)
r.NoError(err)
r.True(info.Size() > 0)
bExp, err := os.ReadFile(filepath.Join(root, "hype.gold"))
r.NoError(err)
bAct, err := os.ReadFile(outputFile)
r.NoError(err)
exp := strings.TrimSpace(string(bExp))
act := strings.TrimSpace(string(bAct))
r.Equal(exp, act)
}