2017-04-15 20:26:05 +02:00
package archive
import (
2017-10-02 18:43:03 +02:00
"archive/tar"
2018-07-25 02:33:31 +02:00
"archive/zip"
2017-10-02 18:43:03 +02:00
"compress/gzip"
2020-03-04 05:52:43 +02:00
"fmt"
2017-10-02 18:43:03 +02:00
"io"
2018-11-11 23:02:45 +02:00
"io/ioutil"
2017-04-15 20:26:05 +02:00
"os"
"path/filepath"
"testing"
2021-07-22 03:09:02 +02:00
"time"
2017-04-15 20:26:05 +02:00
2017-12-17 22:40:19 +02:00
"github.com/goreleaser/goreleaser/internal/artifact"
2017-07-23 21:27:46 +02:00
"github.com/goreleaser/goreleaser/internal/testlib"
2021-07-22 03:09:02 +02:00
"github.com/goreleaser/goreleaser/internal/tmpl"
2018-11-11 23:02:45 +02:00
"github.com/goreleaser/goreleaser/pkg/archive"
2018-08-15 04:50:20 +02:00
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
2018-07-09 05:54:42 +02:00
"github.com/stretchr/testify/require"
2017-04-15 20:26:05 +02:00
)
func TestDescription ( t * testing . T ) {
2018-11-11 22:17:44 +02:00
require . NotEmpty ( t , Pipe { } . String ( ) )
2017-04-15 20:26:05 +02:00
}
2020-02-06 03:08:18 +02:00
func createFakeBinary ( t * testing . T , dist , arch , bin string ) {
2020-12-29 17:40:21 +02:00
t . Helper ( )
2021-04-25 19:20:49 +02:00
path := filepath . Join ( dist , arch , bin )
require . NoError ( t , os . MkdirAll ( filepath . Dir ( path ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( path )
2020-02-06 03:08:18 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2020-02-06 03:08:18 +02:00
}
2017-04-15 20:26:05 +02:00
func TestRunPipe ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
2017-12-19 01:32:44 +02:00
for _ , format := range [ ] string { "tar.gz" , "zip" } {
2021-01-07 14:15:32 +02:00
t . Run ( "Archive format " + format , func ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
dist := filepath . Join ( folder , format + "_dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
2021-10-12 19:55:43 +02:00
for _ , arch := range [ ] string { "darwinamd64" , "darwinall" , "linux386" , "linuxarm7" , "linuxmipssoftfloat" } {
2020-03-28 20:40:16 +02:00
createFakeBinary ( t , dist , arch , "bin/mybin" )
2020-02-06 03:08:18 +02:00
}
2020-03-28 20:40:16 +02:00
createFakeBinary ( t , dist , "windowsamd64" , "bin/mybin.exe" )
2020-03-04 05:52:43 +02:00
for _ , tt := range [ ] string { "darwin" , "linux" , "windows" } {
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( folder , fmt . Sprintf ( "README.%s.md" , tt ) ) )
2020-03-04 05:52:43 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2020-03-04 05:52:43 +02:00
}
2021-04-25 19:20:49 +02:00
require . NoError ( t , os . MkdirAll ( filepath . Join ( folder , "foo" , "bar" , "foobar" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( filepath . Join ( folder , "foo" , "bar" , "foobar" , "blah.txt" ) ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2017-12-19 01:32:44 +02:00
config . Project {
Dist : dist ,
ProjectName : "foobar" ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
2020-02-06 03:08:18 +02:00
ID : "myid" ,
2019-04-16 15:19:15 +02:00
Builds : [ ] string { "default" } ,
NameTemplate : defaultNameTemplate ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "README.{{.Os}}.*" } ,
{ Source : "./foo/**/*" } ,
2019-04-16 15:19:15 +02:00
} ,
FormatOverrides : [ ] config . FormatOverride {
{
Goos : "windows" ,
Format : "zip" ,
} ,
2017-12-19 01:32:44 +02:00
} ,
} ,
2017-04-21 21:01:19 +02:00
} ,
} ,
2017-12-19 01:32:44 +02:00
)
2021-10-12 19:55:43 +02:00
darwinUniversalBinary := & artifact . Artifact {
Goos : "darwin" ,
Goarch : "all" ,
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "darwinall" , "bin" , "mybin" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2021-10-12 19:55:43 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
darwinBuild := & artifact . Artifact {
2017-12-19 01:32:44 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
2020-03-28 20:40:16 +02:00
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "darwinamd64" , "bin" , "mybin" ) ,
2017-12-19 01:32:44 +02:00
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2017-12-19 01:32:44 +02:00
} ,
2019-01-01 18:40:17 +02:00
}
2021-04-25 19:20:49 +02:00
linux386Build := & artifact . Artifact {
2020-02-06 03:08:18 +02:00
Goos : "linux" ,
Goarch : "386" ,
2020-03-28 20:40:16 +02:00
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "linux386" , "bin" , "mybin" ) ,
2020-02-06 03:08:18 +02:00
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2020-02-06 03:08:18 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
linuxArmBuild := & artifact . Artifact {
2020-02-06 03:08:18 +02:00
Goos : "linux" ,
Goarch : "arm" ,
Goarm : "7" ,
2020-03-28 20:40:16 +02:00
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "linuxarm7" , "bin" , "mybin" ) ,
2020-02-06 03:08:18 +02:00
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2020-02-06 03:08:18 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
linuxMipsBuild := & artifact . Artifact {
2020-02-06 03:08:18 +02:00
Goos : "linux" ,
Goarch : "mips" ,
Gomips : "softfloat" ,
2020-03-28 20:40:16 +02:00
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "linuxmipssoftfloat" , "bin" , "mybin" ) ,
2020-02-06 03:08:18 +02:00
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2020-02-06 03:08:18 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
windowsBuild := & artifact . Artifact {
2017-12-19 01:32:44 +02:00
Goos : "windows" ,
Goarch : "amd64" ,
2020-03-28 20:40:16 +02:00
Name : "bin/mybin.exe" ,
Path : filepath . Join ( dist , "windowsamd64" , "bin" , "mybin.exe" ) ,
2017-12-19 01:32:44 +02:00
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraExt : ".exe" ,
artifact . ExtraID : "default" ,
2017-12-19 01:32:44 +02:00
} ,
2019-01-01 18:40:17 +02:00
}
ctx . Artifacts . Add ( darwinBuild )
2021-10-12 19:55:43 +02:00
ctx . Artifacts . Add ( darwinUniversalBinary )
2020-02-06 03:08:18 +02:00
ctx . Artifacts . Add ( linux386Build )
ctx . Artifacts . Add ( linuxArmBuild )
ctx . Artifacts . Add ( linuxMipsBuild )
2019-01-01 18:40:17 +02:00
ctx . Artifacts . Add ( windowsBuild )
2017-12-19 01:32:44 +02:00
ctx . Version = "0.0.1"
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-04-16 15:19:15 +02:00
ctx . Config . Archives [ 0 ] . Format = format
2021-01-07 14:15:32 +02:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2021-04-25 19:20:49 +02:00
archives := ctx . Artifacts . Filter ( artifact . ByType ( artifact . UploadableArchive ) ) . List ( )
2020-02-06 03:08:18 +02:00
for _ , arch := range archives {
2021-10-17 04:03:06 +02:00
expectBin := "bin/mybin"
if arch . Goos == "windows" {
expectBin += ".exe"
}
2021-10-17 03:46:11 +02:00
require . Equal ( t , "myid" , arch . ID ( ) , "all archives must have the archive ID set" )
2021-10-17 04:03:06 +02:00
require . Equal ( t , [ ] string { expectBin } , arch . ExtraOr ( artifact . ExtraBinaries , [ ] string { } ) . ( [ ] string ) )
2021-10-17 04:20:14 +02:00
require . Equal ( t , "" , arch . ExtraOr ( artifact . ExtraBinary , "" ) . ( string ) )
2019-04-16 15:19:15 +02:00
}
2021-10-12 19:55:43 +02:00
require . Len ( t , archives , 6 )
2020-02-06 03:08:18 +02:00
// TODO: should verify the artifact fields here too
2019-01-01 18:40:17 +02:00
2018-11-11 22:17:44 +02:00
if format == "tar.gz" {
// Check archive contents
2020-03-04 05:52:43 +02:00
for name , os := range map [ string ] string {
"foobar_0.0.1_darwin_amd64.tar.gz" : "darwin" ,
2021-10-12 19:55:43 +02:00
"foobar_0.0.1_darwin_all.tar.gz" : "darwin" ,
2020-03-04 05:52:43 +02:00
"foobar_0.0.1_linux_386.tar.gz" : "linux" ,
"foobar_0.0.1_linux_armv7.tar.gz" : "linux" ,
"foobar_0.0.1_linux_mips_softfloat.tar.gz" : "linux" ,
2020-02-06 03:08:18 +02:00
} {
require . Equal (
t ,
[ ] string {
2020-03-04 05:52:43 +02:00
fmt . Sprintf ( "README.%s.md" , os ) ,
2020-02-06 03:08:18 +02:00
"foo/bar/foobar/blah.txt" ,
2020-03-28 20:40:16 +02:00
"bin/mybin" ,
2020-02-06 03:08:18 +02:00
} ,
tarFiles ( t , filepath . Join ( dist , name ) ) ,
)
}
2018-11-11 22:17:44 +02:00
}
if format == "zip" {
require . Equal (
t ,
[ ] string {
2020-03-04 05:52:43 +02:00
"README.windows.md" ,
2018-11-11 22:17:44 +02:00
"foo/bar/foobar/blah.txt" ,
2020-03-28 20:40:16 +02:00
"bin/mybin.exe" ,
2018-11-11 22:17:44 +02:00
} ,
zipFiles ( t , filepath . Join ( dist , "foobar_0.0.1_windows_amd64.zip" ) ) ,
)
}
2017-04-15 20:26:05 +02:00
} )
}
2018-07-25 02:33:31 +02:00
}
2020-10-10 13:46:16 +02:00
func TestRunPipeDifferentBinaryCount ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
2020-10-10 13:46:16 +02:00
for _ , arch := range [ ] string { "darwinamd64" , "linuxamd64" } {
createFakeBinary ( t , dist , arch , "bin/mybin" )
}
createFakeBinary ( t , dist , "darwinamd64" , "bin/foobar" )
2021-04-25 19:20:49 +02:00
ctx := context . New ( config . Project {
2020-10-10 13:46:16 +02:00
Dist : dist ,
ProjectName : "foobar" ,
Archives : [ ] config . Archive {
{
ID : "myid" ,
Format : "tar.gz" ,
Builds : [ ] string { "default" , "foobar" } ,
NameTemplate : defaultNameTemplate ,
} ,
} ,
} )
2021-04-25 19:20:49 +02:00
darwinBuild := & artifact . Artifact {
2020-10-10 13:46:16 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "darwinamd64" , "bin" , "mybin" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2020-10-10 13:46:16 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
darwinBuild2 := & artifact . Artifact {
2020-10-10 13:46:16 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "bin/foobar" ,
Path : filepath . Join ( dist , "darwinamd64" , "bin" , "foobar" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/foobar" ,
artifact . ExtraID : "foobar" ,
2020-10-10 13:46:16 +02:00
} ,
}
2021-04-25 19:20:49 +02:00
linuxArmBuild := & artifact . Artifact {
2020-10-10 13:46:16 +02:00
Goos : "linux" ,
Goarch : "amd64" ,
Name : "bin/mybin" ,
Path : filepath . Join ( dist , "linuxamd64" , "bin" , "mybin" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "bin/mybin" ,
artifact . ExtraID : "default" ,
2020-10-10 13:46:16 +02:00
} ,
}
ctx . Artifacts . Add ( darwinBuild )
ctx . Artifacts . Add ( darwinBuild2 )
ctx . Artifacts . Add ( linuxArmBuild )
ctx . Version = "0.0.1"
ctx . Git . CurrentTag = "v0.0.1"
2020-10-13 22:57:08 +02:00
t . Run ( "check enabled" , func ( t * testing . T ) {
ctx . Config . Archives [ 0 ] . AllowDifferentBinaryCount = false
require . EqualError ( t , Pipe { } . Run ( ctx ) , "invalid archive: 0: " + ErrArchiveDifferentBinaryCount . Error ( ) )
} )
t . Run ( "check disabled" , func ( t * testing . T ) {
ctx . Config . Archives [ 0 ] . AllowDifferentBinaryCount = true
require . NoError ( t , Pipe { } . Run ( ctx ) )
} )
2020-10-10 13:46:16 +02:00
}
2020-10-11 18:00:42 +02:00
func TestRunPipeNoBinaries ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
ctx := context . New ( config . Project {
2020-10-11 18:00:42 +02:00
Dist : dist ,
ProjectName : "foobar" ,
Archives : [ ] config . Archive { { } } ,
} )
ctx . Version = "0.0.1"
ctx . Git . CurrentTag = "v0.0.1"
require . NoError ( t , Pipe { } . Run ( ctx ) )
}
2018-07-25 02:33:31 +02:00
func zipFiles ( t * testing . T , path string ) [ ] string {
2020-12-29 17:40:21 +02:00
t . Helper ( )
2018-07-25 02:33:31 +02:00
f , err := os . Open ( path )
require . NoError ( t , err )
info , err := f . Stat ( )
require . NoError ( t , err )
r , err := zip . NewReader ( f , info . Size ( ) )
require . NoError ( t , err )
2021-04-25 19:20:49 +02:00
paths := make ( [ ] string , len ( r . File ) )
2018-07-25 02:33:31 +02:00
for i , zf := range r . File {
paths [ i ] = zf . Name
}
return paths
}
func tarFiles ( t * testing . T , path string ) [ ] string {
2020-12-29 17:40:21 +02:00
t . Helper ( )
2018-07-25 02:33:31 +02:00
f , err := os . Open ( path )
require . NoError ( t , err )
defer f . Close ( )
2017-10-02 18:43:03 +02:00
gr , err := gzip . NewReader ( f )
2018-07-25 02:33:31 +02:00
require . NoError ( t , err )
defer gr . Close ( )
2021-04-25 19:20:49 +02:00
r := tar . NewReader ( gr )
2018-07-25 02:33:31 +02:00
var paths [ ] string
for {
next , err := r . Next ( )
2017-10-02 18:43:03 +02:00
if err == io . EOF {
break
}
2018-07-25 02:33:31 +02:00
require . NoError ( t , err )
paths = append ( paths , next . Name )
2017-10-02 18:43:03 +02:00
}
2018-07-25 02:33:31 +02:00
return paths
2017-04-15 20:26:05 +02:00
}
2017-04-21 21:03:36 +02:00
2017-06-08 11:41:09 +02:00
func TestRunPipeBinary ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "windowsamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( folder , "README.md" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2017-12-17 22:40:19 +02:00
config . Project {
2017-06-05 16:38:59 +02:00
Dist : dist ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Format : "binary" ,
NameTemplate : defaultBinaryNameTemplate ,
Builds : [ ] string { "default" } ,
} ,
2017-06-05 16:38:59 +02:00
} ,
} ,
2017-12-17 22:40:19 +02:00
)
2017-12-19 01:32:44 +02:00
ctx . Version = "0.0.1"
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:40:19 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( dist , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2017-12-17 22:40:19 +02:00
} ,
} )
2021-10-12 19:55:43 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
Goos : "darwin" ,
Goarch : "all" ,
Name : "myunibin" ,
Path : filepath . Join ( dist , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "myunibin" ,
artifact . ExtraID : "default" ,
2021-10-12 19:55:43 +02:00
} ,
} )
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:40:19 +02:00
Goos : "windows" ,
Goarch : "amd64" ,
Name : "mybin.exe" ,
Path : filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraExt : ".exe" ,
artifact . ExtraID : "default" ,
2017-12-17 22:40:19 +02:00
} ,
} )
2018-11-11 22:17:44 +02:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2021-04-25 19:20:49 +02:00
binaries := ctx . Artifacts . Filter ( artifact . ByType ( artifact . UploadableBinary ) )
2021-10-12 19:55:43 +02:00
require . Len ( t , binaries . List ( ) , 3 )
darwinThin := binaries . Filter ( artifact . And (
artifact . ByGoos ( "darwin" ) ,
artifact . ByGoarch ( "amd64" ) ,
) ) . List ( ) [ 0 ]
darwinUniversal := binaries . Filter ( artifact . And (
artifact . ByGoos ( "darwin" ) ,
artifact . ByGoarch ( "all" ) ,
) ) . List ( ) [ 0 ]
2017-12-19 01:32:44 +02:00
windows := binaries . Filter ( artifact . ByGoos ( "windows" ) ) . List ( ) [ 0 ]
2021-10-12 19:55:43 +02:00
require . Equal ( t , "mybin_0.0.1_darwin_amd64" , darwinThin . Name )
2021-10-17 04:20:14 +02:00
require . Equal ( t , "mybin" , darwinThin . ExtraOr ( artifact . ExtraBinary , "" ) )
2021-10-12 19:55:43 +02:00
require . Equal ( t , "myunibin_0.0.1_darwin_all" , darwinUniversal . Name )
2021-10-17 04:20:14 +02:00
require . Equal ( t , "myunibin" , darwinUniversal . ExtraOr ( artifact . ExtraBinary , "" ) )
2018-11-11 22:17:44 +02:00
require . Equal ( t , "mybin_0.0.1_windows_amd64.exe" , windows . Name )
2021-10-17 04:20:14 +02:00
require . Equal ( t , "mybin.exe" , windows . ExtraOr ( artifact . ExtraBinary , "" ) )
2017-06-05 16:38:59 +02:00
}
2017-04-22 15:29:53 +02:00
func TestRunPipeDistRemoved ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := context . New (
2017-12-17 22:40:19 +02:00
config . Project {
2020-03-28 20:40:16 +02:00
Dist : "/tmp/path/to/nope" ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
NameTemplate : "nope" ,
Format : "zip" ,
Builds : [ ] string { "default" } ,
} ,
2017-04-22 15:29:53 +02:00
} ,
} ,
2017-12-17 22:40:19 +02:00
)
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:40:19 +02:00
Goos : "windows" ,
Goarch : "amd64" ,
Name : "mybin.exe" ,
2020-03-28 20:40:16 +02:00
Path : filepath . Join ( "/tmp/path/to/nope" , "windowsamd64" , "mybin.exe" ) ,
2017-12-17 22:40:19 +02:00
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraExt : ".exe" ,
artifact . ExtraID : "default" ,
2017-05-11 05:05:51 +02:00
} ,
2017-12-17 22:40:19 +02:00
} )
2020-03-28 20:40:16 +02:00
// not checking on error msg because it may change depending on OS/version
require . Error ( t , Pipe { } . Run ( ctx ) )
2017-05-11 05:05:51 +02:00
}
2017-12-17 22:40:19 +02:00
func TestRunPipeInvalidGlob ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2017-12-17 22:40:19 +02:00
config . Project {
Dist : dist ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo" ,
Format : "zip" ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "[x-]" } ,
2019-04-16 15:19:15 +02:00
} ,
2017-05-11 05:05:51 +02:00
} ,
} ,
} ,
2017-12-17 22:40:19 +02:00
)
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:40:19 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( "dist" , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2017-12-17 22:40:19 +02:00
} ,
} )
2020-11-10 16:20:55 +02:00
require . EqualError ( t , Pipe { } . Run ( ctx ) , ` failed to find files to archive: globbing failed for pattern [x-]: compile glob pattern: unexpected end of input ` )
2017-05-11 05:05:51 +02:00
}
2018-11-11 16:12:48 +02:00
2018-11-14 13:38:29 +02:00
func TestRunPipeInvalidNameTemplate ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-14 13:38:29 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2018-11-14 13:38:29 +02:00
config . Project {
Dist : dist ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo{{ .fff }" ,
Format : "zip" ,
} ,
2018-11-14 13:38:29 +02:00
} ,
} ,
)
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-11-14 13:38:29 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( "dist" , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2018-11-14 13:38:29 +02:00
} ,
} )
require . EqualError ( t , Pipe { } . Run ( ctx ) , ` template: tmpl:1: unexpected "}" in operand ` )
}
2020-03-04 05:52:43 +02:00
func TestRunPipeInvalidFilesNameTemplate ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2020-03-04 05:52:43 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2020-03-04 05:52:43 +02:00
config . Project {
Dist : dist ,
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo" ,
Format : "zip" ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "{{.asdsd}" } ,
2020-03-04 05:52:43 +02:00
} ,
} ,
} ,
} ,
)
ctx . Git . CurrentTag = "v0.0.1"
ctx . Artifacts . Add ( & artifact . Artifact {
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( "dist" , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2020-03-04 05:52:43 +02:00
} ,
} )
require . EqualError ( t , Pipe { } . Run ( ctx ) , ` failed to find files to archive: failed to apply template {{ .asdsd } : template : tmpl : 1 : unexpected "}" in operand ` )
}
2018-11-14 13:38:29 +02:00
func TestRunPipeInvalidWrapInDirectoryTemplate ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-14 13:38:29 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2018-11-14 13:38:29 +02:00
config . Project {
Dist : dist ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo" ,
WrapInDirectory : "foo{{ .fff }" ,
Format : "zip" ,
} ,
2018-11-14 13:38:29 +02:00
} ,
} ,
)
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-11-14 13:38:29 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( "dist" , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2018-11-14 13:38:29 +02:00
} ,
} )
require . EqualError ( t , Pipe { } . Run ( ctx ) , ` template: tmpl:1: unexpected "}" in operand ` )
}
2017-10-02 18:43:03 +02:00
func TestRunPipeWrap ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( folder , "README.md" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2017-12-17 22:40:19 +02:00
config . Project {
2017-10-02 18:43:03 +02:00
Dist : dist ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo" ,
WrapInDirectory : "foo_{{ .Os }}" ,
Format : "tar.gz" ,
Replacements : map [ string ] string {
"darwin" : "macOS" ,
} ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "README.*" } ,
2019-04-16 15:19:15 +02:00
} ,
2017-10-02 18:43:03 +02:00
} ,
} ,
} ,
2017-12-17 22:40:19 +02:00
)
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2017-12-17 22:40:19 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( "dist" , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2017-12-17 22:40:19 +02:00
} ,
} )
2018-11-11 22:17:44 +02:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2017-10-02 18:43:03 +02:00
2021-04-25 19:20:49 +02:00
archives := ctx . Artifacts . Filter ( artifact . ByType ( artifact . UploadableArchive ) ) . List ( )
2020-04-21 21:11:18 +02:00
require . Len ( t , archives , 1 )
2021-10-17 03:46:11 +02:00
require . Equal ( t , "foo_macOS" , archives [ 0 ] . ExtraOr ( artifact . ExtraWrappedIn , "" ) )
2020-04-21 21:11:18 +02:00
2017-10-02 18:43:03 +02:00
// Check archive contents
2021-04-25 16:34:40 +02:00
f , err = os . Open ( filepath . Join ( dist , "foo.tar.gz" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
defer func ( ) { require . NoError ( t , f . Close ( ) ) } ( )
2017-10-02 18:43:03 +02:00
gr , err := gzip . NewReader ( f )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
defer func ( ) { require . NoError ( t , gr . Close ( ) ) } ( )
2017-10-02 18:43:03 +02:00
r := tar . NewReader ( gr )
for _ , n := range [ ] string { "README.md" , "mybin" } {
h , err := r . Next ( )
if err == io . EOF {
break
}
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2018-11-13 17:48:16 +02:00
require . Equal ( t , filepath . Join ( "foo_macOS" , n ) , h . Name )
2017-10-02 18:43:03 +02:00
}
}
2017-12-03 04:24:55 +02:00
func TestDefault ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := & context . Context {
2017-12-03 04:24:55 +02:00
Config : config . Project {
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive { } ,
2017-12-03 04:24:55 +02:00
} ,
}
2018-11-11 22:17:44 +02:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2019-04-16 15:19:15 +02:00
require . NotEmpty ( t , ctx . Config . Archives [ 0 ] . NameTemplate )
require . Equal ( t , "tar.gz" , ctx . Config . Archives [ 0 ] . Format )
require . NotEmpty ( t , ctx . Config . Archives [ 0 ] . Files )
2017-12-03 04:24:55 +02:00
}
func TestDefaultSet ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := & context . Context {
2017-12-03 04:24:55 +02:00
Config : config . Project {
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "foo" ,
Format : "zip" ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "foo" } ,
2019-04-16 15:19:15 +02:00
} ,
2017-12-03 04:24:55 +02:00
} ,
} ,
} ,
}
2018-11-11 22:17:44 +02:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2019-04-16 15:19:15 +02:00
require . Equal ( t , "foo" , ctx . Config . Archives [ 0 ] . NameTemplate )
require . Equal ( t , "zip" , ctx . Config . Archives [ 0 ] . Format )
2021-07-22 03:09:02 +02:00
require . Equal ( t , config . File { Source : "foo" } , ctx . Config . Archives [ 0 ] . Files [ 0 ] )
2017-12-03 04:24:55 +02:00
}
2017-12-19 01:15:32 +02:00
2017-12-27 01:19:58 +02:00
func TestDefaultFormatBinary ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := & context . Context {
2017-12-27 01:19:58 +02:00
Config : config . Project {
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Format : "binary" ,
} ,
2017-12-27 01:19:58 +02:00
} ,
} ,
}
2018-11-11 22:17:44 +02:00
require . NoError ( t , Pipe { } . Default ( ctx ) )
2019-04-16 15:19:15 +02:00
require . Equal ( t , defaultBinaryNameTemplate , ctx . Config . Archives [ 0 ] . NameTemplate )
2017-12-27 01:19:58 +02:00
}
2017-12-19 01:15:32 +02:00
func TestFormatFor ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := & context . Context {
2017-12-19 01:15:32 +02:00
Config : config . Project {
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
Format : "tar.gz" ,
FormatOverrides : [ ] config . FormatOverride {
{
Goos : "windows" ,
Format : "zip" ,
} ,
2017-12-19 01:15:32 +02:00
} ,
} ,
} ,
} ,
}
2019-04-16 15:19:15 +02:00
require . Equal ( t , "zip" , packageFormat ( ctx . Config . Archives [ 0 ] , "windows" ) )
require . Equal ( t , "tar.gz" , packageFormat ( ctx . Config . Archives [ 0 ] , "linux" ) )
2017-12-19 01:15:32 +02:00
}
2018-03-17 03:52:04 +02:00
func TestBinaryOverride ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "windowsamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( folder , "README.md" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2018-03-17 03:52:04 +02:00
for _ , format := range [ ] string { "tar.gz" , "zip" } {
2021-01-07 14:15:32 +02:00
t . Run ( "Archive format " + format , func ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := context . New (
2018-03-17 03:52:04 +02:00
config . Project {
Dist : dist ,
ProjectName : "foobar" ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : defaultNameTemplate ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "README.*" } ,
2019-04-16 15:19:15 +02:00
} ,
FormatOverrides : [ ] config . FormatOverride {
{
Goos : "windows" ,
Format : "binary" ,
} ,
2018-03-17 03:52:04 +02:00
} ,
} ,
} ,
} ,
)
2018-07-09 05:54:42 +02:00
ctx . Git . CurrentTag = "v0.0.1"
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-03-17 03:52:04 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( dist , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2018-03-17 03:52:04 +02:00
} ,
} )
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-03-17 03:52:04 +02:00
Goos : "windows" ,
Goarch : "amd64" ,
Name : "mybin.exe" ,
Path : filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraExt : ".exe" ,
artifact . ExtraID : "default" ,
2018-03-17 03:52:04 +02:00
} ,
} )
ctx . Version = "0.0.1"
2019-04-16 15:19:15 +02:00
ctx . Config . Archives [ 0 ] . Format = format
2018-03-17 03:52:04 +02:00
2021-01-07 14:15:32 +02:00
require . NoError ( t , Pipe { } . Run ( ctx ) )
2021-04-25 19:20:49 +02:00
archives := ctx . Artifacts . Filter ( artifact . ByType ( artifact . UploadableArchive ) )
2018-03-17 03:52:04 +02:00
darwin := archives . Filter ( artifact . ByGoos ( "darwin" ) ) . List ( ) [ 0 ]
2021-01-07 14:15:32 +02:00
require . Equal ( t , "foobar_0.0.1_darwin_amd64." + format , darwin . Name )
2021-10-17 03:46:11 +02:00
require . Equal ( t , format , darwin . Format ( ) )
require . Empty ( t , darwin . ExtraOr ( artifact . ExtraWrappedIn , "" ) )
2018-03-17 03:52:04 +02:00
archives = ctx . Artifacts . Filter ( artifact . ByType ( artifact . UploadableBinary ) )
windows := archives . Filter ( artifact . ByGoos ( "windows" ) ) . List ( ) [ 0 ]
2021-01-07 14:15:32 +02:00
require . Equal ( t , "foobar_0.0.1_windows_amd64.exe" , windows . Name )
2021-10-17 03:46:11 +02:00
require . Empty ( t , windows . ExtraOr ( artifact . ExtraWrappedIn , "" ) )
2021-10-17 04:52:01 +02:00
require . Equal ( t , "mybin.exe" , windows . ExtraOr ( artifact . ExtraBinary , "" ) )
2018-03-17 03:52:04 +02:00
} )
}
}
2018-11-11 16:11:03 +02:00
func TestRunPipeSameArchiveFilename ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := testlib . Mktmp ( t )
dist := filepath . Join ( folder , "dist" )
require . NoError ( t , os . Mkdir ( dist , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "darwinamd64" ) , 0 o755 ) )
require . NoError ( t , os . Mkdir ( filepath . Join ( dist , "windowsamd64" ) , 0 o755 ) )
2021-04-25 16:34:40 +02:00
f , err := os . Create ( filepath . Join ( dist , "darwinamd64" , "mybin" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
f , err = os . Create ( filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) )
2018-11-11 22:17:44 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , f . Close ( ) )
2021-04-25 19:20:49 +02:00
ctx := context . New (
2018-11-11 16:11:03 +02:00
config . Project {
Dist : dist ,
ProjectName : "foobar" ,
2019-04-16 15:19:15 +02:00
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
NameTemplate : "same-filename" ,
2021-07-22 03:09:02 +02:00
Files : [ ] config . File {
{ Source : "README.*" } ,
{ Source : "./foo/**/*" } ,
2019-04-16 15:19:15 +02:00
} ,
Format : "tar.gz" ,
2018-11-11 16:11:03 +02:00
} ,
} ,
} ,
)
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-11-11 16:11:03 +02:00
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "mybin" ,
Path : filepath . Join ( dist , "darwinamd64" , "mybin" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraID : "default" ,
2018-11-11 16:11:03 +02:00
} ,
} )
2019-08-12 22:44:48 +02:00
ctx . Artifacts . Add ( & artifact . Artifact {
2018-11-11 16:11:03 +02:00
Goos : "windows" ,
Goarch : "amd64" ,
Name : "mybin.exe" ,
Path : filepath . Join ( dist , "windowsamd64" , "mybin.exe" ) ,
Type : artifact . Binary ,
2019-01-01 18:40:17 +02:00
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraBinary : "mybin" ,
artifact . ExtraExt : ".exe" ,
artifact . ExtraID : "default" ,
2018-11-11 16:11:03 +02:00
} ,
} )
ctx . Version = "0.0.1"
ctx . Git . CurrentTag = "v0.0.1"
err = Pipe { } . Run ( ctx )
require . Error ( t , err )
require . Contains ( t , err . Error ( ) , "same-filename.tar.gz already exists. Check your archive name template" )
}
2018-11-11 23:02:45 +02:00
func TestDuplicateFilesInsideArchive ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
folder := t . TempDir ( )
2020-12-12 18:27:35 +02:00
f , err := ioutil . TempFile ( folder , "" )
2018-11-11 23:02:45 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
t . Cleanup ( func ( ) {
require . NoError ( t , f . Close ( ) )
} )
2018-11-11 23:02:45 +02:00
2020-12-12 18:27:35 +02:00
ff , err := ioutil . TempFile ( folder , "" )
2018-11-11 23:02:45 +02:00
require . NoError ( t , err )
2021-04-25 16:34:40 +02:00
require . NoError ( t , ff . Close ( ) )
2018-11-11 23:02:45 +02:00
a := NewEnhancedArchive ( archive . New ( f ) , "" )
2021-04-25 16:34:40 +02:00
t . Cleanup ( func ( ) {
require . NoError ( t , a . Close ( ) )
} )
2020-12-12 18:27:35 +02:00
2021-07-22 03:09:02 +02:00
require . NoError ( t , a . Add ( config . File {
Source : ff . Name ( ) ,
Destination : "foo" ,
} ) )
require . EqualError ( t , a . Add ( config . File {
Source : ff . Name ( ) ,
Destination : "foo" ,
} ) , "file foo already exists in the archive" )
2018-11-11 23:02:45 +02:00
}
2018-11-13 17:48:16 +02:00
func TestWrapInDirectory ( t * testing . T ) {
t . Run ( "false" , func ( t * testing . T ) {
require . Equal ( t , "" , wrapFolder ( config . Archive {
WrapInDirectory : "false" ,
} ) )
} )
t . Run ( "true" , func ( t * testing . T ) {
require . Equal ( t , "foo" , wrapFolder ( config . Archive {
WrapInDirectory : "true" ,
NameTemplate : "foo" ,
} ) )
} )
t . Run ( "custom" , func ( t * testing . T ) {
require . Equal ( t , "foobar" , wrapFolder ( config . Archive {
WrapInDirectory : "foobar" ,
} ) )
} )
}
2019-04-16 15:19:15 +02:00
func TestSeveralArchivesWithTheSameID ( t * testing . T ) {
2021-04-25 19:20:49 +02:00
ctx := & context . Context {
2019-04-16 15:19:15 +02:00
Config : config . Project {
Archives : [ ] config . Archive {
{
ID : "a" ,
} ,
{
ID : "a" ,
} ,
} ,
} ,
}
2019-07-27 15:55:56 +02:00
require . EqualError ( t , Pipe { } . Default ( ctx ) , "found 2 archives with the ID 'a', please fix your config" )
2019-04-16 15:19:15 +02:00
}
2021-07-22 03:09:02 +02:00
func TestFindFiles ( t * testing . T ) {
now := time . Now ( ) . Truncate ( time . Second )
tmpl := tmpl . New ( context . New ( config . Project { } ) )
t . Run ( "single file" , func ( t * testing . T ) {
result , err := findFiles ( tmpl , [ ] config . File {
{
Source : "./testdata/**/d.txt" ,
Destination : "var/foobar/d.txt" ,
} ,
} )
require . NoError ( t , err )
require . Equal ( t , [ ] config . File {
{
Source : "testdata/a/b/c/d.txt" ,
Destination : "var/foobar/d.txt/testdata/a/b/c/d.txt" ,
} ,
} , result )
} )
t . Run ( "match multiple files within tree without destination" , func ( t * testing . T ) {
result , err := findFiles ( tmpl , [ ] config . File { { Source : "./testdata/a" } } )
require . NoError ( t , err )
require . Equal ( t , [ ] config . File {
{ Source : "testdata/a/a.txt" , Destination : "testdata/a/a.txt" } ,
{ Source : "testdata/a/b/a.txt" , Destination : "testdata/a/b/a.txt" } ,
{ Source : "testdata/a/b/c/d.txt" , Destination : "testdata/a/b/c/d.txt" } ,
} , result )
} )
t . Run ( "match multiple files within tree specific destination" , func ( t * testing . T ) {
result , err := findFiles ( tmpl , [ ] config . File {
{
Source : "./testdata/a" ,
Destination : "usr/local/test" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
} )
require . NoError ( t , err )
require . Equal ( t , [ ] config . File {
{
Source : "testdata/a/a.txt" ,
Destination : "usr/local/test/testdata/a/a.txt" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
{
Source : "testdata/a/b/a.txt" ,
Destination : "usr/local/test/testdata/a/b/a.txt" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
{
Source : "testdata/a/b/c/d.txt" ,
Destination : "usr/local/test/testdata/a/b/c/d.txt" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
} , result )
} )
t . Run ( "match multiple files within tree specific destination stripping parents" , func ( t * testing . T ) {
result , err := findFiles ( tmpl , [ ] config . File {
{
Source : "./testdata/a" ,
Destination : "usr/local/test" ,
StripParent : true ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
} )
require . NoError ( t , err )
require . Equal ( t , [ ] config . File {
{
Source : "testdata/a/a.txt" ,
Destination : "usr/local/test/a.txt" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
{
Source : "testdata/a/b/c/d.txt" ,
Destination : "usr/local/test/d.txt" ,
Info : config . FileInfo {
Owner : "carlos" ,
Group : "users" ,
2021-07-24 04:54:13 +02:00
Mode : 0 o755 ,
2021-07-22 03:09:02 +02:00
MTime : now ,
} ,
} ,
} , result )
} )
}
func TestArchive_globbing ( t * testing . T ) {
assertGlob := func ( t * testing . T , files [ ] config . File , expected [ ] string ) {
t . Helper ( )
bin , err := ioutil . TempFile ( t . TempDir ( ) , "binary" )
require . NoError ( t , err )
dist := t . TempDir ( )
ctx := context . New ( config . Project {
Dist : dist ,
Archives : [ ] config . Archive {
{
Builds : [ ] string { "default" } ,
Format : "tar.gz" ,
NameTemplate : "foo" ,
Files : files ,
} ,
} ,
} )
ctx . Artifacts . Add ( & artifact . Artifact {
Goos : "darwin" ,
Goarch : "amd64" ,
Name : "foobin" ,
Path : bin . Name ( ) ,
Type : artifact . Binary ,
Extra : map [ string ] interface { } {
2021-10-17 03:46:11 +02:00
artifact . ExtraID : "default" ,
2021-07-22 03:09:02 +02:00
} ,
} )
require . NoError ( t , Pipe { } . Run ( ctx ) )
require . Equal ( t , append ( expected , "foobin" ) , tarFiles ( t , filepath . Join ( dist , "foo.tar.gz" ) ) )
}
t . Run ( "exact src file" , func ( t * testing . T ) {
assertGlob ( t , [ ] config . File { { Source : "testdata/a/a.txt" } } , [ ] string { "testdata/a/a.txt" } )
} )
t . Run ( "exact src file with dst" , func ( t * testing . T ) {
assertGlob ( t , [ ] config . File {
{
Source : "testdata/a/a.txt" ,
Destination : "foo/" ,
} ,
} , [ ] string { "foo/testdata/a/a.txt" } )
} )
t . Run ( "glob src" , func ( t * testing . T ) {
assertGlob ( t , [ ] config . File {
{ Source : "testdata/**/*.txt" } ,
} , [ ] string {
"testdata/a/a.txt" ,
"testdata/a/b/a.txt" ,
"testdata/a/b/c/d.txt" ,
} )
} )
t . Run ( "glob src with dst" , func ( t * testing . T ) {
assertGlob ( t , [ ] config . File {
{
Source : "testdata/**/*.txt" ,
Destination : "var/yada" ,
} ,
} , [ ] string {
"var/yada/testdata/a/a.txt" ,
"var/yada/testdata/a/b/a.txt" ,
"var/yada/testdata/a/b/c/d.txt" ,
} )
} )
t . Run ( "glob src with dst stripping parent" , func ( t * testing . T ) {
assertGlob ( t , [ ] config . File {
{
Source : "testdata/**/*.txt" ,
Destination : "var/yada" ,
StripParent : true ,
} ,
} , [ ] string {
"var/yada/a.txt" ,
"var/yada/d.txt" ,
} )
} )
}