You've already forked goreleaser
mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-07-15 01:34:21 +02:00
refactor(test): use testing.TB Cleanup and Tempdir (#1945)
* refactor: use t.Cleanup instead of defer Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * refactor: use t.Cleanup instead of defer Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * refactor: use t.Cleanup instead of defer Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * refactor: use t.Cleanup instead of defer Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * fix: filepath Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
This commit is contained in:
committed by
GitHub
parent
4c3e3933a8
commit
92f52ac406
@ -24,8 +24,7 @@ import (
|
||||
|
||||
func TestMinioUpload(t *testing.T) {
|
||||
var listen = randomListen(t)
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var folder = t.TempDir()
|
||||
srcpath := filepath.Join(folder, "source.tar.gz")
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
@ -78,9 +77,8 @@ func TestMinioUpload(t *testing.T) {
|
||||
},
|
||||
})
|
||||
var name = "test_upload"
|
||||
defer stop(t, name)
|
||||
start(t, name, listen)
|
||||
prepareEnv(t, listen)
|
||||
prepareEnv()
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
|
||||
@ -94,15 +92,13 @@ func TestMinioUpload(t *testing.T) {
|
||||
|
||||
func TestMinioUploadCustomBucketID(t *testing.T) {
|
||||
var listen = randomListen(t)
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var folder = t.TempDir()
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
require.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
|
||||
require.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
|
||||
// Set custom BUCKET_ID env variable.
|
||||
err = os.Setenv("BUCKET_ID", "test")
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, os.Setenv("BUCKET_ID", "test"))
|
||||
var ctx = context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
@ -126,23 +122,19 @@ func TestMinioUploadCustomBucketID(t *testing.T) {
|
||||
Path: debpath,
|
||||
})
|
||||
var name = "custom_bucket_id"
|
||||
defer stop(t, name)
|
||||
start(t, name, listen)
|
||||
prepareEnv(t, listen)
|
||||
prepareEnv()
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
}
|
||||
|
||||
func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
|
||||
var listen = randomListen(t)
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var folder = t.TempDir()
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
require.NoError(t, ioutil.WriteFile(tgzpath, []byte("fake\ntargz"), 0744))
|
||||
require.NoError(t, ioutil.WriteFile(debpath, []byte("fake\ndeb"), 0744))
|
||||
// Set custom BUCKET_ID env variable.
|
||||
require.NoError(t, err)
|
||||
var ctx = context.New(config.Project{
|
||||
Dist: folder,
|
||||
ProjectName: "testupload",
|
||||
@ -166,17 +158,15 @@ func TestMinioUploadInvalidCustomBucketID(t *testing.T) {
|
||||
Path: debpath,
|
||||
})
|
||||
var name = "invalid_bucket_id"
|
||||
defer stop(t, name)
|
||||
start(t, name, listen)
|
||||
prepareEnv(t, listen)
|
||||
prepareEnv()
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.Error(t, Pipe{}.Publish(ctx))
|
||||
}
|
||||
|
||||
func TestMinioUploadSkipPublish(t *testing.T) {
|
||||
var listen = randomListen(t)
|
||||
folder, err := ioutil.TempDir("", "goreleasertest")
|
||||
require.NoError(t, err)
|
||||
var folder = t.TempDir()
|
||||
srcpath := filepath.Join(folder, "source.tar.gz")
|
||||
tgzpath := filepath.Join(folder, "bin.tar.gz")
|
||||
debpath := filepath.Join(folder, "bin.deb")
|
||||
@ -230,9 +220,8 @@ func TestMinioUploadSkipPublish(t *testing.T) {
|
||||
},
|
||||
})
|
||||
var name = "test_upload"
|
||||
defer stop(t, name)
|
||||
start(t, name, listen)
|
||||
prepareEnv(t, listen)
|
||||
prepareEnv()
|
||||
require.NoError(t, Pipe{}.Default(ctx))
|
||||
require.NoError(t, Pipe{}.Publish(ctx))
|
||||
|
||||
@ -251,17 +240,24 @@ func randomListen(t *testing.T) string {
|
||||
return listener.Addr().String()
|
||||
}
|
||||
|
||||
func prepareEnv(t *testing.T, listen string) {
|
||||
func prepareEnv() {
|
||||
os.Setenv("AWS_ACCESS_KEY_ID", "minio")
|
||||
os.Setenv("AWS_SECRET_ACCESS_KEY", "miniostorage")
|
||||
os.Setenv("AWS_REGION", "us-east-1")
|
||||
}
|
||||
|
||||
func start(t *testing.T, name, listen string) {
|
||||
func start(t testing.TB, name, listen string) {
|
||||
wd, err := os.Getwd()
|
||||
require.NoError(t, err)
|
||||
|
||||
removeTestData(t)
|
||||
removeTestData()
|
||||
|
||||
t.Cleanup(func() {
|
||||
if out, err := exec.Command("docker", "stop", name).CombinedOutput(); err != nil {
|
||||
t.Fatalf("failed to stop minio: %s", string(out))
|
||||
}
|
||||
removeTestData()
|
||||
})
|
||||
|
||||
if out, err := exec.Command(
|
||||
"docker", "run", "-d", "--rm",
|
||||
@ -291,14 +287,7 @@ func start(t *testing.T, name, listen string) {
|
||||
}
|
||||
}
|
||||
|
||||
func stop(t *testing.T, name string) {
|
||||
if out, err := exec.Command("docker", "stop", name).CombinedOutput(); err != nil {
|
||||
t.Fatalf("failed to stop minio: %s", string(out))
|
||||
}
|
||||
removeTestData(t)
|
||||
}
|
||||
|
||||
func removeTestData(t *testing.T) {
|
||||
func removeTestData() {
|
||||
_ = os.RemoveAll("./testdata/data/test/testupload") // dont care if it fails
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user