You've already forked goreleaser
							
							
				mirror of
				https://github.com/goreleaser/goreleaser.git
				synced 2025-10-30 23:58:09 +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:
		
				
					committed by
					
						 GitHub
						GitHub
					
				
			
			
				
	
			
			
			
						parent
						
							29335c84a4
						
					
				
				
					commit
					82144cb2c0
				
			| @@ -344,7 +344,7 @@ func TestChecksumFileDoesntExist(t *testing.T) { | ||||
| 		Path: file, | ||||
| 	} | ||||
| 	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) | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -758,9 +758,9 @@ func TestRunPipeWithoutMainFunc(t *testing.T) { | ||||
| 	t.Run("not main.go", func(t *testing.T) { | ||||
| 		ctx := newCtx(t) | ||||
| 		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, | ||||
| 		}), `couldn't find main file: stat foo.go: no such file or directory`) | ||||
| 		}), os.ErrNotExist) | ||||
| 	}) | ||||
| 	t.Run("glob", func(t *testing.T) { | ||||
| 		ctx := newCtx(t) | ||||
|   | ||||
| @@ -496,7 +496,7 @@ func TestRunPipe_FileNotFound(t *testing.T) { | ||||
| 	}) | ||||
|  | ||||
| 	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) { | ||||
|   | ||||
| @@ -147,9 +147,10 @@ func TestSrcInfoSimple(t *testing.T) { | ||||
|  | ||||
| func TestFullPipe(t *testing.T) { | ||||
| 	type testcase struct { | ||||
| 		prepare              func(ctx *context.Context) | ||||
| 		expectedRunError     string | ||||
| 		expectedPublishError string | ||||
| 		prepare                func(ctx *context.Context) | ||||
| 		expectedRunError       string | ||||
| 		expectedPublishError   string | ||||
| 		expectedPublishErrorIs error | ||||
| 	} | ||||
| 	for name, tt := range map[string]testcase{ | ||||
| 		"default": { | ||||
| @@ -210,7 +211,7 @@ func TestFullPipe(t *testing.T) { | ||||
| 			prepare: func(ctx *context.Context) { | ||||
| 				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": { | ||||
| 			prepare: func(ctx *context.Context) { | ||||
| @@ -322,6 +323,12 @@ func TestFullPipe(t *testing.T) { | ||||
| 				require.EqualError(t, Pipe{}.Publish(ctx), tt.expectedPublishError) | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			if tt.expectedPublishErrorIs != nil { | ||||
| 				require.ErrorIs(t, Pipe{}.Publish(ctx), tt.expectedPublishErrorIs) | ||||
| 				return | ||||
| 			} | ||||
|  | ||||
| 			require.NoError(t, Pipe{}.Publish(ctx)) | ||||
|  | ||||
| 			requireEqualRepoFiles(t, folder, name, url) | ||||
| @@ -696,7 +703,7 @@ func TestKeyPath(t *testing.T) { | ||||
| 	}) | ||||
| 	t.Run("with invalid path", func(t *testing.T) { | ||||
| 		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) | ||||
| 	}) | ||||
|  | ||||
|   | ||||
| @@ -66,19 +66,19 @@ func TestTemplatedChangelogProvidedViaFlagResultIsEmpty(t *testing.T) { | ||||
| func TestChangelogProvidedViaFlagDoesntExist(t *testing.T) { | ||||
| 	ctx := testctx.New() | ||||
| 	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) { | ||||
| 	ctx := testctx.New() | ||||
| 	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) { | ||||
| 	ctx := testctx.New() | ||||
| 	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) { | ||||
|   | ||||
| @@ -149,8 +149,7 @@ func TestPipeFileNotExist(t *testing.T) { | ||||
| 		Type: artifact.UploadableBinary, | ||||
| 	}) | ||||
| 	err := Pipe{}.Run(ctx) | ||||
| 	require.Error(t, err) | ||||
| 	require.Contains(t, err.Error(), "/nope: no such file or directory") | ||||
| 	require.ErrorIs(t, err, os.ErrNotExist) | ||||
| } | ||||
|  | ||||
| func TestPipeInvalidNameTemplate(t *testing.T) { | ||||
|   | ||||
| @@ -18,7 +18,7 @@ func TestRunWithError(t *testing.T) { | ||||
| 		Dist:        "testadata/nope", | ||||
| 		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) { | ||||
|   | ||||
| @@ -3,7 +3,6 @@ package nfpm | ||||
| import ( | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"runtime" | ||||
| 	"testing" | ||||
|  | ||||
| 	"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) { | ||||
| 		require.Contains( | ||||
| 			t, | ||||
| 			Pipe{}.Run(ctx).Error(), | ||||
| 			`open /does/not/exist_pretrans.sh: no such file or directory`, | ||||
| 		) | ||||
| 		require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("PostTrans script file does not exist", func(t *testing.T) { | ||||
| 		ctx.Config.NFPMs[0].RPM.Scripts.PreTrans = "testdata/testfile.txt" | ||||
|  | ||||
| 		require.Contains( | ||||
| 			t, | ||||
| 			Pipe{}.Run(ctx).Error(), | ||||
| 			`open /does/not/exist_posttrans.sh: no such file or directory`, | ||||
| 		) | ||||
| 		require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist) | ||||
| 	}) | ||||
|  | ||||
| 	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) { | ||||
| 		ctx.Config.NFPMs[0].APK.Scripts = scripts | ||||
| 		ctx.Config.NFPMs[0].APK.Scripts.PostUpgrade = "testdata/testfile.txt" | ||||
|  | ||||
| 		require.Contains( | ||||
| 			t, | ||||
| 			Pipe{}.Run(ctx).Error(), | ||||
| 			`stat /does/not/exist_preupgrade.sh: no such file or directory`, | ||||
| 		) | ||||
| 		require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist) | ||||
| 	}) | ||||
|  | ||||
| 	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.PreUpgrade = "testdata/testfile.txt" | ||||
|  | ||||
| 		require.Contains( | ||||
| 			t, | ||||
| 			Pipe{}.Run(ctx).Error(), | ||||
| 			`stat /does/not/exist_postupgrade.sh: no such file or directory`, | ||||
| 		) | ||||
| 		require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist) | ||||
| 	}) | ||||
|  | ||||
| 	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) { | ||||
| 		contains := "open /does/not/exist.gpg: no such file or directory" | ||||
| 		if runtime.GOOS == "windows" { | ||||
| 			contains = "open /does/not/exist.gpg: The system cannot find the path specified." | ||||
| 		} | ||||
| 		require.Contains( | ||||
| 			t, | ||||
| 			Pipe{}.Run(ctx).Error(), | ||||
| 			contains, | ||||
| 		) | ||||
| 		// TODO: once https://github.com/goreleaser/nfpm/pull/630 is released, | ||||
| 		// use require.ErrorIs() here. | ||||
| 		require.Error(t, Pipe{}.Run(ctx)) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("skip sign set", func(t *testing.T) { | ||||
|   | ||||
| @@ -84,6 +84,7 @@ func TestSignArtifacts(t *testing.T) { | ||||
| 		signatureNames   []string | ||||
| 		certificateNames []string | ||||
| 		expectedErrMsg   string | ||||
| 		expectedErrIs    error | ||||
| 		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", | ||||
| @@ -504,12 +505,12 @@ func TestSignArtifacts(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() | ||||
| 	tmpdir := tb.TempDir() | ||||
|  | ||||
| @@ -605,15 +606,21 @@ func testSign(tb testing.TB, ctx *context.Context, certificateNames, signaturePa | ||||
| 		) | ||||
| 	} | ||||
|  | ||||
| 	err := Pipe{}.Run(ctx) | ||||
|  | ||||
| 	// run the pipeline | ||||
| 	if expectedErrMsg != "" { | ||||
| 		err := Pipe{}.Run(ctx) | ||||
| 		require.Error(tb, err) | ||||
| 		require.Contains(tb, err.Error(), expectedErrMsg) | ||||
| 		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 | ||||
| 	for _, arti := range ctx.Artifacts.Filter( | ||||
|   | ||||
| @@ -472,7 +472,7 @@ func TestRunPipe_FileNotFound(t *testing.T) { | ||||
| 		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) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user