1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-08-11 17:50:58 +00:00

fix tests

Signed-off-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
Casey Lee 2020-02-13 11:47:38 -08:00
parent e67e06809c
commit 7183c74cd0
6 changed files with 159 additions and 29 deletions

View file

@ -75,7 +75,17 @@ func (rc *RunContext) newVM() *otto.Otto {
vmFormat,
vmJoin,
vmToJSON,
vmHashFiles(rc.Config.Workdir),
vmAlways,
vmCancelled,
rc.vmHashFiles(),
rc.vmSuccess(),
rc.vmFailure(),
rc.vmGithub(),
rc.vmEnv(),
rc.vmJob(),
rc.vmSteps(),
rc.vmRunner(),
}
vm := otto.New()
for _, configer := range configers {
@ -85,12 +95,12 @@ func (rc *RunContext) newVM() *otto.Otto {
}
func vmContains(vm *otto.Otto) {
vm.Set("contains", func(searchString interface{}, searchValue string) bool {
_ = vm.Set("contains", func(searchString interface{}, searchValue string) bool {
if searchStringString, ok := searchString.(string); ok {
return strings.Contains(strings.ToLower(searchStringString), strings.ToLower(searchValue))
} else if searchStringArray, ok := searchString.([]string); ok {
for _, s := range searchStringArray {
if strings.ToLower(s) == strings.ToLower(searchValue) {
if strings.EqualFold(s, searchValue) {
return true
}
}
@ -100,19 +110,19 @@ func vmContains(vm *otto.Otto) {
}
func vmStartsWith(vm *otto.Otto) {
vm.Set("startsWith", func(searchString string, searchValue string) bool {
_ = vm.Set("startsWith", func(searchString string, searchValue string) bool {
return strings.HasPrefix(strings.ToLower(searchString), strings.ToLower(searchValue))
})
}
func vmEndsWith(vm *otto.Otto) {
vm.Set("endsWith", func(searchString string, searchValue string) bool {
_ = vm.Set("endsWith", func(searchString string, searchValue string) bool {
return strings.HasSuffix(strings.ToLower(searchString), strings.ToLower(searchValue))
})
}
func vmFormat(vm *otto.Otto) {
vm.Set("format", func(s string, vals ...string) string {
_ = vm.Set("format", func(s string, vals ...string) string {
for i, v := range vals {
s = strings.ReplaceAll(s, fmt.Sprintf("{%d}", i), v)
}
@ -121,7 +131,7 @@ func vmFormat(vm *otto.Otto) {
}
func vmJoin(vm *otto.Otto) {
vm.Set("join", func(element interface{}, optionalElem string) string {
_ = vm.Set("join", func(element interface{}, optionalElem string) string {
slist := make([]string, 0)
if elementString, ok := element.(string); ok {
slist = append(slist, elementString)
@ -136,7 +146,7 @@ func vmJoin(vm *otto.Otto) {
}
func vmToJSON(vm *otto.Otto) {
vm.Set("toJSON", func(o interface{}) string {
_ = vm.Set("toJSON", func(o interface{}) string {
rtn, err := json.MarshalIndent(o, "", " ")
if err != nil {
logrus.Errorf("Unable to marsal: %v", err)
@ -146,10 +156,10 @@ func vmToJSON(vm *otto.Otto) {
})
}
func vmHashFiles(workdir string) func(*otto.Otto) {
func (rc *RunContext) vmHashFiles() func(*otto.Otto) {
return func(vm *otto.Otto) {
vm.Set("hashFiles", func(path string) string {
files, _, err := glob.Glob([]string{filepath.Join(workdir, path)})
_ = vm.Set("hashFiles", func(path string) string {
files, _, err := glob.Glob([]string{filepath.Join(rc.Config.Workdir, path)})
if err != nil {
logrus.Error(err)
return ""
@ -169,3 +179,99 @@ func vmHashFiles(workdir string) func(*otto.Otto) {
})
}
}
func (rc *RunContext) vmSuccess() func(*otto.Otto) {
return func(vm *otto.Otto) {
_ = vm.Set("success", func() bool {
return !rc.PriorStepFailed
})
}
}
func (rc *RunContext) vmFailure() func(*otto.Otto) {
return func(vm *otto.Otto) {
_ = vm.Set("failure", func() bool {
return rc.PriorStepFailed
})
}
}
func vmAlways(vm *otto.Otto) {
_ = vm.Set("always", func() bool {
return true
})
}
func vmCancelled(vm *otto.Otto) {
_ = vm.Set("cancelled", func() bool {
return false
})
}
func (rc *RunContext) vmGithub() func(*otto.Otto) {
github := map[string]interface{}{
"event": make(map[string]interface{}),
"event_path": "/github/workflow/event.json",
"workflow": rc.Run.Workflow.Name,
"run_id": "1",
"run_number": "1",
"actor": "nektos/act",
// TODO
"repository": "",
"event_name": "",
"sha": "",
"ref": "",
"head_ref": "",
"base_ref": "",
"token": "",
"workspace": rc.Config.Workdir,
"action": "",
}
err := json.Unmarshal([]byte(rc.EventJSON), github["event"])
if err != nil {
logrus.Error(err)
}
return func(vm *otto.Otto) {
_ = vm.Set("github", github)
}
}
func (rc *RunContext) vmEnv() func(*otto.Otto) {
env := map[string]interface{}{}
// TODO
return func(vm *otto.Otto) {
_ = vm.Set("env", env)
}
}
func (rc *RunContext) vmJob() func(*otto.Otto) {
job := map[string]interface{}{}
// TODO
return func(vm *otto.Otto) {
_ = vm.Set("job", job)
}
}
func (rc *RunContext) vmSteps() func(*otto.Otto) {
steps := map[string]interface{}{}
// TODO
return func(vm *otto.Otto) {
_ = vm.Set("steps", steps)
}
}
func (rc *RunContext) vmRunner() func(*otto.Otto) {
runner := map[string]interface{}{
"os": "Linux",
"temp": "/tmp",
"tool_cache": "/tmp",
}
return func(vm *otto.Otto) {
_ = vm.Set("runner", runner)
}
}