github.com/triarius/goreleaser@v1.12.5/internal/pipe/metadata/metadata_test.go (about)

     1  package metadata
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/triarius/goreleaser/internal/artifact"
    10  	"github.com/triarius/goreleaser/internal/golden"
    11  	"github.com/triarius/goreleaser/pkg/config"
    12  	"github.com/triarius/goreleaser/pkg/context"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestRunWithError(t *testing.T) {
    17  	ctx := context.New(config.Project{
    18  		Dist:        "testadata/nope",
    19  		ProjectName: "foo",
    20  	})
    21  	require.EqualError(t, Pipe{}.Run(ctx), `open testadata/nope/artifacts.json: no such file or directory`)
    22  }
    23  
    24  func TestRun(t *testing.T) {
    25  	tmp := t.TempDir()
    26  	ctx := context.New(config.Project{
    27  		Dist:        tmp,
    28  		ProjectName: "name",
    29  	})
    30  	ctx.Runtime = context.Runtime{
    31  		Goos:   "fakeos",
    32  		Goarch: "fakearch",
    33  	}
    34  	ctx.Version = "1.2.3"
    35  	ctx.Git = context.GitInfo{
    36  		CurrentTag:  "v1.2.3",
    37  		PreviousTag: "v1.2.2",
    38  		Commit:      "aef34a",
    39  	}
    40  	ctx.Date = time.Date(2022, 0o1, 22, 10, 12, 13, 0, time.UTC)
    41  	ctx.Artifacts.Add(&artifact.Artifact{
    42  		Name:   "foo",
    43  		Path:   "foo.txt",
    44  		Type:   artifact.Binary,
    45  		Goos:   "darwin",
    46  		Goarch: "amd64",
    47  		Goarm:  "7",
    48  		Extra: map[string]interface{}{
    49  			"foo": "bar",
    50  		},
    51  	})
    52  
    53  	require.NoError(t, Pipe{}.Run(ctx))
    54  	t.Run("artifacts", func(t *testing.T) {
    55  		requireEqualJSONFile(t, tmp, "artifacts.json")
    56  	})
    57  	t.Run("metadata", func(t *testing.T) {
    58  		requireEqualJSONFile(t, tmp, "metadata.json")
    59  	})
    60  }
    61  
    62  func requireEqualJSONFile(tb testing.TB, tmp, s string) {
    63  	tb.Helper()
    64  	path := filepath.Join(tmp, s)
    65  	golden.RequireEqualJSON(tb, golden.RequireReadFile(tb, path))
    66  
    67  	info, err := os.Stat(path)
    68  	require.NoError(tb, err)
    69  	require.Equal(tb, "-rw-r--r--", info.Mode().String())
    70  }