github.com/windmeup/goreleaser@v1.21.95/internal/pipe/metadata/metadata_test.go (about) 1 package metadata 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strconv" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 "github.com/windmeup/goreleaser/internal/artifact" 13 "github.com/windmeup/goreleaser/internal/golden" 14 "github.com/windmeup/goreleaser/internal/testctx" 15 "github.com/windmeup/goreleaser/internal/testlib" 16 "github.com/windmeup/goreleaser/pkg/config" 17 "github.com/windmeup/goreleaser/pkg/context" 18 ) 19 20 func TestRunWithError(t *testing.T) { 21 ctx := testctx.NewWithCfg(config.Project{ 22 Dist: "testadata/nope", 23 ProjectName: "foo", 24 }) 25 require.ErrorIs(t, Pipe{}.Run(ctx), os.ErrNotExist) 26 } 27 28 func TestRun(t *testing.T) { 29 modTime := time.Now().AddDate(-1, 0, 0).Round(1 * time.Second).UTC() 30 31 getCtx := func(tmp string) *context.Context { 32 ctx := testctx.NewWithCfg( 33 config.Project{ 34 Dist: tmp, 35 ProjectName: "name", 36 Metadata: config.ProjectMetadata{ 37 ModTimestamp: "{{.Env.MOD_TS}}", 38 }, 39 }, 40 testctx.WithPreviousTag("v1.2.2"), 41 testctx.WithCurrentTag("v1.2.3"), 42 testctx.WithCommit("aef34a"), 43 testctx.WithVersion("1.2.3"), 44 testctx.WithDate(time.Date(2022, 0o1, 22, 10, 12, 13, 0, time.UTC)), 45 testctx.WithFakeRuntime, 46 testctx.WithEnv(map[string]string{ 47 "MOD_TS": fmt.Sprintf("%d", modTime.Unix()), 48 }), 49 ) 50 ctx.Artifacts.Add(&artifact.Artifact{ 51 Name: "foo", 52 Path: "foo.txt", 53 Type: artifact.Binary, 54 Goos: "darwin", 55 Goarch: "amd64", 56 Goarm: "7", 57 Extra: map[string]interface{}{ 58 "foo": "bar", 59 }, 60 }) 61 return ctx 62 } 63 64 t.Run("artifacts", func(t *testing.T) { 65 tmp := t.TempDir() 66 ctx := getCtx(tmp) 67 require.NoError(t, Pipe{}.Run(ctx)) 68 requireEqualJSONFile(t, tmp, "artifacts.json", modTime) 69 }) 70 t.Run("metadata", func(t *testing.T) { 71 tmp := t.TempDir() 72 ctx := getCtx(tmp) 73 require.NoError(t, Pipe{}.Run(ctx)) 74 requireEqualJSONFile(t, tmp, "metadata.json", modTime) 75 }) 76 77 t.Run("invalid mod metadata", func(t *testing.T) { 78 tmp := t.TempDir() 79 ctx := getCtx(tmp) 80 ctx.Config.Metadata.ModTimestamp = "not a number" 81 require.ErrorIs(t, Pipe{}.Run(ctx), strconv.ErrSyntax) 82 }) 83 84 t.Run("invalid mod metadata tmpl", func(t *testing.T) { 85 tmp := t.TempDir() 86 ctx := getCtx(tmp) 87 ctx.Config.Metadata.ModTimestamp = "{{.Nope}}" 88 testlib.RequireTemplateError(t, Pipe{}.Run(ctx)) 89 }) 90 } 91 92 func requireEqualJSONFile(tb testing.TB, tmp, s string, modTime time.Time) { 93 tb.Helper() 94 path := filepath.Join(tmp, s) 95 golden.RequireEqualJSON(tb, golden.RequireReadFile(tb, path)) 96 stat, err := os.Stat(path) 97 require.NoError(tb, err) 98 require.Equal(tb, modTime.Unix(), stat.ModTime().Unix()) 99 }