Skip to content

Commit

Permalink
Add more unit tests (#279)
Browse files Browse the repository at this point in the history
* 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 <linuxsuren@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored Aug 20, 2022
1 parent 6475b7a commit 0f6cfe2
Show file tree
Hide file tree
Showing 26 changed files with 459 additions and 68 deletions.
18 changes: 11 additions & 7 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package cmd
import (
"context"
"fmt"
"github.com/linuxsuren/http-downloader/pkg"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
"net/http"
"net/url"
sysos "os"
"path"
"runtime"
"strings"

"github.com/linuxsuren/http-downloader/pkg"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)

// newGetCmd return the get command
Expand Down Expand Up @@ -95,6 +96,7 @@ type downloadOption struct {
Package *installer.HDConfig
org string
repo string
fetcher installer.Fetcher
}

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

// fetch the latest config
fmt.Println("start to fetch the config")
fetcher := &installer.DefaultFetcher{}
if err = fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
if o.fetcher == nil {
o.fetcher = &installer.DefaultFetcher{}
}
if err = o.fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
return
}
Expand Down
57 changes: 56 additions & 1 deletion cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package cmd

import (
"context"
"github.com/stretchr/testify/assert"
"errors"
"testing"

"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func Test_newGetCmd(t *testing.T) {
Expand Down Expand Up @@ -51,3 +55,54 @@ func Test_newGetCmd(t *testing.T) {
})
}
}

func TestFetch(t *testing.T) {
opt := &downloadOption{}
opt.Fetch = false
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}

// do not fetch
assert.Nil(t, opt.fetch())

// fetch flag is true
opt.Fetch = true
assert.NotNil(t, opt.fetch())

// failed when fetching
opt.fetcher = &installer.FakeFetcher{}
assert.Nil(t, opt.fetch())
}

func TestPreRunE(t *testing.T) {
opt := &downloadOption{}
opt.Fetch = true
opt.fetcher = &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}
opt.PrintSchema = true

// only print schema
assert.Nil(t, opt.preRunE(nil, nil))

// failed to fetch
opt.PrintSchema = false
assert.NotNil(t, opt.preRunE(nil, nil))

// pripnt categories
opt.fetcher = &installer.FakeFetcher{}
opt.PrintCategories = true
assert.Nil(t, opt.preRunE(nil, nil))

// not args provided
opt.PrintCategories = false
assert.NotNil(t, opt.preRunE(nil, nil))
}

func TestRunE(t *testing.T) {
fakeCmd := &cobra.Command{}

opt := &downloadOption{}
opt.fetcher = &installer.FakeFetcher{}

// print schema
opt.PrintSchema = true
assert.Nil(t, opt.runE(fakeCmd, nil))
}
13 changes: 8 additions & 5 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package cmd
import (
"context"
"fmt"
sysos "os"
"path"
"runtime"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/linuxsuren/http-downloader/pkg/common"
"github.com/linuxsuren/http-downloader/pkg/exec"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/linuxsuren/http-downloader/pkg/os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
sysos "os"
"path"
"runtime"
"strings"
)

// newInstallCmd returns the install command
Expand All @@ -22,6 +23,7 @@ func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
downloadOption: downloadOption{
RoundTripper: getRoundTripper(ctx),
},
execer: &exec.DefaultExecer{},
}
cmd = &cobra.Command{
Use: "install",
Expand Down Expand Up @@ -79,10 +81,11 @@ type installOption struct {
// inner fields
nativePackage bool
tool string
execer exec.Execer
}

func (o *installOption) shouldInstall() (should, exist bool) {
if _, lookErr := exec.LookPath(o.tool); lookErr == nil {
if _, lookErr := o.execer.LookPath(o.tool); lookErr == nil {
exist = true
}
should = o.force || !exist
Expand Down
102 changes: 102 additions & 0 deletions cmd/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package cmd

import (
"context"
"errors"
cotesting "github.com/linuxsuren/cobra-extension/pkg/testing"
"github.com/linuxsuren/http-downloader/pkg/exec"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -51,3 +54,102 @@ func Test_newInstallCmd(t *testing.T) {
}}
test.Valid(t, cmd.Flags())
}

func TestInstallPreRunE(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
}
for i, tt := range []struct {
name string
opt *installOption
args args
expectErr bool
}{{
name: "tool and category are empty",
opt: &installOption{},
expectErr: true,
}, {
name: "a fake tool that have an invalid path, no category",
opt: &installOption{
downloadOption: downloadOption{searchOption: searchOption{Fetch: false}},
},
args: args{
args: []string{"xx@xx@xx"},
},
expectErr: true,
}, {
name: "have category",
opt: &installOption{
downloadOption: downloadOption{
searchOption: searchOption{Fetch: false},
Category: "tool",
},
},
args: args{
args: []string{"xx@xx@xx"},
},
expectErr: false,
}} {
t.Run(tt.name, func(t *testing.T) {
err := tt.opt.preRunE(tt.args.cmd, tt.args.args)
if tt.expectErr {
assert.NotNil(t, err, "failed with [%d] - case [%s]", i, tt.name)
} else {
assert.Nil(t, err, "failed with [%d] - case [%s]", i, tt.name)
}
})
}
}

func TestShouldInstall(t *testing.T) {
opt := &installOption{
execer: &exec.FakeExecer{},
tool: "fake",
}
should, exist := opt.shouldInstall()
assert.False(t, should)
assert.True(t, exist)

// force to install
opt.force = true
should, exist = opt.shouldInstall()
assert.True(t, should)
assert.True(t, exist)

// not exist
opt.execer = &exec.FakeExecer{ExpectError: errors.New("fake")}
should, exist = opt.shouldInstall()
assert.True(t, should)
assert.False(t, exist)
}

func TestInstall(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
}
for i, tt := range []struct {
name string
opt *installOption
args args
expectErr bool
}{{
name: "is a nativePackage, but it's exist",
opt: &installOption{
nativePackage: true,
execer: exec.FakeExecer{},
},
args: args{cmd: &cobra.Command{}},
expectErr: false,
}} {
t.Run(tt.name, func(t *testing.T) {
err := tt.opt.install(tt.args.cmd, tt.args.args)
if tt.expectErr {
assert.NotNil(t, err, "failed with [%d] - case [%s]", i, tt.name)
} else {
assert.Nil(t, err, "failed with [%d] - case [%s]", i, tt.name)
}
})
}
}
14 changes: 10 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package cmd
import (
"context"
"fmt"
"os"
"runtime"

extpkg "github.com/linuxsuren/cobra-extension/pkg"
extver "github.com/linuxsuren/cobra-extension/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"runtime"
)

// NewRoot returns the root command
Expand Down Expand Up @@ -42,8 +43,13 @@ func loadConfig() (err error) {
}
}
viper.SetDefault("provider", ProviderGitHub)
viper.SetDefault("fetch", true)
viper.SetDefault("thread", runtime.NumCPU()/2)
viper.SetDefault("fetch", false)
viper.SetDefault("goget", false)

thread := runtime.NumCPU()
if thread > 4 {
thread = thread / 2
}
viper.SetDefault("thread", thread)
return
}
3 changes: 2 additions & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package cmd

import (
"context"
"github.com/stretchr/testify/assert"
"testing"

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

func TestNewRoot(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@ require (
github.com/stretchr/testify v1.7.0
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,8 @@ golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210502180810-71e4cd670f79/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -709,8 +706,6 @@ golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
4 changes: 2 additions & 2 deletions pkg/compress/bzip2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var _ Compress = &Bzip2{}

// ExtractFiles extracts files from a target compress file
func (x *Bzip2) ExtractFiles(sourceFile, targetName string) (err error) {
if targetName == "" {
err = errors.New("target filename is empty")
if sourceFile == "" || targetName == "" {
err = errors.New("source or target filename is empty")
return
}
var f *os.File
Expand Down
4 changes: 2 additions & 2 deletions pkg/compress/gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var _ Compress = &GZip{}

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

Expand Down
8 changes: 4 additions & 4 deletions pkg/compress/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func extraFile(name, targetName, tarFile string, header *tar.Header, tarReader *
os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode)); err != nil {
return
}
if _, err = io.Copy(targetFile, tarReader); err != nil {
return
}
_ = targetFile.Close()
defer func() {
_ = targetFile.Close()
}()
_, err = io.Copy(targetFile, tarReader)
return
}

Expand Down
Loading

0 comments on commit 0f6cfe2

Please sign in to comment.