Skip to content

Commit

Permalink
Merge pull request #135 from WillAbides/retest
Browse files Browse the repository at this point in the history
Improve tests and stop mocking
  • Loading branch information
WillAbides authored Apr 7, 2023
2 parents 42fe5bf + b5e33d0 commit 59b95e2
Show file tree
Hide file tree
Showing 37 changed files with 2,449 additions and 813 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
tmp/
bin/
/bin
dist/
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ bin/yq: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/yq

bin/mockgen: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/mockgen

bin/semver-next: bin/bindown
bin/bindown install -q $(notdir $@)
bins += bin/semver-next
Expand Down
30 changes: 0 additions & 30 deletions bindown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ dependencies:
template: origin#jq
vars:
version: "1.6"
mockgen:
template: local-origin#mockgen
vars:
version: 1.6.0
semver-next:
template: local-origin#semver-next
vars:
Expand Down Expand Up @@ -185,28 +181,6 @@ templates:
- linux/amd64
- windows/386
- windows/amd64
local-origin#mockgen:
url: https://github.com/golang/mock/releases/download/v{{.version}}/mock_{{.version}}_{{.os}}_{{.arch}}.tar.gz
archive_path: mock_{{.version}}_{{.os}}_{{.arch}}/mockgen{{.archivepathsuffix}}
bin: mockgen
vars:
archivepathsuffix: ""
required_vars:
- version
overrides:
- matcher:
os:
- windows
dependency:
vars:
archivepathsuffix: .exe
systems:
- darwin/amd64
- darwin/arm64
- linux/386
- linux/amd64
- windows/386
- windows/amd64
local-origin#semver-next:
url: https://github.com/WillAbides/semver-next/releases/download/v{{.version}}/semver-next_{{.version}}_{{.os}}_{{.arch}}.tar.gz
archive_path: semver-next{{.archivepathsuffix}}
Expand Down Expand Up @@ -416,10 +390,6 @@ url_checksums:
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_macOS_amd64.tar.gz: 414f5766a50b95ec73fcff17af6dc5c354b44d7f041370fea9de8f91990cc419
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_macOS_arm64.tar.gz: e18f423f9983f84f011bebf9dea3b3bcd67cc79ef419040316cba35595bdb4b4
https://github.com/cli/cli/releases/download/v2.25.1/gh_2.25.1_windows_amd64.zip: 33be468155dabc1990e984eb9cb9e134c50cfe1c0bbeab92643355a8ea04e162
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_darwin_amd64.tar.gz: 938649db27055ec85bede32ccb95ff82f82af3c90a084ee52687a92066bcca92
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_darwin_arm64.tar.gz: 67a4a597dd759e186e17400cbc273be99c8cffc590335381134b3e933b409c83
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_linux_amd64.tar.gz: c73e117943739df5dd89d63fed6e80cb783852a0cc42abc3359d708c2e125b78
https://github.com/golang/mock/releases/download/v1.6.0/mock_1.6.0_windows_amd64.tar.gz: 12f760cc521b45d0726de11512ed04aea2424d067312f39c926f96b085002590
https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-darwin-amd64.tar.gz: 0549cbaa2df451cf3a2011a9d73a9cb127784d26749d9cd14c9f4818af104d44
https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-darwin-arm64.tar.gz: 36e69882205a0e42a63ad57ec3015639c11051e03f0beb9cf7949c6451408960
https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-linux-amd64.tar.gz: 4de479eb9d9bc29da51aec1834e7c255b333723d38dbd56781c68e5dddc6a90b
Expand Down
14 changes: 6 additions & 8 deletions cmd/bindown/checksums.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package main

import (
"context"

"github.com/willabides/bindown/v3"
)

Expand All @@ -16,28 +14,28 @@ type addChecksumsCmd struct {
Systems []bindown.SystemInfo `kong:"name=system,help=${systems_help},predictor=allSystems"`
}

func (d *addChecksumsCmd) Run(ctx context.Context) error {
config, err := configLoader.Load(ctx, cli.Configfile, true)
func (d *addChecksumsCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
err = config.AddChecksums(d.Dependency, d.Systems)
if err != nil {
return err
}
return config.Write(cli.JSONConfig)
return config.Write(ctx.rootCmd.JSONConfig)
}

type pruneChecksumsCmd struct{}

func (d *pruneChecksumsCmd) Run(ctx context.Context) error {
config, err := configLoader.Load(ctx, cli.Configfile, true)
func (d *pruneChecksumsCmd) Run(ctx *runContext) error {
config, err := loadConfigFile(ctx, true)
if err != nil {
return err
}
err = config.PruneChecksums()
if err != nil {
return err
}
return config.Write(cli.JSONConfig)
return config.Write(ctx.rootCmd.JSONConfig)
}
107 changes: 107 additions & 0 deletions cmd/bindown/checksums_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"net/http/httptest"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
"github.com/willabides/bindown/v3"
)

func Test_addChecksumsCmd(t *testing.T) {
t.Run("success", func(t *testing.T) {
var servers [5]*httptest.Server
var urls [5]string
for i := range servers {
servers[i] = serveFile(t, filepath.FromSlash("../../testdata/downloadables/foo.tar.gz"), "/foo/foo.tar.gz", "")
urls[i] = servers[i].URL + "/foo/foo.tar.gz"
}
runner := newCmdRunner(t)
runner.writeConfig(&bindown.Config{
Dependencies: map[string]*bindown.Dependency{
"d1": {
URL: &urls[0],
Overrides: []bindown.DependencyOverride{
{
Dependency: bindown.Dependency{URL: &urls[1]},
OverrideMatcher: bindown.OverrideMatcher{"os": []string{"darwin"}},
},
{
Dependency: bindown.Dependency{URL: &urls[4]},
OverrideMatcher: bindown.OverrideMatcher{"os": []string{"windows"}},
},
},
},
"d2": {
URL: &urls[2],
Overrides: []bindown.DependencyOverride{
{
Dependency: bindown.Dependency{URL: &urls[3]},
OverrideMatcher: bindown.OverrideMatcher{"os": []string{"darwin"}},
},
},
},
},
})
result := runner.run("checksums", "add", "--system", "darwin/amd64", "--system", "linux/amd64")
result.assertState(resultState{})
want := map[string]string{
urls[0]: fooChecksum,
urls[1]: fooChecksum,
urls[2]: fooChecksum,
urls[3]: fooChecksum,
}
require.Equal(t, want, runner.getConfigFile().URLChecksums)
})

t.Run("400", func(t *testing.T) {
server := serveErr(t, 400)
runner := newCmdRunner(t)
runner.writeConfig(&bindown.Config{
Dependencies: map[string]*bindown.Dependency{
"d1": {URL: &server.URL},
},
})
result := runner.run("checksums", "add", "--system", "darwin/amd64")
result.assertState(resultState{
stderr: "cmd: error: failed downloading",
exit: 1,
})
})

t.Run("dependency does not exist", func(t *testing.T) {
runner := newCmdRunner(t)
runner.writeConfig(&bindown.Config{
Dependencies: map[string]*bindown.Dependency{
"d1": {URL: ptr("fake")},
},
})
result := runner.run("checksums", "add", "--system", "darwin/amd64", "--dependency", "d2")
result.assertState(resultState{
stderr: `cmd: error: no dependency configured with the name "d2"`,
exit: 1,
})
})
}

func Test_pruneChecksumsCmd(t *testing.T) {
t.Run("prunes", func(t *testing.T) {
runner := newCmdRunner(t)
runner.writeConfig(&bindown.Config{
URLChecksums: map[string]string{
"foo": "bar",
"baz": "qux",
},
Dependencies: map[string]*bindown.Dependency{
"d1": {URL: ptr("foo")},
},
})
result := runner.run("checksums", "prune")
result.assertState(resultState{})
want := map[string]string{
"foo": "bar",
}
require.Equal(t, want, runner.getConfigFile().URLChecksums)
})
}
Loading

0 comments on commit 59b95e2

Please sign in to comment.