2020-05-25 20:07:40 +02:00
package extrafiles
import (
"testing"
"github.com/goreleaser/goreleaser/pkg/config"
2021-11-22 19:53:08 +02:00
"github.com/goreleaser/goreleaser/pkg/context"
2020-10-06 14:48:04 +02:00
"github.com/stretchr/testify/require"
2020-05-25 20:07:40 +02:00
)
2021-11-22 19:53:08 +02:00
func TestTemplate ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./testdata/file{{ .Env.ONE }}.golden" } ,
}
ctx := context . New ( config . Project { } )
ctx . Env [ "ONE" ] = "1"
files , err := Find ( ctx , globs )
require . NoError ( t , err )
require . Len ( t , files , 1 )
require . Equal ( t , "testdata/file1.golden" , files [ "file1.golden" ] )
}
func TestBadTemplate ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./testdata/file{{ .Env.NOPE }}.golden" } ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . EqualError ( t , err , ` failed to apply template to glob "./testdata/file {{ .Env .NOPE }} .golden": template: tmpl:1:22: executing "tmpl" at <.Env.NOPE>: map has no entry for key "NOPE" ` )
}
2020-11-10 16:20:55 +02:00
func TestShouldGetSpecificFile ( t * testing . T ) {
2020-05-25 20:07:40 +02:00
globs := [ ] config . ExtraFile {
2021-11-22 19:53:08 +02:00
{ } , // empty glob, will be ignored
{ Glob : "./testdata/sub3" } , // will get a file1.golden as well, but will be overridden
2020-05-25 20:07:40 +02:00
{ Glob : "./testdata/file1.golden" } ,
}
2021-11-22 19:53:08 +02:00
files , err := Find ( context . New ( config . Project { } ) , globs )
2020-10-06 14:48:04 +02:00
require . NoError ( t , err )
2020-11-10 16:20:55 +02:00
require . Len ( t , files , 1 )
2020-05-25 20:07:40 +02:00
2020-11-10 16:20:55 +02:00
require . Equal ( t , "testdata/file1.golden" , files [ "file1.golden" ] )
}
func TestFailToGetSpecificFile ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./testdata/file453.golden" } ,
}
2021-11-22 19:53:08 +02:00
files , err := Find ( context . New ( config . Project { } ) , globs )
2021-03-01 19:19:13 +02:00
require . EqualError ( t , err , "globbing failed for pattern ./testdata/file453.golden: matching \"./testdata/file453.golden\": file does not exist" )
2020-11-10 16:20:55 +02:00
require . Empty ( t , files )
}
func TestShouldGetFilesWithSuperStar ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./**/file?.golden" } ,
}
2021-11-22 19:53:08 +02:00
files , err := Find ( context . New ( config . Project { } ) , globs )
2020-11-10 16:20:55 +02:00
require . NoError ( t , err )
require . Len ( t , files , 3 )
require . Equal ( t , "testdata/file2.golden" , files [ "file2.golden" ] )
2021-11-22 19:53:08 +02:00
require . Equal ( t , "testdata/sub3/file1.golden" , files [ "file1.golden" ] )
2020-11-10 16:20:55 +02:00
require . Equal ( t , "testdata/sub/file5.golden" , files [ "file5.golden" ] )
2020-05-25 20:07:40 +02:00
}
func TestShouldGetAllFilesWithGoldenExtension ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./testdata/*.golden" } ,
}
2021-11-22 19:53:08 +02:00
files , err := Find ( context . New ( config . Project { } ) , globs )
2020-10-06 14:48:04 +02:00
require . NoError ( t , err )
2020-11-10 16:20:55 +02:00
require . Len ( t , files , 2 )
require . Equal ( t , "testdata/file1.golden" , files [ "file1.golden" ] )
require . Equal ( t , "testdata/file2.golden" , files [ "file2.golden" ] )
2020-05-25 20:07:40 +02:00
}
func TestShouldGetAllFilesInsideTestdata ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : "./testdata/*" } ,
}
2021-11-22 19:53:08 +02:00
files , err := Find ( context . New ( config . Project { } ) , globs )
2020-10-06 14:48:04 +02:00
require . NoError ( t , err )
2020-11-10 16:20:55 +02:00
require . Len ( t , files , 4 )
2021-11-22 19:53:08 +02:00
require . Equal ( t , "testdata/sub3/file1.golden" , files [ "file1.golden" ] )
2020-11-10 16:20:55 +02:00
require . Equal ( t , "testdata/file2.golden" , files [ "file2.golden" ] )
require . Equal ( t , "testdata/file3.gold" , files [ "file3.gold" ] )
require . Equal ( t , "testdata/sub/file5.golden" , files [ "file5.golden" ] )
2020-05-25 20:07:40 +02:00
}
2021-11-24 03:18:47 +02:00
func TestTargetName ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{
Glob : "./testdata/file1.golden" ,
NameTemplate : "file1_{{.Tag}}.golden" ,
} ,
}
ctx := context . New ( config . Project { } )
ctx . Git . CurrentTag = "v1.0.0"
files , err := Find ( ctx , globs )
require . NoError ( t , err )
require . Len ( t , files , 1 )
require . Equal ( t , "testdata/file1.golden" , files [ "file1_v1.0.0.golden" ] )
}
func TestTargetInvalidNameTemplate ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{
Glob : "./testdata/file1.golden" ,
NameTemplate : "file1_{{.Env.HONK}}.golden" ,
} ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . EqualError ( t , err , ` failed to apply template to name "file1_ {{ .Env .HONK }} .golden": template: tmpl:1:12: executing "tmpl" at <.Env.HONK>: map has no entry for key "HONK" ` )
}
func TestTargetNameMatchesMultipleFiles ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{
Glob : "./testdata/*" ,
NameTemplate : "file1.golden" ,
} ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . EqualError ( t , err , ` failed to add extra_file: "./testdata/*" -> "file1.golden": glob matches multiple files ` )
}
func TestTargetNameNoMatches ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{
Glob : "./testdata/file1.silver" ,
NameTemplate : "file1_{{.Tag}}.golden" ,
} ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . EqualError ( t , err , ` globbing failed for pattern ./testdata/file1.silver: matching "./testdata/file1.silver": file does not exist ` )
}
func TestGlobEvalsToEmpty ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ Glob : ` {{ printf "" }} ` } ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . NoError ( t , err )
}
func TestTargetNameNoGlob ( t * testing . T ) {
globs := [ ] config . ExtraFile {
{ NameTemplate : "file1.golden" } ,
}
ctx := context . New ( config . Project { } )
files , err := Find ( ctx , globs )
require . Empty ( t , files )
require . NoError ( t , err )
}