Skip to content

Commit 0f6cfe2

Browse files
authored
Add more unit tests (#279)
* Add more unit tests * Upgrade github.com/prometheus/client_golang to 1.11.1 * Fix the security issue found by gosec * Add more unit tests on get command * Add unit test on http package * Add more unit tests on get command * Remove duplicated test methods Co-authored-by: Rick <[email protected]>
1 parent 6475b7a commit 0f6cfe2

26 files changed

+459
-68
lines changed

cmd/get.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package cmd
33
import (
44
"context"
55
"fmt"
6-
"github.com/linuxsuren/http-downloader/pkg"
7-
"github.com/linuxsuren/http-downloader/pkg/installer"
8-
"github.com/spf13/cobra"
9-
"github.com/spf13/viper"
10-
"gopkg.in/yaml.v2"
116
"net/http"
127
"net/url"
138
sysos "os"
149
"path"
1510
"runtime"
1611
"strings"
12+
13+
"github.com/linuxsuren/http-downloader/pkg"
14+
"github.com/linuxsuren/http-downloader/pkg/installer"
15+
"github.com/spf13/cobra"
16+
"github.com/spf13/viper"
17+
"gopkg.in/yaml.v3"
1718
)
1819

1920
// newGetCmd return the get command
@@ -95,6 +96,7 @@ type downloadOption struct {
9596
Package *installer.HDConfig
9697
org string
9798
repo string
99+
fetcher installer.Fetcher
98100
}
99101

100102
const (
@@ -111,8 +113,10 @@ func (o *downloadOption) fetch() (err error) {
111113

112114
// fetch the latest config
113115
fmt.Println("start to fetch the config")
114-
fetcher := &installer.DefaultFetcher{}
115-
if err = fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
116+
if o.fetcher == nil {
117+
o.fetcher = &installer.DefaultFetcher{}
118+
}
119+
if err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
116120
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
117121
return
118122
}

cmd/get_test.go

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ package cmd
22

33
import (
44
"context"
5-
"github.com/stretchr/testify/assert"
5+
"errors"
66
"testing"
7+
8+
"github.com/linuxsuren/http-downloader/pkg/installer"
9+
"github.com/spf13/cobra"
10+
"github.com/stretchr/testify/assert"
711
)
812

913
func Test_newGetCmd(t *testing.T) {
@@ -51,3 +55,54 @@ func Test_newGetCmd(t *testing.T) {
5155
})
5256
}
5357
}
58+
59+
func TestFetch(t *testing.T) {
60+
opt := &downloadOption{}
61+
opt.Fetch = false
62+
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}
63+
64+
// do not fetch
65+
assert.Nil(t, opt.fetch())
66+
67+
// fetch flag is true
68+
opt.Fetch = true
69+
assert.NotNil(t, opt.fetch())
70+
71+
// failed when fetching
72+
opt.fetcher = &installer.FakeFetcher{}
73+
assert.Nil(t, opt.fetch())
74+
}
75+
76+
func TestPreRunE(t *testing.T) {
77+
opt := &downloadOption{}
78+
opt.Fetch = true
79+
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}
80+
opt.PrintSchema = true
81+
82+
// only print schema
83+
assert.Nil(t, opt.preRunE(nil, nil))
84+
85+
// failed to fetch
86+
opt.PrintSchema = false
87+
assert.NotNil(t, opt.preRunE(nil, nil))
88+
89+
// pripnt categories
90+
opt.fetcher = &installer.FakeFetcher{}
91+
opt.PrintCategories = true
92+
assert.Nil(t, opt.preRunE(nil, nil))
93+
94+
// not args provided
95+
opt.PrintCategories = false
96+
assert.NotNil(t, opt.preRunE(nil, nil))
97+
}
98+
99+
func TestRunE(t *testing.T) {
100+
fakeCmd := &cobra.Command{}
101+
102+
opt := &downloadOption{}
103+
opt.fetcher = &installer.FakeFetcher{}
104+
105+
// print schema
106+
opt.PrintSchema = true
107+
assert.Nil(t, opt.runE(fakeCmd, nil))
108+
}

cmd/install.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
sysos "os"
7+
"path"
8+
"runtime"
9+
"strings"
10+
611
"github.com/AlecAivazis/survey/v2"
712
"github.com/linuxsuren/http-downloader/pkg/common"
813
"github.com/linuxsuren/http-downloader/pkg/exec"
914
"github.com/linuxsuren/http-downloader/pkg/installer"
1015
"github.com/linuxsuren/http-downloader/pkg/os"
1116
"github.com/spf13/cobra"
1217
"github.com/spf13/viper"
13-
sysos "os"
14-
"path"
15-
"runtime"
16-
"strings"
1718
)
1819

1920
// newInstallCmd returns the install command
@@ -22,6 +23,7 @@ func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
2223
downloadOption: downloadOption{
2324
RoundTripper: getRoundTripper(ctx),
2425
},
26+
execer: &exec.DefaultExecer{},
2527
}
2628
cmd = &cobra.Command{
2729
Use: "install",
@@ -79,10 +81,11 @@ type installOption struct {
7981
// inner fields
8082
nativePackage bool
8183
tool string
84+
execer exec.Execer
8285
}
8386

8487
func (o *installOption) shouldInstall() (should, exist bool) {
85-
if _, lookErr := exec.LookPath(o.tool); lookErr == nil {
88+
if _, lookErr := o.execer.LookPath(o.tool); lookErr == nil {
8689
exist = true
8790
}
8891
should = o.force || !exist

cmd/install_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package cmd
22

33
import (
44
"context"
5+
"errors"
56
cotesting "github.com/linuxsuren/cobra-extension/pkg/testing"
7+
"github.com/linuxsuren/http-downloader/pkg/exec"
8+
"github.com/spf13/cobra"
69
"github.com/stretchr/testify/assert"
710
"testing"
811
)
@@ -51,3 +54,102 @@ func Test_newInstallCmd(t *testing.T) {
5154
}}
5255
test.Valid(t, cmd.Flags())
5356
}
57+
58+
func TestInstallPreRunE(t *testing.T) {
59+
type args struct {
60+
cmd *cobra.Command
61+
args []string
62+
}
63+
for i, tt := range []struct {
64+
name string
65+
opt *installOption
66+
args args
67+
expectErr bool
68+
}{{
69+
name: "tool and category are empty",
70+
opt: &installOption{},
71+
expectErr: true,
72+
}, {
73+
name: "a fake tool that have an invalid path, no category",
74+
opt: &installOption{
75+
downloadOption: downloadOption{searchOption: searchOption{Fetch: false}},
76+
},
77+
args: args{
78+
args: []string{"xx@xx@xx"},
79+
},
80+
expectErr: true,
81+
}, {
82+
name: "have category",
83+
opt: &installOption{
84+
downloadOption: downloadOption{
85+
searchOption: searchOption{Fetch: false},
86+
Category: "tool",
87+
},
88+
},
89+
args: args{
90+
args: []string{"xx@xx@xx"},
91+
},
92+
expectErr: false,
93+
}} {
94+
t.Run(tt.name, func(t *testing.T) {
95+
err := tt.opt.preRunE(tt.args.cmd, tt.args.args)
96+
if tt.expectErr {
97+
assert.NotNil(t, err, "failed with [%d] - case [%s]", i, tt.name)
98+
} else {
99+
assert.Nil(t, err, "failed with [%d] - case [%s]", i, tt.name)
100+
}
101+
})
102+
}
103+
}
104+
105+
func TestShouldInstall(t *testing.T) {
106+
opt := &installOption{
107+
execer: &exec.FakeExecer{},
108+
tool: "fake",
109+
}
110+
should, exist := opt.shouldInstall()
111+
assert.False(t, should)
112+
assert.True(t, exist)
113+
114+
// force to install
115+
opt.force = true
116+
should, exist = opt.shouldInstall()
117+
assert.True(t, should)
118+
assert.True(t, exist)
119+
120+
// not exist
121+
opt.execer = &exec.FakeExecer{ExpectError: errors.New("fake")}
122+
should, exist = opt.shouldInstall()
123+
assert.True(t, should)
124+
assert.False(t, exist)
125+
}
126+
127+
func TestInstall(t *testing.T) {
128+
type args struct {
129+
cmd *cobra.Command
130+
args []string
131+
}
132+
for i, tt := range []struct {
133+
name string
134+
opt *installOption
135+
args args
136+
expectErr bool
137+
}{{
138+
name: "is a nativePackage, but it's exist",
139+
opt: &installOption{
140+
nativePackage: true,
141+
execer: exec.FakeExecer{},
142+
},
143+
args: args{cmd: &cobra.Command{}},
144+
expectErr: false,
145+
}} {
146+
t.Run(tt.name, func(t *testing.T) {
147+
err := tt.opt.install(tt.args.cmd, tt.args.args)
148+
if tt.expectErr {
149+
assert.NotNil(t, err, "failed with [%d] - case [%s]", i, tt.name)
150+
} else {
151+
assert.Nil(t, err, "failed with [%d] - case [%s]", i, tt.name)
152+
}
153+
})
154+
}
155+
}

cmd/root.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"runtime"
8+
69
extpkg "github.com/linuxsuren/cobra-extension/pkg"
710
extver "github.com/linuxsuren/cobra-extension/version"
811
"github.com/spf13/cobra"
912
"github.com/spf13/viper"
10-
"os"
11-
"runtime"
1213
)
1314

1415
// NewRoot returns the root command
@@ -42,8 +43,13 @@ func loadConfig() (err error) {
4243
}
4344
}
4445
viper.SetDefault("provider", ProviderGitHub)
45-
viper.SetDefault("fetch", true)
46-
viper.SetDefault("thread", runtime.NumCPU()/2)
46+
viper.SetDefault("fetch", false)
4747
viper.SetDefault("goget", false)
48+
49+
thread := runtime.NumCPU()
50+
if thread > 4 {
51+
thread = thread / 2
52+
}
53+
viper.SetDefault("thread", thread)
4854
return
4955
}

cmd/root_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package cmd
22

33
import (
44
"context"
5-
"github.com/stretchr/testify/assert"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestNewRoot(t *testing.T) {

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ require (
2121
github.com/stretchr/testify v1.7.0
2222
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
2323
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
24-
gopkg.in/yaml.v2 v2.4.0
2524
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
2625
)

go.sum

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,8 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
694694
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
695695
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
696696
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
697-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
698-
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
699697
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
700698
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
701-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
702699
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
703700
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
704701
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -709,8 +706,6 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
709706
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
710707
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
711708
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
712-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
713-
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
714709
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
715710
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
716711
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

pkg/compress/bzip2.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var _ Compress = &Bzip2{}
2424

2525
// ExtractFiles extracts files from a target compress file
2626
func (x *Bzip2) ExtractFiles(sourceFile, targetName string) (err error) {
27-
if targetName == "" {
28-
err = errors.New("target filename is empty")
27+
if sourceFile == "" || targetName == "" {
28+
err = errors.New("source or target filename is empty")
2929
return
3030
}
3131
var f *os.File

pkg/compress/gzip.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ var _ Compress = &GZip{}
2525

2626
// ExtractFiles extracts files from a target compress file
2727
func (c *GZip) ExtractFiles(sourceFile, targetName string) (err error) {
28-
if targetName == "" {
29-
err = errors.New("target filename is empty")
28+
if sourceFile == "" || targetName == "" {
29+
err = errors.New("source or target filename is empty")
3030
return
3131
}
3232

pkg/compress/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *
2323
os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil {
2424
return
2525
}
26-
if _, err = io.Copy(targetFile, tarReader); err != nil {
27-
return
28-
}
29-
_ = targetFile.Close()
26+
defer func() {
27+
_ = targetFile.Close()
28+
}()
29+
_, err = io.Copy(targetFile, tarReader)
3030
return
3131
}
3232

0 commit comments

Comments
 (0)