Skip to content

Commit 854a60e

Browse files
committed
ci: complete tests
add more tests for cmd add test for style handlers add test for main.go update go-testcoverag config
1 parent 069b7ae commit 854a60e

8 files changed

Lines changed: 381 additions & 28 deletions

File tree

.testcoverage.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ profile: cover.out
33
threshold:
44
file: 70
55
package: 80
6-
total: 80
6+
total: 85
77

88
exclude:
99
paths:
10-
- internal/style/style_handlers.go
1110
- main\.go$
12-
- cmd/main\.go$

cmd/config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
var emptyString = ""
1414

15-
func TestNewHTTPSWrenchConfig(t *testing.T) {
15+
func TestConfig_NewHTTPSWrenchConfig(t *testing.T) {
1616
t.Run("new HTTPSWrenchConfig", func(t *testing.T) {
1717
var mc requests.RequestsMetaConfig
1818

cmd/man_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestMan(t *testing.T) {
11+
t.Run("Run manCmd", func(t *testing.T) {
12+
destDir := t.TempDir()
13+
manBuf := new(bytes.Buffer)
14+
rootCmd.SetOut(manBuf)
15+
rootCmd.SetErr(manBuf)
16+
rootCmd.SetArgs([]string{"man", "--dest-dir", destDir})
17+
18+
err := rootCmd.Execute()
19+
require.NoError(t, err)
20+
21+
rootCmd.SetArgs([]string{"man", "--dest-dir", "fake-dir"})
22+
errWrongDir := rootCmd.Execute()
23+
require.NoError(t, errWrongDir)
24+
// WARN stdout does not get into the buffer
25+
// require.Contains(t, manBuf.String(), "no such file or directory--- FAIL")
26+
})
27+
}

cmd/root.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ https://github.com/xenOs76/https-wrench`,
8080
},
8181
}
8282

83-
func Execute() {
83+
func Execute() error {
8484
err := rootCmd.Execute()
8585
if err != nil {
86-
return
86+
return err
8787
}
88+
89+
return nil
8890
}
8991

9092
func init() {

cmd/root_test.go

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,105 @@ package cmd
22

33
import (
44
"bytes"
5+
"crypto/x509"
56
_ "embed"
67
"testing"
78

89
_ "github.com/breml/rootcerts"
10+
"github.com/google/go-cmp/cmp"
911
"github.com/stretchr/testify/require"
12+
"github.com/xenos76/https-wrench/internal/requests"
1013
)
1114

15+
func TestRootCmd_LoadConfig(t *testing.T) {
16+
t.Run("LoadConfig no config file", func(t *testing.T) {
17+
var mc requests.RequestsMetaConfig
18+
19+
config, err := LoadConfig()
20+
require.NoError(t, err)
21+
require.False(t, config.Debug)
22+
require.False(t, config.Verbose)
23+
require.Empty(t, config.CaBundle)
24+
25+
if diff := cmp.Diff(mc, config.RequestsMetaConfig); diff != "" {
26+
t.Errorf(
27+
"NewHTTPSWrenchConfig: RequestsMetaConfig mismatch (-want +got):\n%s",
28+
diff,
29+
)
30+
}
31+
})
32+
33+
t.Run("LoadConfig embedded config file", func(t *testing.T) {
34+
var expectedCaCertsPool *x509.CertPool
35+
36+
var expectedRequestsConfigs []requests.RequestConfig
37+
38+
cfgFile = "./embedded/config-example.yaml"
39+
40+
initConfig()
41+
42+
config, err := LoadConfig()
43+
require.NoError(t, err)
44+
require.False(t, config.Debug)
45+
require.True(t, config.Verbose)
46+
require.Empty(t, config.CaBundle)
47+
48+
// testing mapstructure squash/embedding of requests.RequestsMetaConfig
49+
// into HTTPSWrenchConfig
50+
require.False(t, config.RequestDebug)
51+
require.False(t, config.RequestVerbose)
52+
require.IsType(t, expectedCaCertsPool, config.CACertsPool)
53+
require.IsType(t, expectedRequestsConfigs, config.Requests)
54+
55+
// testing against the current values of the embedded config
56+
require.Equal(t, "httpBunComGet", config.Requests[0].Name)
57+
require.Equal(t, "https://cat.httpbun.com:443", config.Requests[0].TransportOverrideURL)
58+
})
59+
}
60+
61+
func TestRootCmd_Execute(t *testing.T) {
62+
t.Run("Execute empty config", func(t *testing.T) {
63+
cfgFile = ""
64+
65+
initConfig()
66+
67+
_, err := LoadConfig()
68+
69+
require.NoError(t, err)
70+
Execute()
71+
})
72+
}
73+
1274
func TestRootCmd(t *testing.T) {
1375
tests := []struct {
1476
name string
1577
args []string
1678
expectError bool
1779
expected []string
1880
}{
81+
// {
82+
// name: "no args",
83+
// args: []string{},
84+
// expectError: false,
85+
// expected: []string{
86+
// "HTTPS Wrench",
87+
// "Usage:",
88+
// "Available Commands:",
89+
// "Flags:",
90+
// "certinfo",
91+
// "requests",
92+
// "--config",
93+
// "--version",
94+
// "--help",
95+
// },
96+
// },
97+
//
1998
{
20-
name: "no args",
21-
args: []string{},
99+
name: "config flag valid arg",
100+
args: []string{"--config", "./embedded/config-example.yaml"},
22101
expectError: false,
23-
expected: []string{
24-
"HTTPS Wrench",
25-
"Usage:",
26-
"Available Commands:",
27-
"Flags:",
28-
"certinfo",
29-
"requests",
30-
"--config",
31-
"--version",
32-
"--help",
33-
},
102+
// Unable to intercept the output
103+
expected: []string{},
34104
},
35105

36106
{
@@ -39,13 +109,7 @@ func TestRootCmd(t *testing.T) {
39109
expectError: true,
40110
expected: []string{"flag needs an argument: --config"},
41111
},
42-
{
43-
name: "config flag valid arg",
44-
args: []string{"--config", "./embedded/config-example.yaml"},
45-
expectError: false,
46-
// Unable to intercept the output
47-
expected: []string{},
48-
},
112+
49113
{
50114
name: "version",
51115
args: []string{"--version"},

0 commit comments

Comments
 (0)