github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/nfpm/nfpm_test.go (about) 1 package nfpm 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "runtime" 8 "testing" 9 10 "github.com/goreleaser/goreleaser/internal/artifact" 11 "github.com/goreleaser/goreleaser/internal/testlib" 12 "github.com/goreleaser/goreleaser/pkg/config" 13 "github.com/goreleaser/goreleaser/pkg/context" 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestDescription(t *testing.T) { 18 assert.NotEmpty(t, Pipe{}.String()) 19 } 20 21 func TestRunPipeNoFormats(t *testing.T) { 22 var ctx = &context.Context{ 23 Git: context.GitInfo{ 24 CurrentTag: "v1.0.0", 25 }, 26 Config: config.Project{}, 27 Parallelism: runtime.NumCPU(), 28 } 29 testlib.AssertSkipped(t, Pipe{}.Run(ctx)) 30 } 31 32 func TestRunPipeInvalidFormat(t *testing.T) { 33 var ctx = context.New(config.Project{ 34 ProjectName: "nope", 35 NFPM: config.NFPM{ 36 Bindir: "/usr/bin", 37 Formats: []string{"nope"}, 38 NFPMOverridables: config.NFPMOverridables{ 39 NameTemplate: defaultNameTemplate, 40 Files: map[string]string{}, 41 }, 42 }, 43 }) 44 ctx.Git = context.GitInfo{ 45 CurrentTag: "v1.2.3", 46 } 47 for _, goos := range []string{"linux", "darwin"} { 48 for _, goarch := range []string{"amd64", "386"} { 49 ctx.Artifacts.Add(artifact.Artifact{ 50 Name: "mybin", 51 Path: "whatever", 52 Goarch: goarch, 53 Goos: goos, 54 Type: artifact.Binary, 55 }) 56 } 57 } 58 assert.Contains(t, Pipe{}.Run(ctx).Error(), `no packager registered for the format nope`) 59 } 60 61 func TestRunPipe(t *testing.T) { 62 folder, err := ioutil.TempDir("", "archivetest") 63 assert.NoError(t, err) 64 var dist = filepath.Join(folder, "dist") 65 assert.NoError(t, os.Mkdir(dist, 0755)) 66 assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755)) 67 var binPath = filepath.Join(dist, "mybin", "mybin") 68 _, err = os.Create(binPath) 69 assert.NoError(t, err) 70 var ctx = context.New(config.Project{ 71 ProjectName: "mybin", 72 Dist: dist, 73 NFPM: config.NFPM{ 74 Bindir: "/usr/bin", 75 Formats: []string{"deb", "rpm"}, 76 Description: "Some description", 77 License: "MIT", 78 Maintainer: "me@me", 79 Vendor: "asdf", 80 Homepage: "https://goreleaser.github.io", 81 NFPMOverridables: config.NFPMOverridables{ 82 NameTemplate: defaultNameTemplate, 83 Dependencies: []string{"make"}, 84 Recommends: []string{"svn"}, 85 Suggests: []string{"bzr"}, 86 Conflicts: []string{"git"}, 87 EmptyFolders: []string{"/var/log/foobar"}, 88 Files: map[string]string{ 89 "./testdata/testfile.txt": "/usr/share/testfile.txt", 90 }, 91 ConfigFiles: map[string]string{ 92 "./testdata/testfile.txt": "/etc/nope.conf", 93 }, 94 Replacements: map[string]string{ 95 "linux": "Tux", 96 }, 97 }, 98 Overrides: map[string]config.NFPMOverridables{ 99 "rpm": { 100 ConfigFiles: map[string]string{ 101 "./testdata/testfile.txt": "/etc/nope-rpm.conf", 102 }, 103 }, 104 }, 105 }, 106 }) 107 ctx.Version = "1.0.0" 108 ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"} 109 for _, goos := range []string{"linux", "darwin"} { 110 for _, goarch := range []string{"amd64", "386"} { 111 ctx.Artifacts.Add(artifact.Artifact{ 112 Name: "mybin", 113 Path: binPath, 114 Goarch: goarch, 115 Goos: goos, 116 Type: artifact.Binary, 117 }) 118 } 119 } 120 assert.NoError(t, Pipe{}.Run(ctx)) 121 var packages = ctx.Artifacts.Filter(artifact.ByType(artifact.LinuxPackage)).List() 122 assert.Len(t, packages, 4) 123 for _, pkg := range packages { 124 assert.Contains(t, pkg.Name, "mybin_1.0.0_Tux_", "linux should have been replaced by Tux") 125 } 126 assert.Len(t, ctx.Config.NFPM.Files, 1, "should not modify the config file list") 127 } 128 129 func TestInvalidNameTemplate(t *testing.T) { 130 var ctx = &context.Context{ 131 Parallelism: runtime.NumCPU(), 132 Artifacts: artifact.New(), 133 Config: config.Project{ 134 NFPM: config.NFPM{ 135 NFPMOverridables: config.NFPMOverridables{NameTemplate: "{{.Foo}"}, 136 Formats: []string{"deb"}, 137 }, 138 }, 139 } 140 ctx.Artifacts.Add(artifact.Artifact{ 141 Name: "mybin", 142 Goos: "linux", 143 Goarch: "amd64", 144 Type: artifact.Binary, 145 }) 146 assert.Contains(t, Pipe{}.Run(ctx).Error(), `template: tmpl:1: unexpected "}" in operand`) 147 } 148 149 func TestCreateFileDoesntExist(t *testing.T) { 150 folder, err := ioutil.TempDir("", "archivetest") 151 assert.NoError(t, err) 152 var dist = filepath.Join(folder, "dist") 153 assert.NoError(t, os.Mkdir(dist, 0755)) 154 assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755)) 155 var ctx = context.New(config.Project{ 156 Dist: dist, 157 ProjectName: "asd", 158 NFPM: config.NFPM{ 159 Formats: []string{"deb", "rpm"}, 160 NFPMOverridables: config.NFPMOverridables{ 161 Files: map[string]string{ 162 "testdata/testfile.txt": "/var/lib/test/testfile.txt", 163 }, 164 }, 165 }, 166 }) 167 ctx.Git = context.GitInfo{ 168 CurrentTag: "v1.2.3", 169 } 170 ctx.Artifacts.Add(artifact.Artifact{ 171 Name: "mybin", 172 Path: filepath.Join(dist, "mybin", "mybin"), 173 Goos: "linux", 174 Goarch: "amd64", 175 Type: artifact.Binary, 176 }) 177 assert.Contains(t, Pipe{}.Run(ctx).Error(), `dist/mybin/mybin: file does not exist`) 178 } 179 180 func TestInvalidConfig(t *testing.T) { 181 folder, err := ioutil.TempDir("", "archivetest") 182 assert.NoError(t, err) 183 var dist = filepath.Join(folder, "dist") 184 assert.NoError(t, os.Mkdir(dist, 0755)) 185 assert.NoError(t, os.Mkdir(filepath.Join(dist, "mybin"), 0755)) 186 var ctx = context.New(config.Project{ 187 Dist: dist, 188 NFPM: config.NFPM{ 189 Formats: []string{"deb"}, 190 }, 191 }) 192 ctx.Git.CurrentTag = "v1.2.3" 193 ctx.Version = "v1.2.3" 194 ctx.Artifacts.Add(artifact.Artifact{ 195 Name: "mybin", 196 Path: filepath.Join(dist, "mybin", "mybin"), 197 Goos: "linux", 198 Goarch: "amd64", 199 Type: artifact.Binary, 200 }) 201 assert.Contains(t, Pipe{}.Run(ctx).Error(), `invalid nfpm config: package name cannot be empty`) 202 } 203 204 func TestDefault(t *testing.T) { 205 var ctx = &context.Context{ 206 Config: config.Project{ 207 NFPM: config.NFPM{}, 208 }, 209 } 210 assert.NoError(t, Pipe{}.Default(ctx)) 211 assert.Equal(t, "/usr/local/bin", ctx.Config.NFPM.Bindir) 212 assert.Equal(t, defaultNameTemplate, ctx.Config.NFPM.NameTemplate) 213 } 214 215 func TestDefaultSet(t *testing.T) { 216 var ctx = &context.Context{ 217 Config: config.Project{ 218 NFPM: config.NFPM{ 219 Bindir: "/bin", 220 NFPMOverridables: config.NFPMOverridables{ 221 NameTemplate: "foo", 222 }, 223 }, 224 }, 225 } 226 assert.NoError(t, Pipe{}.Default(ctx)) 227 assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir) 228 assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate) 229 } 230 231 func TestOverrides(t *testing.T) { 232 var ctx = &context.Context{ 233 Config: config.Project{ 234 NFPM: config.NFPM{ 235 Bindir: "/bin", 236 NFPMOverridables: config.NFPMOverridables{ 237 NameTemplate: "foo", 238 }, 239 Overrides: map[string]config.NFPMOverridables{ 240 "deb": { 241 NameTemplate: "bar", 242 }, 243 }, 244 }, 245 }, 246 } 247 assert.NoError(t, Pipe{}.Default(ctx)) 248 merged, err := mergeOverrides(ctx, "deb") 249 assert.NoError(t, err) 250 assert.Equal(t, "/bin", ctx.Config.NFPM.Bindir) 251 assert.Equal(t, "foo", ctx.Config.NFPM.NameTemplate) 252 assert.Equal(t, "bar", ctx.Config.NFPM.Overrides["deb"].NameTemplate) 253 assert.Equal(t, "bar", merged.NameTemplate) 254 }