1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-04-15 11:56:56 +02:00

test: improve file not found checkings (#3831)

using `errors.Is` everywhere, as file not found errors have different
messages on different OSes.
This commit is contained in:
Carlos Alexandro Becker 2023-03-04 12:16:26 -03:00 committed by GitHub
parent 29335c84a4
commit 82144cb2c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 41 additions and 54 deletions

View File

@ -344,7 +344,7 @@ func TestChecksumFileDoesntExist(t *testing.T) {
Path: file, Path: file,
} }
sum, err := artifact.Checksum("sha1") sum, err := artifact.Checksum("sha1")
require.EqualError(t, err, fmt.Sprintf(`failed to checksum: open %s: no such file or directory`, file)) require.ErrorIs(t, err, os.ErrNotExist)
require.Empty(t, sum) require.Empty(t, sum)
} }

View File

@ -758,9 +758,9 @@ func TestRunPipeWithoutMainFunc(t *testing.T) {
t.Run("not main.go", func(t *testing.T) { t.Run("not main.go", func(t *testing.T) {
ctx := newCtx(t) ctx := newCtx(t)
ctx.Config.Builds[0].Main = "foo.go" ctx.Config.Builds[0].Main = "foo.go"
require.EqualError(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{ require.ErrorIs(t, Default.Build(ctx, ctx.Config.Builds[0], api.Options{
Target: runtimeTarget, Target: runtimeTarget,
}), `couldn't find main file: stat foo.go: no such file or directory`) }), os.ErrNotExist)
}) })
t.Run("glob", func(t *testing.T) { t.Run("glob", func(t *testing.T) {
ctx := newCtx(t) ctx := newCtx(t)

View File

@ -496,7 +496,7 @@ func TestRunPipe_FileNotFound(t *testing.T) {
}) })
require.NoError(t, Pipe{}.Default(ctx)) require.NoError(t, Pipe{}.Default(ctx))
require.EqualError(t, Pipe{}.Publish(ctx), `open archivetest/dist/mybin/mybin: no such file or directory`) require.ErrorIs(t, Pipe{}.Publish(ctx), os.ErrNotExist)
} }
func TestRunPipe_UnparsableTarget(t *testing.T) { func TestRunPipe_UnparsableTarget(t *testing.T) {

View File

@ -147,9 +147,10 @@ func TestSrcInfoSimple(t *testing.T) {
func TestFullPipe(t *testing.T) { func TestFullPipe(t *testing.T) {
type testcase struct { type testcase struct {
prepare func(ctx *context.Context) prepare func(ctx *context.Context)
expectedRunError string expectedRunError string
expectedPublishError string expectedPublishError string
expectedPublishErrorIs error
} }
for name, tt := range map[string]testcase{ for name, tt := range map[string]testcase{
"default": { "default": {
@ -210,7 +211,7 @@ func TestFullPipe(t *testing.T) {
prepare: func(ctx *context.Context) { prepare: func(ctx *context.Context) {
ctx.Config.AURs[0].PrivateKey = "testdata/nope" ctx.Config.AURs[0].PrivateKey = "testdata/nope"
}, },
expectedPublishError: `could not stat aur.private_key: stat testdata/nope: no such file or directory`, expectedPublishErrorIs: os.ErrNotExist,
}, },
"invalid-git-url-template": { "invalid-git-url-template": {
prepare: func(ctx *context.Context) { prepare: func(ctx *context.Context) {
@ -322,6 +323,12 @@ func TestFullPipe(t *testing.T) {
require.EqualError(t, Pipe{}.Publish(ctx), tt.expectedPublishError) require.EqualError(t, Pipe{}.Publish(ctx), tt.expectedPublishError)
return return
} }
if tt.expectedPublishErrorIs != nil {
require.ErrorIs(t, Pipe{}.Publish(ctx), tt.expectedPublishErrorIs)
return
}
require.NoError(t, Pipe{}.Publish(ctx)) require.NoError(t, Pipe{}.Publish(ctx))
requireEqualRepoFiles(t, folder, name, url) requireEqualRepoFiles(t, folder, name, url)
@ -696,7 +703,7 @@ func TestKeyPath(t *testing.T) {
}) })
t.Run("with invalid path", func(t *testing.T) { t.Run("with invalid path", func(t *testing.T) {
result, err := keyPath("testdata/nope") result, err := keyPath("testdata/nope")
require.EqualError(t, err, `could not stat aur.private_key: stat testdata/nope: no such file or directory`) require.ErrorIs(t, err, os.ErrNotExist)
require.Equal(t, "", result) require.Equal(t, "", result)
}) })

View File

@ -66,19 +66,19 @@ func TestTemplatedChangelogProvidedViaFlagResultIsEmpty(t *testing.T) {
func TestChangelogProvidedViaFlagDoesntExist(t *testing.T) { func TestChangelogProvidedViaFlagDoesntExist(t *testing.T) {
ctx := testctx.New() ctx := testctx.New()
ctx.ReleaseNotesFile = "testdata/changes.nope" ctx.ReleaseNotesFile = "testdata/changes.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/changes.nope: no such file or directory") require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
} }
func TestReleaseHeaderProvidedViaFlagDoesntExist(t *testing.T) { func TestReleaseHeaderProvidedViaFlagDoesntExist(t *testing.T) {
ctx := testctx.New() ctx := testctx.New()
ctx.ReleaseHeaderFile = "testdata/header.nope" ctx.ReleaseHeaderFile = "testdata/header.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/header.nope: no such file or directory") require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
} }
func TestReleaseFooterProvidedViaFlagDoesntExist(t *testing.T) { func TestReleaseFooterProvidedViaFlagDoesntExist(t *testing.T) {
ctx := testctx.New() ctx := testctx.New()
ctx.ReleaseFooterFile = "testdata/footer.nope" ctx.ReleaseFooterFile = "testdata/footer.nope"
require.EqualError(t, Pipe{}.Run(ctx), "open testdata/footer.nope: no such file or directory") require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
} }
func TestChangelog(t *testing.T) { func TestChangelog(t *testing.T) {

View File

@ -149,8 +149,7 @@ func TestPipeFileNotExist(t *testing.T) {
Type: artifact.UploadableBinary, Type: artifact.UploadableBinary,
}) })
err := Pipe{}.Run(ctx) err := Pipe{}.Run(ctx)
require.Error(t, err) require.ErrorIs(t, err, os.ErrNotExist)
require.Contains(t, err.Error(), "/nope: no such file or directory")
} }
func TestPipeInvalidNameTemplate(t *testing.T) { func TestPipeInvalidNameTemplate(t *testing.T) {

View File

@ -18,7 +18,7 @@ func TestRunWithError(t *testing.T) {
Dist: "testadata/nope", Dist: "testadata/nope",
ProjectName: "foo", ProjectName: "foo",
}) })
require.EqualError(t, Pipe{}.Run(ctx), `open testadata/nope/artifacts.json: no such file or directory`) require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
} }
func TestRun(t *testing.T) { func TestRun(t *testing.T) {

View File

@ -3,7 +3,6 @@ package nfpm
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"runtime"
"testing" "testing"
"github.com/goreleaser/goreleaser/internal/artifact" "github.com/goreleaser/goreleaser/internal/artifact"
@ -981,21 +980,12 @@ func TestRPMSpecificScriptsConfig(t *testing.T) {
} }
t.Run("PreTrans script file does not exist", func(t *testing.T) { t.Run("PreTrans script file does not exist", func(t *testing.T) {
require.Contains( require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
t,
Pipe{}.Run(ctx).Error(),
`open /does/not/exist_pretrans.sh: no such file or directory`,
)
}) })
t.Run("PostTrans script file does not exist", func(t *testing.T) { t.Run("PostTrans script file does not exist", func(t *testing.T) {
ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt" ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt"
require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`open /does/not/exist_posttrans.sh: no such file or directory`,
)
}) })
t.Run("pretrans and posttrans scriptlets set", func(t *testing.T) { t.Run("pretrans and posttrans scriptlets set", func(t *testing.T) {
@ -1134,23 +1124,13 @@ func TestAPKSpecificScriptsConfig(t *testing.T) {
t.Run("PreUpgrade script file does not exist", func(t *testing.T) { t.Run("PreUpgrade script file does not exist", func(t *testing.T) {
ctx.Config.NFPMs[0].APK.Scripts = scripts ctx.Config.NFPMs[0].APK.Scripts = scripts
ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt" ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt"
require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`stat /does/not/exist_preupgrade.sh: no such file or directory`,
)
}) })
t.Run("PostUpgrade script file does not exist", func(t *testing.T) { t.Run("PostUpgrade script file does not exist", func(t *testing.T) {
ctx.Config.NFPMs[0].APK.Scripts = scripts ctx.Config.NFPMs[0].APK.Scripts = scripts
ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt" ctx.Config.NFPMs[0].APK.Scripts.PreUpgrade = "testdata/testfile.txt"
require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist)
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
`stat /does/not/exist_postupgrade.sh: no such file or directory`,
)
}) })
t.Run("preupgrade and postupgrade scriptlets set", func(t *testing.T) { t.Run("preupgrade and postupgrade scriptlets set", func(t *testing.T) {
@ -1333,15 +1313,9 @@ func TestSkipSign(t *testing.T) {
} }
t.Run("skip sign not set", func(t *testing.T) { t.Run("skip sign not set", func(t *testing.T) {
contains := "open /does/not/exist.gpg: no such file or directory" // TODO: once https://github.com/goreleaser/nfpm/pull/630 is released,
if runtime.GOOS == "windows" { // use require.ErrorIs() here.
contains = "open /does/not/exist.gpg: The system cannot find the path specified." require.Error(t, Pipe{}.Run(ctx))
}
require.Contains(
t,
Pipe{}.Run(ctx).Error(),
contains,
)
}) })
t.Run("skip sign set", func(t *testing.T) { t.Run("skip sign set", func(t *testing.T) {

View File

@ -84,6 +84,7 @@ func TestSignArtifacts(t *testing.T) {
signatureNames []string signatureNames []string
certificateNames []string certificateNames []string
expectedErrMsg string expectedErrMsg string
expectedErrIs error
user string user string
}{ }{
{ {
@ -465,7 +466,7 @@ func TestSignArtifacts(t *testing.T) {
}, },
}, },
}), }),
expectedErrMsg: `sign failed: cannot open file /tmp/non-existing-file: open /tmp/non-existing-file: no such file or directory`, expectedErrIs: os.ErrNotExist,
}, },
{ {
desc: "sign creating certificate", desc: "sign creating certificate",
@ -504,12 +505,12 @@ func TestSignArtifacts(t *testing.T) {
} }
t.Run(test.desc, func(t *testing.T) { t.Run(test.desc, func(t *testing.T) {
testSign(t, test.ctx, test.certificateNames, test.signaturePaths, test.signatureNames, test.user, test.expectedErrMsg) testSign(t, test.ctx, test.certificateNames, test.signaturePaths, test.signatureNames, test.user, test.expectedErrMsg, test.expectedErrIs)
}) })
} }
} }
func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePaths, signatureNames []string, user, expectedErrMsg string) { func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePaths, signatureNames []string, user, expectedErrMsg string, expectedErrIs error) {
tb.Helper() tb.Helper()
tmpdir := tb.TempDir() tmpdir := tb.TempDir()
@ -605,15 +606,21 @@ func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePa
) )
} }
err := Pipe{}.Run(ctx)
// run the pipeline // run the pipeline
if expectedErrMsg != "" { if expectedErrMsg != "" {
err := Pipe{}.Run(ctx)
require.Error(tb, err) require.Error(tb, err)
require.Contains(tb, err.Error(), expectedErrMsg) require.Contains(tb, err.Error(), expectedErrMsg)
return return
} }
require.NoError(tb, Pipe{}.Run(ctx)) if expectedErrIs != nil {
require.ErrorIs(tb, err, expectedErrIs)
return
}
require.NoError(tb, err)
// ensure all artifacts have an ID // ensure all artifacts have an ID
for _, arti := range ctx.Artifacts.Filter( for _, arti := range ctx.Artifacts.Filter(

View File

@ -472,7 +472,7 @@ func TestRunPipe_FileNotFound(t *testing.T) {
Type: artifact.UploadableBinary, Type: artifact.UploadableBinary,
}) })
require.EqualError(t, Pipe{}.Publish(ctx), `open archivetest/dist/mybin/mybin: no such file or directory`) require.ErrorIs(t, Pipe{}.Publish(ctx), os.ErrNotExist)
} }
func TestRunPipe_UnparsableTarget(t *testing.T) { func TestRunPipe_UnparsableTarget(t *testing.T) {