github.com/goreleaser/goreleaser@v1.25.1/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/testctx"
    10  	"github.com/goreleaser/goreleaser/internal/testlib"
    11  	"github.com/goreleaser/goreleaser/pkg/config"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestArchive(t *testing.T) {
    17  	for _, format := range []string{"tar.gz", "tar", "zip"} {
    18  		t.Run(format, func(t *testing.T) {
    19  			tmp := testlib.Mktmp(t)
    20  			require.NoError(t, os.Mkdir("dist", 0o744))
    21  
    22  			testlib.GitInit(t)
    23  			require.NoError(t, os.WriteFile("code.rb", []byte("not really code"), 0o655))
    24  			require.NoError(t, os.WriteFile("code.py", []byte("print 1"), 0o655))
    25  			require.NoError(t, os.WriteFile("README.md", []byte("# my dope fake project"), 0o655))
    26  			require.NoError(t, os.WriteFile("ملف.go", []byte("محتوى عربي"), 0o655))
    27  			require.NoError(t, os.WriteFile("🤔.patch", []byte("thinking"), 0o655))
    28  			require.NoError(t, os.WriteFile(".gitignore", []byte(`
    29  added-later.txt
    30  ignored.txt
    31  code.txt
    32  subfolder/
    33  			`), 0o655))
    34  
    35  			require.NoError(t, os.WriteFile(".gitattributes", []byte(`
    36  .VERSION export-subst
    37  			`), 0o655))
    38  			require.NoError(t, os.WriteFile(".VERSION", []byte("$Format:%d$"), 0o655))
    39  			testlib.GitAdd(t)
    40  			testlib.GitCommit(t, "feat: first")
    41  			testlib.GitTag(t, "v1.0.0")
    42  			require.NoError(t, os.WriteFile("added-later.txt", []byte("this file was added later"), 0o655))
    43  			require.NoError(t, os.WriteFile("ignored.md", []byte("never added"), 0o655))
    44  			require.NoError(t, os.WriteFile("code.txt", []byte("not really code"), 0o655))
    45  			require.NoError(t, os.WriteFile("ملف.txt", []byte("محتوى عربي"), 0o655))
    46  			require.NoError(t, os.WriteFile("🤝", []byte("it works"), 0o655))
    47  			require.NoError(t, os.MkdirAll("subfolder", 0o755))
    48  			require.NoError(t, os.WriteFile("subfolder/file.md", []byte("a file within a folder, added later"), 0o655))
    49  
    50  			t.Run("with extra files", func(t *testing.T) {
    51  				doVerifyTestArchive(
    52  					t,
    53  					testctx.NewWithCfg(
    54  						config.Project{
    55  							ProjectName: "foo",
    56  							Dist:        "dist",
    57  							Source: config.Source{
    58  								Format:         format,
    59  								Enabled:        true,
    60  								PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
    61  								Files: []config.File{
    62  									{Source: "*.txt"},
    63  									{Source: "subfolder/*"},
    64  								},
    65  							},
    66  						},
    67  						testctx.WithCommit("HEAD"),
    68  						testctx.WithVersion("1.0.0"),
    69  						testctx.WithCurrentTag("v1.0.0"),
    70  					),
    71  					tmp,
    72  					format,
    73  					[]string{
    74  						"foo-1.0.0/",
    75  						"foo-1.0.0/.VERSION",
    76  						"foo-1.0.0/.gitattributes",
    77  						"foo-1.0.0/.gitignore",
    78  						"foo-1.0.0/README.md",
    79  						"foo-1.0.0/added-later.txt",
    80  						"foo-1.0.0/code.py",
    81  						"foo-1.0.0/code.rb",
    82  						"foo-1.0.0/code.txt",
    83  						"foo-1.0.0/subfolder/file.md",
    84  						"foo-1.0.0/ملف.go",
    85  						"foo-1.0.0/ملف.txt",
    86  						"foo-1.0.0/🤔.patch",
    87  					},
    88  				)
    89  			})
    90  
    91  			t.Run("simple", func(t *testing.T) {
    92  				doVerifyTestArchive(
    93  					t,
    94  					testctx.NewWithCfg(
    95  						config.Project{
    96  							ProjectName: "foo",
    97  							Dist:        "dist",
    98  							Source: config.Source{
    99  								Format:         format,
   100  								Enabled:        true,
   101  								PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
   102  							},
   103  						},
   104  						testctx.WithCommit("HEAD"),
   105  						testctx.WithVersion("1.0.0"),
   106  						testctx.WithCurrentTag("v1.0.0"),
   107  					),
   108  					tmp,
   109  					format,
   110  					[]string{
   111  						"foo-1.0.0/",
   112  						"foo-1.0.0/.VERSION",
   113  						"foo-1.0.0/.gitattributes",
   114  						"foo-1.0.0/.gitignore",
   115  						"foo-1.0.0/README.md",
   116  						"foo-1.0.0/code.py",
   117  						"foo-1.0.0/code.rb",
   118  						"foo-1.0.0/ملف.go",
   119  						"foo-1.0.0/🤔.patch",
   120  					},
   121  				)
   122  			})
   123  		})
   124  	}
   125  }
   126  
   127  func doVerifyTestArchive(tb testing.TB, ctx *context.Context, tmp, format string, expected []string) {
   128  	tb.Helper()
   129  	require.NoError(tb, Pipe{}.Default(ctx))
   130  	require.NoError(tb, Pipe{}.Run(ctx))
   131  
   132  	artifacts := ctx.Artifacts.List()
   133  	require.Len(tb, artifacts, 1)
   134  	require.Equal(tb, artifact.Artifact{
   135  		Type: artifact.UploadableSourceArchive,
   136  		Name: "foo-1.0.0." + format,
   137  		Path: "dist/foo-1.0.0." + format,
   138  		Extra: map[string]interface{}{
   139  			artifact.ExtraFormat: format,
   140  		},
   141  	}, *artifacts[0])
   142  	path := filepath.Join(tmp, "dist", "foo-1.0.0."+format)
   143  	stat, err := os.Stat(path)
   144  	require.NoError(tb, err)
   145  	require.Greater(tb, stat.Size(), int64(100))
   146  
   147  	require.ElementsMatch(tb, expected, testlib.LsArchive(tb, path, format))
   148  
   149  	version := testlib.GetFileFromArchive(tb, path, format, "foo-1.0.0/.VERSION")
   150  	require.Equal(tb, " (HEAD -> main, tag: v1.0.0)", string(version))
   151  }
   152  
   153  func TestInvalidFormat(t *testing.T) {
   154  	ctx := testctx.NewWithCfg(config.Project{
   155  		Dist:        t.TempDir(),
   156  		ProjectName: "foo",
   157  		Source: config.Source{
   158  			Format:         "7z",
   159  			Enabled:        true,
   160  			PrefixTemplate: "{{ .ProjectName }}-{{ .Version }}/",
   161  		},
   162  	})
   163  	require.NoError(t, Pipe{}.Default(ctx))
   164  	require.EqualError(t, Pipe{}.Run(ctx), "invalid source archive format: 7z")
   165  }
   166  
   167  func TestDefault(t *testing.T) {
   168  	ctx := testctx.New()
   169  	require.NoError(t, Pipe{}.Default(ctx))
   170  	require.Equal(t, config.Source{
   171  		NameTemplate: "{{ .ProjectName }}-{{ .Version }}",
   172  		Format:       "tar.gz",
   173  	}, ctx.Config.Source)
   174  }
   175  
   176  func TestInvalidNameTemplate(t *testing.T) {
   177  	ctx := testctx.NewWithCfg(config.Project{
   178  		Source: config.Source{
   179  			Enabled:      true,
   180  			NameTemplate: "{{ .foo }-asdda",
   181  		},
   182  	})
   183  	require.NoError(t, Pipe{}.Default(ctx))
   184  	testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   185  }
   186  
   187  func TestInvalidInvalidFileTemplate(t *testing.T) {
   188  	testlib.Mktmp(t)
   189  	require.NoError(t, os.Mkdir("dist", 0o744))
   190  
   191  	testlib.GitInit(t)
   192  	require.NoError(t, os.WriteFile("code.txt", []byte("not really code"), 0o655))
   193  	testlib.GitAdd(t)
   194  	testlib.GitCommit(t, "feat: first")
   195  
   196  	ctx := testctx.NewWithCfg(config.Project{
   197  		ProjectName: "foo",
   198  		Dist:        "dist",
   199  		Source: config.Source{
   200  			Format:  "tar.gz",
   201  			Enabled: true,
   202  			Files: []config.File{
   203  				{Source: "{{.Test}"},
   204  			},
   205  		},
   206  	})
   207  	ctx.Git.FullCommit = "HEAD"
   208  	ctx.Version = "1.0.0"
   209  	require.NoError(t, Pipe{}.Default(ctx))
   210  	testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   211  }
   212  
   213  func TestInvalidPrefixTemplate(t *testing.T) {
   214  	ctx := testctx.NewWithCfg(config.Project{
   215  		Dist: t.TempDir(),
   216  		Source: config.Source{
   217  			Enabled:        true,
   218  			PrefixTemplate: "{{ .ProjectName }/",
   219  		},
   220  	})
   221  	require.NoError(t, Pipe{}.Default(ctx))
   222  	testlib.RequireTemplateError(t, Pipe{}.Run(ctx))
   223  }
   224  
   225  func TestDisabled(t *testing.T) {
   226  	require.True(t, Pipe{}.Skip(testctx.New()))
   227  }
   228  
   229  func TestSkip(t *testing.T) {
   230  	t.Run("skip", func(t *testing.T) {
   231  		require.True(t, Pipe{}.Skip(testctx.New()))
   232  	})
   233  
   234  	t.Run("dont skip", func(t *testing.T) {
   235  		ctx := testctx.NewWithCfg(config.Project{
   236  			Source: config.Source{
   237  				Enabled: true,
   238  			},
   239  		})
   240  		require.False(t, Pipe{}.Skip(ctx))
   241  	})
   242  }
   243  
   244  func TestString(t *testing.T) {
   245  	require.NotEmpty(t, Pipe{}.String())
   246  }