Skip to content

Commit

Permalink
Added EnsureStaticCheckAt (#15)
Browse files Browse the repository at this point in the history
* Added EnsureStaticCheckAt

Signed-off-by: Tanmay Chaudhry <[email protected]>

* Restore Old Go Path at the end of tests

Signed-off-by: Tanmay Chaudhry <[email protected]>
  • Loading branch information
tchaudhry91 authored Jun 8, 2022
1 parent 3a13e9a commit abc7688
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var (

// DefaultKindVersion is the default version of KinD that is installed when it's not present
DefaultKindVersion = "v0.12.0"

// DefaultStaticCheckVersion is the default version of StaticCheck that is installed when it's not present
DefaultStaticCheckVersion = "2022.1.2"
)

// Fail if the go version doesn't match the specified constraint
Expand Down Expand Up @@ -100,3 +103,31 @@ func EnsureKindAt(version string) {
kindURL := "https://github.com/kubernetes-sigs/kind/releases/download/{{.VERSION}}/kind-{{.GOOS}}-{{.GOARCH}}"
mgx.Must(pkg.DownloadToGopathBin(kindURL, "kind", version))
}

// Install Staticcheck
func EnsureStaticCheck() {
EnsureStaticCheckAt(DefaultStaticCheckVersion)
}

// Install Staticcheck at the specified version
func EnsureStaticCheckAt(version string) {
if ok, _ := pkg.IsCommandAvailable("staticcheck", version); ok {
return
}

opts := archive.DownloadArchiveOptions{
DownloadOptions: downloads.DownloadOptions{
UrlTemplate: "https://github.com/dominikh/go-tools/releases/download/{{.VERSION}}/staticcheck_{{.GOOS}}_{{.GOARCH}}.tar.gz",
Name: "staticcheck",
Version: version,
},
ArchiveExtensions: map[string]string{
"linux": ".tar.gz",
"darwin": ".tar.gz",
"windows": ".tar.gz",
},
TargetFileTemplate: "staticcheck/staticcheck{{.EXT}}",
}
err := archive.DownloadToGopathBin(opts)
mgx.Must(err)
}
22 changes: 22 additions & 0 deletions tools/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ func TestEnsureKind(t *testing.T) {
require.NoError(t, err, "Error creating temp directory")
defer os.RemoveAll(tmp)

oldGoPath := os.Getenv("GOPATH")
defer os.Setenv("GOPATH", oldGoPath)

os.Setenv("GOPATH", tmp)
tools.EnsureKindAt(tools.DefaultKindVersion)
xplat.PrependPath(gopath.GetGopathBin())
Expand All @@ -29,3 +32,22 @@ func TestEnsureKind(t *testing.T) {
require.NoError(t, err, "IsCommandAvailable failed")
assert.True(t, found, "kind was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}

func TestEnsureStaticCheck(t *testing.T) {
tmp, err := ioutil.TempDir("", "magefiles")
require.NoError(t, err, "Error creating temp directory")
defer os.RemoveAll(tmp)

oldGoPath := os.Getenv("GOPATH")
defer os.Setenv("GOPATH", oldGoPath)

os.Setenv("GOPATH", tmp)
tools.EnsureStaticCheck()
xplat.PrependPath(gopath.GetGopathBin())

require.FileExists(t, filepath.Join(tmp, "bin", "staticcheck"+xplat.FileExt()))

found, err := pkg.IsCommandAvailable("staticcheck", tools.DefaultStaticCheckVersion, "--version")
require.NoError(t, err, "IsCommandAvailable failed")
assert.True(t, found, "staticcheck was not available from its location in GOPATH/bin. PATH=%s", os.Getenv("PATH"))
}

0 comments on commit abc7688

Please sign in to comment.