Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add more unit tests to main_tests.go #107

Merged
merged 2 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,57 @@ func Test_runVerify(t *testing.T) {
"CGO_ENABLED=0",
},
},
{
name: "valid main",
subject: "binary-linux-amd64",
config: "./testdata/valid-main.yml",
evalEnvs: "VERSION_LDFLAGS:bla, ELSE:else",
commands: []string{
"-trimpath",
"-tags=netgo",
"-ldflags=bla something-else",
"-o",
"binary-linux-amd64",
"./path/to/main.go",
},
envs: []string{
"GOOS=linux",
"GOARCH=amd64",
"GO111MODULE=on",
"CGO_ENABLED=0",
},
},
// Below are the same tests we do in pkg/config_test.go
{
name: "invalid main",
config: "./pkg/testdata/releaser-invalid-main.yml",
err: pkg.ErrorInvalidDirectory,
},
{
name: "missing version",
config: "./pkg/testdata/releaser-noversion.yml",
err: pkg.ErrorUnsupportedVersion,
},
{
name: "invalid version",
config: "./pkg/testdata/releaser-invalid-version.yml",
err: pkg.ErrorUnsupportedVersion,
},
{
name: "invalid envs",
config: "./pkg/testdata/releaser-invalid-envs.yml",
err: pkg.ErrorInvalidEnvironmentVariable,
},
{
name: "invalid main",
config: "./pkg/testdata/releaser-invalid-main.yml",
err: pkg.ErrorInvalidDirectory,
},
{
name: "invalid path",
config: "../pkg/testdata/releaser-invalid-main.yml",
err: pkg.ErrorInvalidDirectory,
},
}

for _, tt := range tests {
Expand All @@ -194,6 +245,10 @@ func Test_runVerify(t *testing.T) {
t.Errorf(cmp.Diff(err, tt.err, cmpopts.EquateErrors()))
}

if err != nil {
return
}

cmd, env, subject, err := extract(s)
if err != nil {
t.Errorf("extract: %v", err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
)

var (
errorInvalidEnvironmentVariable = errors.New("invalid environment variable")
errorUnsupportedVersion = errors.New("version not supported")
ErrorInvalidEnvironmentVariable = errors.New("invalid environment variable")
ErrorUnsupportedVersion = errors.New("version not supported")
)

var supportedVersions = map[int]bool{
Expand Down Expand Up @@ -116,7 +116,7 @@ func validateMain(cf *goReleaserConfigFile) error {
func validateVersion(cf *goReleaserConfigFile) error {
_, exists := supportedVersions[cf.Version]
if !exists {
return fmt.Errorf("%w:%d", errorUnsupportedVersion, cf.Version)
return fmt.Errorf("%w:%d", ErrorUnsupportedVersion, cf.Version)
}

return nil
Expand All @@ -127,7 +127,7 @@ func (r *GoReleaserConfig) setEnvs(cf *goReleaserConfigFile) error {
for _, e := range cf.Env {
es := strings.Split(e, "=")
if len(es) != 2 {
return fmt.Errorf("%w: %s", errorInvalidEnvironmentVariable, e)
return fmt.Errorf("%w: %s", ErrorInvalidEnvironmentVariable, e)
}
m[es[0]] = es[1]
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ func Test_ConfigFromFile(t *testing.T) {
{
name: "missing version",
path: "./testdata/releaser-noversion.yml",
expected: errorUnsupportedVersion,
expected: ErrorUnsupportedVersion,
},
{
name: "invalid version",
path: "./testdata/releaser-invalid-version.yml",
expected: errorUnsupportedVersion,
expected: ErrorUnsupportedVersion,
},
{
name: "invalid envs",
path: "./testdata/releaser-invalid-envs.yml",
expected: errorInvalidEnvironmentVariable,
expected: ErrorInvalidEnvironmentVariable,
},
{
name: "invalid main",
path: "./testdata/releaser-invalid-main.yml",
expected: errorInvalidDirectory,
expected: ErrorInvalidDirectory,
},
{
name: "invalid path",
path: "../testdata/releaser-invalid-main.yml",
expected: errorInvalidDirectory,
expected: ErrorInvalidDirectory,
},
}
for _, tt := range tests {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"strings"
)

var errorInvalidDirectory = errors.New("invalid directory")
var ErrorInvalidDirectory = errors.New("invalid directory")

func fileIsUnderDirectory(path string) error {
wd, err := os.Getwd()
Expand All @@ -37,7 +37,7 @@ func fileIsUnderDirectory(path string) error {
}

if !strings.HasPrefix(p, wd+"/") {
return errorInvalidDirectory
return ErrorInvalidDirectory
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func Test_fileIsUnderDirectory(t *testing.T) {
{
name: "parent invalid path",
path: "../invalid/path",
expected: errorInvalidDirectory,
expected: ErrorInvalidDirectory,
},
{
name: "some invalid fullpath",
path: "/some/invalid/fullpath",
expected: errorInvalidDirectory,
expected: ErrorInvalidDirectory,
},
}
for _, tt := range tests {
Expand Down
17 changes: 17 additions & 0 deletions testdata/valid-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 1
env:
- GO111MODULE=on
# https://stackoverflow.com/a/62821358/19407
- CGO_ENABLED=0

flags:
- -trimpath
- -tags=netgo

main: ./path/to/main.go
goos: linux
goarch: amd64
binary: binary-{{ .Os }}-{{ .Arch }}
ldflags:
- '{{ .Env.VERSION_LDFLAGS }}'
- 'something-{{ .Env.ELSE }}'