Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .testcoverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ profile: cover.out
threshold:
file: 70
package: 80
total: 80
total: 85

exclude:
paths:
- internal/style/style_handlers.go
- main\.go$
- cmd/main\.go$
2 changes: 1 addition & 1 deletion cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

var emptyString = ""

func TestNewHTTPSWrenchConfig(t *testing.T) {
func TestConfig_NewHTTPSWrenchConfig(t *testing.T) {
t.Run("new HTTPSWrenchConfig", func(t *testing.T) {
var mc requests.RequestsMetaConfig

Expand Down
29 changes: 29 additions & 0 deletions cmd/man_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"bytes"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestMan(t *testing.T) {
t.Run("Run manCmd", func(t *testing.T) {
destDir := t.TempDir()
manBuf := new(bytes.Buffer)
rootCmd.SetOut(manBuf)
rootCmd.SetErr(manBuf)
rootCmd.SetArgs([]string{"man", "--dest-dir", destDir})

err := rootCmd.Execute()
require.NoError(t, err)

rootCmd.SetArgs([]string{"man", "--dest-dir", "fake-dir"})
errWrongDir := rootCmd.Execute()
require.NoError(t, errWrongDir)
require.FileExists(t, filepath.Join(destDir, "https-wrench.1"))
// WARN stdout does not get into the buffer
// require.Contains(t, manBuf.String(), "no such file or directory--- FAIL")
})
}
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,13 @@ https://github.com/xenOs76/https-wrench`,
},
}

func Execute() {
func Execute() error {
err := rootCmd.Execute()
if err != nil {
return
return err
}

return nil
}

func init() {
Expand Down
102 changes: 96 additions & 6 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,93 @@ package cmd

import (
"bytes"
"crypto/x509"
_ "embed"
"testing"

_ "github.com/breml/rootcerts"
"github.com/google/go-cmp/cmp"
"github.com/spf13/viper"
"github.com/stretchr/testify/require"
"github.com/xenos76/https-wrench/internal/requests"
)

func TestRootCmd_LoadConfig(t *testing.T) {
t.Run("LoadConfig no config file", func(t *testing.T) {
oldCfg := cfgFile

t.Cleanup(func() {
cfgFile = oldCfg

viper.Reset()
})

var mc requests.RequestsMetaConfig

config, err := LoadConfig()
require.NoError(t, err)
require.False(t, config.Debug)
require.False(t, config.Verbose)
require.Empty(t, config.CaBundle)

if diff := cmp.Diff(mc, config.RequestsMetaConfig); diff != "" {
t.Errorf(
"NewHTTPSWrenchConfig: RequestsMetaConfig mismatch (-want +got):\n%s",
diff,
)
}
})

t.Run("LoadConfig embedded config file", func(t *testing.T) {
oldCfg := cfgFile

t.Cleanup(func() {
cfgFile = oldCfg

viper.Reset()
})

var expectedCaCertsPool *x509.CertPool

var expectedRequestsConfigs []requests.RequestConfig

cfgFile = "./embedded/config-example.yaml"

initConfig()

config, err := LoadConfig()
require.NoError(t, err)
require.False(t, config.Debug)
require.True(t, config.Verbose)
require.Empty(t, config.CaBundle)

// testing mapstructure squash/embedding of requests.RequestsMetaConfig
// into HTTPSWrenchConfig
require.False(t, config.RequestDebug)
require.False(t, config.RequestVerbose)
require.IsType(t, expectedCaCertsPool, config.CACertsPool)
require.IsType(t, expectedRequestsConfigs, config.Requests)

// testing against the current values of the embedded config
require.Equal(t, "httpBunComGet", config.Requests[0].Name)
require.Equal(t, "https://cat.httpbun.com:443", config.Requests[0].TransportOverrideURL)
})
}

func TestRootCmd_Execute(t *testing.T) {
t.Run("Execute empty config", func(t *testing.T) {
cfgFile = ""

initConfig()

_, err := LoadConfig()

require.NoError(t, err)
err = Execute()
require.EqualError(t, err, "flag needs an argument: --config")
})
}

func TestRootCmd(t *testing.T) {
tests := []struct {
name string
Expand All @@ -33,19 +113,21 @@ func TestRootCmd(t *testing.T) {
},
},

{
name: "config flag not arg",
args: []string{"--config"},
expectError: true,
expected: []string{"flag needs an argument: --config"},
},
{
name: "config flag valid arg",
args: []string{"--config", "./embedded/config-example.yaml"},
expectError: false,
// Unable to intercept the output
expected: []string{},
},

{
name: "config flag not arg",
args: []string{"--config"},
expectError: true,
expected: []string{"flag needs an argument: --config"},
},

{
name: "version",
args: []string{"--version"},
Expand All @@ -57,6 +139,14 @@ func TestRootCmd(t *testing.T) {
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
oldCfg := cfgFile

t.Cleanup(func() {
cfgFile = oldCfg

viper.Reset()
})

buf := new(bytes.Buffer)
rootCmd.SetOut(buf)
rootCmd.SetErr(buf)
Expand Down
Loading
Loading