1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-10-30 23:58:09 +02:00

test: allow to locally skip some tests on missing tools (#2594)

* test: allow to locally skip some tests on missing tools

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: ci tests

* fix: helper
This commit is contained in:
Carlos Alexandro Becker
2021-10-20 21:56:27 -03:00
committed by GitHub
parent ba9b75a095
commit be6199f081
5 changed files with 86 additions and 0 deletions

View File

@@ -66,6 +66,7 @@ func killAndRm(t *testing.T) {
// TODO: this test is too big... split in smaller tests? Mainly the manifest ones...
func TestRunPipe(t *testing.T) {
testlib.CheckPath(t, "docker")
type errChecker func(*testing.T, error)
shouldErr := func(msg string) errChecker {
return func(t *testing.T, err error) {
@@ -1047,6 +1048,8 @@ func TestRunPipe(t *testing.T) {
}
func TestRunPipeWhileUsingBuildpacks(t *testing.T) {
testlib.CheckPath(t, "packs")
type errChecker func(*testing.T, error)
shouldNotErr := func(t *testing.T, err error) {
t.Helper()

View File

@@ -5,6 +5,7 @@ import (
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
@@ -47,6 +48,7 @@ func TestDockerSignInvalidArtifacts(t *testing.T) {
}
func TestDockerSignArtifacts(t *testing.T) {
testlib.CheckPath(t, "cosign")
key := "testdata/cosign/cosign.key"
cmd := "sh"
args := []string{"-c", "echo ${artifact} > ${signature} && cosign sign -key=" + key + " -upload=false ${artifact} > ${signature}"}

View File

@@ -44,6 +44,7 @@ func TestRunPipeMissingInfo(t *testing.T) {
}
func TestRunPipe(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -87,6 +88,7 @@ func TestRunPipe(t *testing.T) {
}
func TestRunPipeInvalidNameTemplate(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -110,6 +112,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
}
func TestRunPipeWithName(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -145,6 +148,7 @@ func TestRunPipeWithName(t *testing.T) {
}
func TestRunPipeMetadata(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -216,6 +220,7 @@ func TestNoSnapcraftInPath(t *testing.T) {
}
func TestRunNoArguments(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -251,6 +256,7 @@ func TestRunNoArguments(t *testing.T) {
}
func TestCompleter(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -289,6 +295,7 @@ func TestCompleter(t *testing.T) {
}
func TestCommand(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))
@@ -325,6 +332,7 @@ func TestCommand(t *testing.T) {
}
func TestExtraFile(t *testing.T) {
testlib.CheckPath(t, "snapcraft")
folder := t.TempDir()
dist := filepath.Join(folder, "dist")
require.NoError(t, os.Mkdir(dist, 0o755))

19
internal/testlib/path.go Normal file
View File

@@ -0,0 +1,19 @@
package testlib
import (
"os"
"os/exec"
"testing"
)
// CheckPath skips the test if the binary is not in the PATH.
func CheckPath(tb testing.TB, cmd string) {
tb.Helper()
if os.Getenv("CI") == "true" {
// never skip on CI
return
}
if _, err := exec.LookPath(cmd); err != nil {
tb.Skipf("%s not in PATH", cmd)
}
}

View File

@@ -0,0 +1,54 @@
package testlib
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestCheckPath(t *testing.T) {
requireSkipped := func(tb testing.TB, skipped bool) {
tb.Helper()
t.Cleanup(func() {
require.Equalf(tb, skipped, tb.Skipped(), "expected skipped to be %v", skipped)
})
}
setupEnv := func(tb testing.TB, value string) {
tb.Helper()
previous := os.Getenv("CI")
require.NoError(tb, os.Setenv("CI", value))
tb.Cleanup(func() {
require.NoError(tb, os.Setenv("CI", previous))
})
}
t.Run("local", func(t *testing.T) {
setupEnv(t, "false")
t.Run("in path", func(t *testing.T) {
requireSkipped(t, false)
CheckPath(t, "echo")
})
t.Run("not in path", func(t *testing.T) {
requireSkipped(t, true)
CheckPath(t, "do-not-exist")
})
})
t.Run("CI", func(t *testing.T) {
setupEnv(t, "true")
t.Run("in path on CI", func(t *testing.T) {
requireSkipped(t, false)
CheckPath(t, "echo")
})
t.Run("not in path on CI", func(t *testing.T) {
requireSkipped(t, false)
CheckPath(t, "do-not-exist")
})
})
}