github.com/ahmet2mir/goreleaser@v0.180.3-0.20210927151101-8e5ee5a9b8c5/internal/pipe/sourcearchive/source_test.go (about)

     1  package sourcearchive
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/goreleaser/goreleaser/internal/artifact"
     9  	"github.com/goreleaser/goreleaser/internal/testlib"
    10  	"github.com/goreleaser/goreleaser/pkg/config"
    11  	"github.com/goreleaser/goreleaser/pkg/context"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestArchive(t *testing.T) {
    16  	for _, format := range []string{"tar.gz", "tar", "zip"} {
    17  		t.Run(format, func(t *testing.T) {
    18  			tmp := testlib.Mktmp(t)
    19  			require.NoError(t, os.Mkdir("dist", 0o744))
    20  
    21  			testlib.GitInit(t)
    22  			require.NoError(t, os.WriteFile("code.txt", []byte("not really code"), 0o655))
    23  			require.NoError(t, os.WriteFile("README.md", []byte("# my dope fake project"), 0o655))
    24  			testlib.GitAdd(t)
    25  			testlib.GitCommit(t, "feat: first")
    26  
    27  			ctx := context.New(config.Project{
    28  				ProjectName: "foo",
    29  				Dist:        "dist",
    30  				Source: config.Source{
    31  					Format:  format,
    32  					Enabled: true,
    33  				},
    34  			})
    35  			ctx.Git.FullCommit = "HEAD"
    36  			ctx.Version = "1.0.0"
    37  
    38  			require.NoError(t, Pipe{}.Default(ctx))
    39  			require.NoError(t, Pipe{}.Run(ctx))
    40  
    41  			artifacts := ctx.Artifacts.List()
    42  			require.Len(t, artifacts, 1)
    43  			require.Equal(t, artifact.Artifact{
    44  				Type: artifact.UploadableSourceArchive,
    45  				Name: "foo-1.0.0." + format,
    46  				Path: "dist/foo-1.0.0." + format,
    47  				Extra: map[string]interface{}{
    48  					"Format": format,
    49  				},
    50  			}, *artifacts[0])
    51  			stat, err := os.Stat(filepath.Join(tmp, "dist", "foo-1.0.0."+format))
    52  			require.NoError(t, err)
    53  			require.Greater(t, stat.Size(), int64(100))
    54  		})
    55  	}
    56  }
    57  
    58  func TestDefault(t *testing.T) {
    59  	ctx := context.New(config.Project{})
    60  	require.NoError(t, Pipe{}.Default(ctx))
    61  	require.Equal(t, config.Source{
    62  		NameTemplate: "{{ .ProjectName }}-{{ .Version }}",
    63  		Format:       "tar.gz",
    64  	}, ctx.Config.Source)
    65  }
    66  
    67  func TestInvalidNameTemplate(t *testing.T) {
    68  	ctx := context.New(config.Project{
    69  		Source: config.Source{
    70  			Enabled:      true,
    71  			NameTemplate: "{{ .foo }-asdda",
    72  		},
    73  	})
    74  	require.EqualError(t, Pipe{}.Run(ctx), "template: tmpl:1: unexpected \"}\" in operand")
    75  }
    76  
    77  func TestDisabled(t *testing.T) {
    78  	require.True(t, Pipe{}.Skip(context.New(config.Project{})))
    79  }
    80  
    81  func TestSkip(t *testing.T) {
    82  	t.Run("skip", func(t *testing.T) {
    83  		require.True(t, Pipe{}.Skip(context.New(config.Project{})))
    84  	})
    85  
    86  	t.Run("dont skip", func(t *testing.T) {
    87  		ctx := context.New(config.Project{
    88  			Source: config.Source{
    89  				Enabled: true,
    90  			},
    91  		})
    92  		require.False(t, Pipe{}.Skip(ctx))
    93  	})
    94  }