Skip to content

Commit f2cc88b

Browse files
authored
chore: move go fake runtime to a library (#389)
Co-authored-by: rick <[email protected]>
1 parent eb7be78 commit f2cc88b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+205
-575
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ build-linux-no-check:
1010
CGO_ENABLE=0 GOOS=linux go build -ldflags "-w -s" -o bin/linux/hd
1111
upx bin/linux/hd
1212

13-
test: fmt lint
13+
test: fmt
1414
go test ./... -coverprofile coverage.out
15-
pre-commit: fmt lint test build
15+
pre-commit: fmt test build
1616
cp-pre-commit:
1717
cp .github/pre-commit .git/hooks/pre-commit
1818
run:

cmd/get.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ import (
1919
"github.com/linuxsuren/http-downloader/pkg/log"
2020

2121
"github.com/AlecAivazis/survey/v2"
22-
"github.com/linuxsuren/http-downloader/pkg/exec"
2322
"golang.org/x/net/html"
2423
"golang.org/x/net/html/charset"
2524

25+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
2626
"github.com/linuxsuren/http-downloader/pkg"
2727
"github.com/linuxsuren/http-downloader/pkg/installer"
2828
"github.com/linuxsuren/http-downloader/pkg/net"
@@ -82,7 +82,7 @@ func newDownloadOption(ctx context.Context) *downloadOption {
8282
RoundTripper: getRoundTripper(ctx),
8383
fetcher: &installer.DefaultFetcher{},
8484
wait: &sync.WaitGroup{},
85-
execer: exec.DefaultExecer{},
85+
execer: fakeruntime.DefaultExecer{},
8686
}
8787
}
8888

@@ -125,7 +125,7 @@ type downloadOption struct {
125125
org string
126126
repo string
127127
fetcher installer.Fetcher
128-
execer exec.Execer
128+
execer fakeruntime.Execer
129129
ExpectVersion string // should be like >v1.1.0
130130
}
131131

@@ -360,7 +360,7 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
360360
return
361361
}
362362

363-
func downloadMagnetFile(proxyGitHub, target string, execer exec.Execer) (err error) {
363+
func downloadMagnetFile(proxyGitHub, target string, execer fakeruntime.Execer) (err error) {
364364
targetCmd := "gotorrent"
365365
is := installer.Installer{
366366
Provider: "github",

cmd/get_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import (
1515

1616
"github.com/golang/mock/gomock"
1717
"github.com/h2non/gock"
18+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
1819
"github.com/linuxsuren/http-downloader/mock/mhttp"
19-
"github.com/linuxsuren/http-downloader/pkg/exec"
2020
"github.com/linuxsuren/http-downloader/pkg/installer"
2121
"github.com/spf13/cobra"
2222
"github.com/stretchr/testify/assert"
@@ -226,18 +226,18 @@ func TestDownloadMagnetFile(t *testing.T) {
226226
proxyGitHub string
227227
target string
228228
prepare func()
229-
execer exec.Execer
229+
execer fakeruntime.Execer
230230
expectErr bool
231231
}{{
232232
name: "proxyGitHub and target is empty",
233233
proxyGitHub: "",
234234
target: "",
235-
execer: exec.FakeExecer{},
235+
execer: fakeruntime.FakeExecer{},
236236
}, {
237237
name: "failed to download",
238238
proxyGitHub: "",
239239
target: "fake",
240-
execer: exec.FakeExecer{ExpectError: errors.New("error")},
240+
execer: fakeruntime.FakeExecer{ExpectError: errors.New("error")},
241241
expectErr: true,
242242
}, {
243243
name: "one target item",
@@ -249,7 +249,7 @@ func TestDownloadMagnetFile(t *testing.T) {
249249
Reply(http.StatusOK).
250250
File("testdata/magnet.html")
251251
},
252-
execer: exec.FakeExecer{},
252+
execer: fakeruntime.FakeExecer{},
253253
expectErr: false,
254254
}, {
255255
name: "HTTP server error response",
@@ -260,7 +260,7 @@ func TestDownloadMagnetFile(t *testing.T) {
260260
Get("/").
261261
ReplyError(errors.New("error"))
262262
},
263-
execer: exec.FakeExecer{},
263+
execer: fakeruntime.FakeExecer{},
264264
expectErr: true,
265265
}}
266266
for _, tt := range tests {

cmd/install.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"strings"
1010

1111
"github.com/AlecAivazis/survey/v2"
12+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
1213
"github.com/linuxsuren/http-downloader/pkg/common"
13-
"github.com/linuxsuren/http-downloader/pkg/exec"
1414
"github.com/linuxsuren/http-downloader/pkg/installer"
1515
"github.com/linuxsuren/http-downloader/pkg/os"
1616
"github.com/linuxsuren/http-downloader/pkg/version"
@@ -22,7 +22,7 @@ import (
2222
func newInstallCmd(ctx context.Context) (cmd *cobra.Command) {
2323
opt := &installOption{
2424
downloadOption: newDownloadOption(ctx),
25-
execer: &exec.DefaultExecer{},
25+
execer: &fakeruntime.DefaultExecer{},
2626
}
2727
cmd = &cobra.Command{
2828
Use: "install",
@@ -88,7 +88,7 @@ type installOption struct {
8888
// inner fields
8989
nativePackage bool
9090
tool string
91-
execer exec.Execer
91+
execer fakeruntime.Execer
9292
}
9393

9494
func (o *installOption) shouldInstall() (should, exist bool) {

cmd/install_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"testing"
88

99
cotesting "github.com/linuxsuren/cobra-extension/pkg/testing"
10-
"github.com/linuxsuren/http-downloader/pkg/exec"
10+
fakeruntime "github.com/linuxsuren/go-fake-runtime"
1111
"github.com/linuxsuren/http-downloader/pkg/installer"
1212
"github.com/spf13/cobra"
1313
"github.com/stretchr/testify/assert"
@@ -118,7 +118,7 @@ func TestInstallPreRunE(t *testing.T) {
118118
func TestShouldInstall(t *testing.T) {
119119
opt := &installOption{
120120
downloadOption: &downloadOption{},
121-
execer: &exec.FakeExecer{
121+
execer: &fakeruntime.FakeExecer{
122122
ExpectOutput: "v1.2.3",
123123
},
124124
tool: "fake",
@@ -129,7 +129,7 @@ func TestShouldInstall(t *testing.T) {
129129

130130
{
131131
optGreater := &installOption{
132-
execer: &exec.FakeExecer{
132+
execer: &fakeruntime.FakeExecer{
133133
ExpectOutput: "v1.2.3",
134134
},
135135
downloadOption: &downloadOption{
@@ -152,7 +152,7 @@ func TestShouldInstall(t *testing.T) {
152152
assert.True(t, exist)
153153

154154
// not exist
155-
opt.execer = &exec.FakeExecer{
155+
opt.execer = &fakeruntime.FakeExecer{
156156
ExpectError: errors.New("fake"),
157157
ExpectLookPathError: errors.New("error"),
158158
}
@@ -176,7 +176,7 @@ func TestInstall(t *testing.T) {
176176
opt: &installOption{
177177
downloadOption: &downloadOption{},
178178
nativePackage: true,
179-
execer: exec.FakeExecer{},
179+
execer: fakeruntime.FakeExecer{},
180180
},
181181
args: args{cmd: &cobra.Command{}},
182182
expectErr: false,
@@ -195,29 +195,29 @@ func TestInstall(t *testing.T) {
195195
func TestGetDefaultInstallDir(t *testing.T) {
196196
tests := []struct {
197197
name string
198-
execer exec.Execer
198+
execer fakeruntime.Execer
199199
expect string
200200
}{{
201201
name: "linux",
202-
execer: exec.FakeExecer{
202+
execer: fakeruntime.FakeExecer{
203203
ExpectOS: "linux",
204204
},
205205
expect: "/usr/local/bin",
206206
}, {
207207
name: "darwin",
208-
execer: exec.FakeExecer{
208+
execer: fakeruntime.FakeExecer{
209209
ExpectOS: "darwin",
210210
},
211211
expect: "/usr/local/bin",
212212
}, {
213213
name: "windows",
214-
execer: exec.FakeExecer{
214+
execer: fakeruntime.FakeExecer{
215215
ExpectOS: "windows",
216216
},
217217
expect: `C:\Program Files (x86)\Common Files`,
218218
}, {
219219
name: "unknown",
220-
execer: exec.FakeExecer{},
220+
execer: fakeruntime.FakeExecer{},
221221
expect: "",
222222
}}
223223
for _, tt := range tests {

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/spf13/cobra v1.6.1
1717
github.com/spf13/pflag v1.0.5
1818
github.com/spf13/viper v1.9.0
19-
github.com/stretchr/testify v1.8.1
19+
github.com/stretchr/testify v1.8.2
2020
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
2121
gopkg.in/yaml.v3 v3.0.1
2222
)
@@ -25,13 +25,13 @@ require (
2525
github.com/antonmedv/expr v1.11.1
2626
github.com/creack/pty v1.1.17
2727
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213
28+
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f
2829
github.com/schollz/progressbar/v3 v3.13.0
2930
)
3031

3132
require (
3233
github.com/mattn/go-runewidth v0.0.14 // indirect
3334
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
34-
github.com/prometheus/client_golang v1.11.1 // indirect
3535
github.com/rivo/uniseg v0.4.3 // indirect
3636
)
3737

0 commit comments

Comments
 (0)