-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f04405b
commit 04ea24c
Showing
3 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package travisci | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
//Provider implements provider interface | ||
type Provider struct{} | ||
|
||
//GetTag returns tag | ||
func (p *Provider) GetTag() (string, error) { | ||
ref := os.Getenv("TRAVIS_TAG") | ||
if ref == "" { | ||
return "", fmt.Errorf("TRAVIS_TAG env variable not found") | ||
} | ||
|
||
return ref, nil | ||
} | ||
|
||
//GetOwnerAndRepo gets the owner and repo from the env | ||
func (p *Provider) GetOwnerAndRepo() (string, string, error) { | ||
repoFromEnv := os.Getenv("TRAVIS_REPO_SLUG") | ||
if repoFromEnv == "" { | ||
return "", "", fmt.Errorf("env TRAVIS_REPO_SLUG not set") | ||
} | ||
|
||
s := strings.Split(repoFromEnv, "/") | ||
if len(s) != 2 { | ||
return "", "", fmt.Errorf("env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found %q", repoFromEnv) | ||
} | ||
|
||
return s[0], s[1], nil | ||
} | ||
|
||
//GetActor gets the owner and repo from the env | ||
func (p *Provider) GetActor() (string, error) { | ||
owner, _, err := p.GetOwnerAndRepo() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if owner == "" { | ||
return "", fmt.Errorf("failed to find actor for the release") | ||
} | ||
|
||
return owner, nil | ||
} | ||
|
||
//getInputForAction gets input to action | ||
func getInputForAction(key string) string { | ||
return os.Getenv(fmt.Sprintf("INPUT_%s", strings.ToUpper(key))) | ||
} | ||
|
||
//GetWorkDirectory gets workdir | ||
func (p *Provider) GetWorkDirectory() string { | ||
workdirInput := getInputForAction("workdir") | ||
if workdirInput != "" { | ||
return workdirInput | ||
} | ||
|
||
return os.Getenv("TRAVIS_BUILD_DIR") | ||
} | ||
|
||
//GetTemplateFile returns the template file | ||
func (p *Provider) GetTemplateFile() string { | ||
templateFile := getInputForAction("krew_template_file") | ||
if templateFile != "" { | ||
return filepath.Join(p.GetWorkDirectory(), templateFile) | ||
} | ||
|
||
return filepath.Join(p.GetWorkDirectory(), ".krew.yaml") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package travisci | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetOwnerAndRepo(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
setup func() | ||
expectedOwner string | ||
expectedRepo string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "TRAVIS_REPO_SLUG is set as expected", | ||
setup: func() { | ||
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-repo") | ||
}, | ||
expectedOwner: "foo-bar", | ||
expectedRepo: "my-awesome-repo", | ||
}, | ||
{ | ||
name: "TRAVIS_REPO_SLUG is set in incorrect format", | ||
setup: func() { | ||
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar:my-awesome-repo") | ||
}, | ||
expectedError: `env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found "foo-bar:my-awesome-repo"`, | ||
}, | ||
{ | ||
name: "TRAVIS_REPO_SLUG is set in incorrect format 2", | ||
setup: func() { | ||
os.Setenv("TRAVIS_REPO_SLUG", "my-awesome-repo") | ||
}, | ||
expectedError: `env TRAVIS_REPO_SLUG is incorrect format. expected format <owner>/<repo>, found "my-awesome-repo"`, | ||
}, | ||
{ | ||
name: "TRAVIS_REPO_SLUG environment is not set", | ||
expectedError: `env TRAVIS_REPO_SLUG not set`, | ||
}, | ||
} | ||
|
||
p := &Provider{} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Clearenv() | ||
|
||
if tc.setup != nil { | ||
tc.setup() | ||
} | ||
|
||
owner, repo, err := p.GetOwnerAndRepo() | ||
|
||
assert.Equal(t, tc.expectedOwner, owner) | ||
assert.Equal(t, tc.expectedRepo, repo) | ||
assertError(t, tc.expectedError, err) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetActionActor(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
setup func() | ||
expectedActor string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "env TRAVIS_REPO_SLUG is set as expected", | ||
setup: func() { | ||
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-plugin") | ||
}, | ||
expectedActor: "foo-bar", | ||
}, | ||
{ | ||
name: "env TRAVIS_REPO_SLUG is not set", | ||
expectedError: "env TRAVIS_REPO_SLUG not set", | ||
}, | ||
} | ||
|
||
p := &Provider{} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Clearenv() | ||
|
||
if tc.setup != nil { | ||
tc.setup() | ||
} | ||
|
||
actor, err := p.GetActor() | ||
assert.Equal(t, tc.expectedActor, actor) | ||
assertError(t, tc.expectedError, err) | ||
}) | ||
} | ||
} | ||
func TestGetTag(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
setup func() | ||
expectedTag string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "env TRAVIS_TAG is setup", | ||
setup: func() { | ||
os.Setenv("TRAVIS_TAG", "v5.0.0") | ||
}, | ||
expectedTag: "v5.0.0", | ||
}, | ||
{ | ||
name: "TRAVIS_TAG is not set", | ||
setup: func() { | ||
os.Unsetenv("TRAVIS_TAG") | ||
}, | ||
expectedError: `TRAVIS_TAG env variable not found`, | ||
}, | ||
} | ||
|
||
p := &Provider{} | ||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Clearenv() | ||
|
||
if tc.setup != nil { | ||
tc.setup() | ||
} | ||
|
||
tag, err := p.GetTag() | ||
assert.Equal(t, tc.expectedTag, tag) | ||
assertError(t, tc.expectedError, err) | ||
}) | ||
} | ||
} | ||
|
||
func assertError(t *testing.T, expectedError string, err error) { | ||
if expectedError == "" { | ||
assert.Nil(t, err) | ||
} | ||
|
||
if expectedError != "" { | ||
assert.NotNil(t, err) | ||
if err != nil { | ||
assert.Equal(t, expectedError, err.Error()) | ||
} | ||
} | ||
} | ||
|
||
func setupEnvironment() { | ||
os.Setenv("TRAVIS_REPO_SLUG", "foo-bar/my-awesome-plugin") | ||
os.Setenv("TRAVIS_TAG", "v0.0.2") | ||
os.Setenv("TRAVIS_BUILD_DIR", "./data/") | ||
} |