2020-05-04 14:18:13 -05:00
package runner
import (
2021-12-08 14:57:42 -06:00
"context"
2020-11-12 10:15:09 -06:00
"fmt"
2020-11-17 11:31:05 -06:00
"os"
"regexp"
2021-05-04 16:50:35 -05:00
"runtime"
2021-03-12 18:25:10 -06:00
"sort"
2020-11-17 11:31:05 -06:00
"strings"
2020-05-04 14:18:13 -05:00
"testing"
2022-05-24 08:36:06 -05:00
"github.com/nektos/act/pkg/exprparser"
2021-01-18 13:44:27 -06:00
"github.com/nektos/act/pkg/model"
2021-05-06 15:02:29 -05:00
log "github.com/sirupsen/logrus"
2021-05-18 01:14:49 -05:00
assert "github.com/stretchr/testify/assert"
2021-08-09 10:35:05 -05:00
yaml "gopkg.in/yaml.v3"
2020-05-04 14:18:13 -05:00
)
func TestRunContext_EvalBool ( t * testing . T ) {
2021-08-09 10:35:05 -05:00
var yml yaml . Node
err := yml . Encode ( map [ string ] [ ] interface { } {
"os" : { "Linux" , "Windows" } ,
"foo" : { "bar" , "baz" } ,
} )
assert . NoError ( t , err )
2020-05-04 14:18:13 -05:00
rc := & RunContext {
Config : & Config {
Workdir : "." ,
} ,
Env : map [ string ] string {
2020-11-17 11:31:05 -06:00
"SOMETHING_TRUE" : "true" ,
"SOMETHING_FALSE" : "false" ,
"SOME_TEXT" : "text" ,
2020-05-04 14:18:13 -05:00
} ,
Run : & model . Run {
JobID : "job1" ,
Workflow : & model . Workflow {
Name : "test-workflow" ,
Jobs : map [ string ] * model . Job {
"job1" : {
Strategy : & model . Strategy {
2021-08-09 10:35:05 -05:00
RawMatrix : yml ,
2020-05-04 14:18:13 -05:00
} ,
} ,
} ,
} ,
} ,
Matrix : map [ string ] interface { } {
"os" : "Linux" ,
"foo" : "bar" ,
} ,
2021-12-22 13:52:09 -06:00
StepResults : map [ string ] * model . StepResult {
2020-05-04 14:18:13 -05:00
"id1" : {
2021-12-22 13:52:09 -06:00
Conclusion : model . StepStatusSuccess ,
Outcome : model . StepStatusFailure ,
2020-05-04 14:18:13 -05:00
Outputs : map [ string ] string {
"foo" : "bar" ,
} ,
} ,
} ,
}
2022-06-17 10:55:21 -05:00
rc . ExprEval = rc . NewExpressionEvaluator ( context . Background ( ) )
2020-05-04 14:18:13 -05:00
tables := [ ] struct {
2020-11-17 11:31:05 -06:00
in string
out bool
wantErr bool
2020-05-04 14:18:13 -05:00
} {
// The basic ones
2020-11-17 11:31:05 -06:00
{ in : "failure()" , out : false } ,
{ in : "success()" , out : true } ,
{ in : "cancelled()" , out : false } ,
{ in : "always()" , out : true } ,
2022-01-21 10:07:20 -06:00
// TODO: move to sc.NewExpressionEvaluator(), because "steps" context is not available here
// {in: "steps.id1.conclusion == 'success'", out: true},
// {in: "steps.id1.conclusion != 'success'", out: false},
// {in: "steps.id1.outcome == 'failure'", out: true},
// {in: "steps.id1.outcome != 'failure'", out: false},
2020-11-17 11:31:05 -06:00
{ in : "true" , out : true } ,
{ in : "false" , out : false } ,
2022-01-21 10:07:20 -06:00
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
// {in: "!true", wantErr: true},
// {in: "!false", wantErr: true},
2020-11-17 11:31:05 -06:00
{ in : "1 != 0" , out : true } ,
{ in : "1 != 1" , out : false } ,
{ in : "${{ 1 != 0 }}" , out : true } ,
{ in : "${{ 1 != 1 }}" , out : false } ,
{ in : "1 == 0" , out : false } ,
{ in : "1 == 1" , out : true } ,
{ in : "1 > 2" , out : false } ,
{ in : "1 < 2" , out : true } ,
2020-05-04 14:18:13 -05:00
// And or
2020-11-17 11:31:05 -06:00
{ in : "true && false" , out : false } ,
{ in : "true && 1 < 2" , out : true } ,
{ in : "false || 1 < 2" , out : true } ,
{ in : "false || false" , out : false } ,
2020-05-04 14:18:13 -05:00
// None boolable
2020-11-17 11:31:05 -06:00
{ in : "env.UNKNOWN == 'true'" , out : false } ,
{ in : "env.UNKNOWN" , out : false } ,
2020-05-04 14:18:13 -05:00
// Inline expressions
2022-01-21 10:07:20 -06:00
{ in : "env.SOME_TEXT" , out : true } ,
2020-11-17 11:31:05 -06:00
{ in : "env.SOME_TEXT == 'text'" , out : true } ,
{ in : "env.SOMETHING_TRUE == 'true'" , out : true } ,
{ in : "env.SOMETHING_FALSE == 'true'" , out : false } ,
{ in : "env.SOMETHING_TRUE" , out : true } ,
2022-01-21 10:07:20 -06:00
{ in : "env.SOMETHING_FALSE" , out : true } ,
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
// {in: "!env.SOMETHING_TRUE", wantErr: true},
// {in: "!env.SOMETHING_FALSE", wantErr: true},
2020-11-17 11:31:05 -06:00
{ in : "${{ !env.SOMETHING_TRUE }}" , out : false } ,
{ in : "${{ !env.SOMETHING_FALSE }}" , out : false } ,
{ in : "${{ ! env.SOMETHING_TRUE }}" , out : false } ,
{ in : "${{ ! env.SOMETHING_FALSE }}" , out : false } ,
{ in : "${{ env.SOMETHING_TRUE }}" , out : true } ,
{ in : "${{ env.SOMETHING_FALSE }}" , out : true } ,
{ in : "${{ !env.SOMETHING_TRUE }}" , out : false } ,
{ in : "${{ !env.SOMETHING_FALSE }}" , out : false } ,
{ in : "${{ !env.SOMETHING_TRUE && true }}" , out : false } ,
{ in : "${{ !env.SOMETHING_FALSE && true }}" , out : false } ,
{ in : "${{ !env.SOMETHING_TRUE || true }}" , out : true } ,
{ in : "${{ !env.SOMETHING_FALSE || false }}" , out : false } ,
{ in : "${{ env.SOMETHING_TRUE && true }}" , out : true } ,
{ in : "${{ env.SOMETHING_FALSE || true }}" , out : true } ,
{ in : "${{ env.SOMETHING_FALSE || false }}" , out : true } ,
2022-01-21 10:07:20 -06:00
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
// {in: "!env.SOMETHING_TRUE || true", wantErr: true},
2020-11-17 11:31:05 -06:00
{ in : "${{ env.SOMETHING_TRUE == 'true'}}" , out : true } ,
{ in : "${{ env.SOMETHING_FALSE == 'true'}}" , out : false } ,
{ in : "${{ env.SOMETHING_FALSE == 'false'}}" , out : true } ,
{ in : "${{ env.SOMETHING_FALSE }} && ${{ env.SOMETHING_TRUE }}" , out : true } ,
2020-11-12 10:15:09 -06:00
2020-05-04 14:18:13 -05:00
// All together now
2020-11-17 11:31:05 -06:00
{ in : "false || env.SOMETHING_TRUE == 'true'" , out : true } ,
{ in : "true || env.SOMETHING_FALSE == 'true'" , out : true } ,
{ in : "true && env.SOMETHING_TRUE == 'true'" , out : true } ,
{ in : "false && env.SOMETHING_TRUE == 'true'" , out : false } ,
{ in : "env.SOMETHING_FALSE == 'true' && env.SOMETHING_TRUE == 'true'" , out : false } ,
{ in : "env.SOMETHING_FALSE == 'true' && true" , out : false } ,
{ in : "${{ env.SOMETHING_FALSE == 'true' }} && true" , out : true } ,
{ in : "true && ${{ env.SOMETHING_FALSE == 'true' }}" , out : true } ,
2020-05-04 14:18:13 -05:00
// Check github context
2020-11-17 11:31:05 -06:00
{ in : "github.actor == 'nektos/act'" , out : true } ,
{ in : "github.actor == 'unknown'" , out : false } ,
2023-02-27 13:10:31 -06:00
{ in : "github.job == 'job1'" , out : true } ,
2020-11-18 09:14:34 -06:00
// The special ACT flag
{ in : "${{ env.ACT }}" , out : true } ,
{ in : "${{ !env.ACT }}" , out : false } ,
2021-01-18 13:44:27 -06:00
// Invalid expressions should be reported
{ in : "INVALID_EXPRESSION" , wantErr : true } ,
2020-05-04 14:18:13 -05:00
}
2020-11-17 11:31:05 -06:00
updateTestIfWorkflow ( t , tables , rc )
2020-05-04 14:18:13 -05:00
for _ , table := range tables {
table := table
t . Run ( table . in , func ( t * testing . T ) {
2021-05-18 01:14:49 -05:00
assertObject := assert . New ( t )
2022-06-17 10:55:21 -05:00
b , err := EvalBool ( context . Background ( ) , rc . ExprEval , table . in , exprparser . DefaultStatusCheckSuccess )
2020-11-17 11:31:05 -06:00
if table . wantErr {
2021-05-18 01:14:49 -05:00
assertObject . Error ( err )
2020-11-17 11:31:05 -06:00
}
2020-05-04 14:18:13 -05:00
2021-05-18 01:14:49 -05:00
assertObject . Equal ( table . out , b , fmt . Sprintf ( "Expected %s to be %v, was %v" , table . in , table . out , b ) )
2020-05-04 14:18:13 -05:00
} )
}
}
2020-11-17 11:31:05 -06:00
func updateTestIfWorkflow ( t * testing . T , tables [ ] struct {
in string
out bool
wantErr bool
} , rc * RunContext ) {
var envs string
2021-03-12 18:25:10 -06:00
keys := make ( [ ] string , 0 , len ( rc . Env ) )
for k := range rc . Env {
keys = append ( keys , k )
2020-11-17 11:31:05 -06:00
}
2021-03-12 18:25:10 -06:00
sort . Strings ( keys )
for _ , k := range keys {
envs += fmt . Sprintf ( " %s: %s\n" , k , rc . Env [ k ] )
}
2021-12-08 14:57:42 -06:00
// editorconfig-checker-disable
2020-11-17 11:31:05 -06:00
workflow := fmt . Sprintf ( `
2021-03-30 14:26:25 -05:00
name : "Test what expressions result in true and false on GitHub"
2020-11-17 11:31:05 -06:00
on : push
env :
% s
jobs :
test - ifs - and - buts :
runs - on : ubuntu - latest
steps :
` , envs )
2021-12-08 14:57:42 -06:00
// editorconfig-checker-enable
2020-11-17 11:31:05 -06:00
for i , table := range tables {
if table . wantErr || strings . HasPrefix ( table . in , "github.actor" ) {
continue
}
2022-01-21 10:07:20 -06:00
expressionPattern := regexp . MustCompile ( ` \$ {{ \ s * ( . + ? ) \ s * }} ` )
2020-11-17 11:31:05 -06:00
expr := expressionPattern . ReplaceAllStringFunc ( table . in , func ( match string ) string {
return fmt . Sprintf ( "€{{ %s }}" , expressionPattern . ReplaceAllString ( match , "$1" ) )
} )
echo := fmt . Sprintf ( ` run: echo "%s should be false, but was evaluated to true;" exit 1; ` , table . in )
name := fmt . Sprintf ( ` "❌ I should not run, expr: %s" ` , expr )
if table . out {
echo = ` run: echo OK `
name = fmt . Sprintf ( ` "✅ I should run, expr: %s" ` , expr )
}
2021-03-12 18:25:10 -06:00
workflow += fmt . Sprintf ( "\n - name: %s\n id: step%d\n if: %s\n %s\n" , name , i , table . in , echo )
2020-11-17 11:31:05 -06:00
if table . out {
2021-03-12 18:25:10 -06:00
workflow += fmt . Sprintf ( "\n - name: \"Double checking expr: %s\"\n if: steps.step%d.conclusion == 'skipped'\n run: echo \"%s should have been true, but wasn't\"\n" , expr , i , table . in )
2020-11-17 11:31:05 -06:00
}
}
file , err := os . Create ( "../../.github/workflows/test-if.yml" )
if err != nil {
t . Fatal ( err )
}
_ , err = file . WriteString ( workflow )
if err != nil {
t . Fatal ( err )
}
}
2021-05-04 16:50:35 -05:00
func TestRunContext_GetBindsAndMounts ( t * testing . T ) {
rctemplate := & RunContext {
Name : "TestRCName" ,
Run : & model . Run {
Workflow : & model . Workflow {
Name : "TestWorkflowName" ,
} ,
} ,
Config : & Config {
BindWorkdir : false ,
} ,
}
tests := [ ] struct {
windowsPath bool
name string
rc * RunContext
wantbind string
wantmount string
} {
{ false , "/mnt/linux" , rctemplate , "/mnt/linux" , "/mnt/linux" } ,
{ false , "/mnt/path with spaces/linux" , rctemplate , "/mnt/path with spaces/linux" , "/mnt/path with spaces/linux" } ,
{ true , "C:\\Users\\TestPath\\MyTestPath" , rctemplate , "/mnt/c/Users/TestPath/MyTestPath" , "/mnt/c/Users/TestPath/MyTestPath" } ,
{ true , "C:\\Users\\Test Path with Spaces\\MyTestPath" , rctemplate , "/mnt/c/Users/Test Path with Spaces/MyTestPath" , "/mnt/c/Users/Test Path with Spaces/MyTestPath" } ,
{ true , "/LinuxPathOnWindowsShouldFail" , rctemplate , "" , "" } ,
}
isWindows := runtime . GOOS == "windows"
for _ , testcase := range tests {
// pin for scopelint
testcase := testcase
for _ , bindWorkDir := range [ ] bool { true , false } {
// pin for scopelint
bindWorkDir := bindWorkDir
testBindSuffix := ""
if bindWorkDir {
testBindSuffix = "Bind"
}
// Only run windows path tests on windows and non-windows on non-windows
if ( isWindows && testcase . windowsPath ) || ( ! isWindows && ! testcase . windowsPath ) {
t . Run ( ( testcase . name + testBindSuffix ) , func ( t * testing . T ) {
config := testcase . rc . Config
config . Workdir = testcase . name
config . BindWorkdir = bindWorkDir
gotbind , gotmount := rctemplate . GetBindsAndMounts ( )
// Name binds/mounts are either/or
if config . BindWorkdir {
fullBind := testcase . name + ":" + testcase . wantbind
if runtime . GOOS == "darwin" {
fullBind += ":delegated"
}
2021-05-18 01:14:49 -05:00
assert . Contains ( t , gotbind , fullBind )
2021-05-04 16:50:35 -05:00
} else {
mountkey := testcase . rc . jobContainerName ( )
2021-05-18 01:14:49 -05:00
assert . EqualValues ( t , testcase . wantmount , gotmount [ mountkey ] )
2021-05-04 16:50:35 -05:00
}
} )
}
}
}
2022-04-04 13:01:13 -05:00
t . Run ( "ContainerVolumeMountTest" , func ( t * testing . T ) {
tests := [ ] struct {
name string
volumes [ ] string
wantbind string
wantmount map [ string ] string
} {
{ "BindAnonymousVolume" , [ ] string { "/volume" } , "/volume" , map [ string ] string { } } ,
{ "BindHostFile" , [ ] string { "/path/to/file/on/host:/volume" } , "/path/to/file/on/host:/volume" , map [ string ] string { } } ,
{ "MountExistingVolume" , [ ] string { "volume-id:/volume" } , "" , map [ string ] string { "volume-id" : "/volume" } } ,
}
for _ , testcase := range tests {
t . Run ( testcase . name , func ( t * testing . T ) {
job := & model . Job { }
err := job . RawContainer . Encode ( map [ string ] [ ] string {
"volumes" : testcase . volumes ,
} )
assert . NoError ( t , err )
2022-05-11 14:06:05 -05:00
rc := & RunContext {
Name : "TestRCName" ,
Run : & model . Run {
Workflow : & model . Workflow {
Name : "TestWorkflowName" ,
} ,
} ,
Config : & Config {
BindWorkdir : false ,
} ,
}
2022-04-04 13:01:13 -05:00
rc . Run . JobID = "job1"
rc . Run . Workflow . Jobs = map [ string ] * model . Job { "job1" : job }
gotbind , gotmount := rc . GetBindsAndMounts ( )
if len ( testcase . wantbind ) > 0 {
assert . Contains ( t , gotbind , testcase . wantbind )
}
for k , v := range testcase . wantmount {
assert . Contains ( t , gotmount , k )
assert . Equal ( t , gotmount [ k ] , v )
}
} )
}
} )
2021-05-04 16:50:35 -05:00
}
2021-05-06 15:02:29 -05:00
func TestGetGitHubContext ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
cwd , err := os . Getwd ( )
2021-05-18 01:14:49 -05:00
assert . Nil ( t , err )
2021-05-06 15:02:29 -05:00
rc := & RunContext {
Config : & Config {
EventName : "push" ,
Workdir : cwd ,
} ,
Run : & model . Run {
Workflow : & model . Workflow {
Name : "GitHubContextTest" ,
} ,
} ,
Name : "GitHubContextTest" ,
CurrentStep : "step" ,
Matrix : map [ string ] interface { } { } ,
Env : map [ string ] string { } ,
ExtraPath : [ ] string { } ,
2021-12-22 13:52:09 -06:00
StepResults : map [ string ] * model . StepResult { } ,
2021-05-06 15:02:29 -05:00
OutputMappings : map [ MappableOutput ] MappableOutput { } ,
}
2023-02-27 13:10:31 -06:00
rc . Run . JobID = "job1"
2021-05-06 15:02:29 -05:00
2022-06-17 10:55:21 -05:00
ghc := rc . getGithubContext ( context . Background ( ) )
2021-05-06 15:02:29 -05:00
log . Debugf ( "%v" , ghc )
2021-06-10 18:12:05 -05:00
actor := "nektos/act"
if a := os . Getenv ( "ACT_ACTOR" ) ; a != "" {
actor = a
}
repo := "nektos/act"
if r := os . Getenv ( "ACT_REPOSITORY" ) ; r != "" {
repo = r
}
owner := "nektos"
if o := os . Getenv ( "ACT_OWNER" ) ; o != "" {
owner = o
}
2021-05-06 15:02:29 -05:00
assert . Equal ( t , ghc . RunID , "1" )
assert . Equal ( t , ghc . RunNumber , "1" )
assert . Equal ( t , ghc . RetentionDays , "0" )
2021-06-10 18:12:05 -05:00
assert . Equal ( t , ghc . Actor , actor )
assert . Equal ( t , ghc . Repository , repo )
assert . Equal ( t , ghc . RepositoryOwner , owner )
2021-05-06 15:02:29 -05:00
assert . Equal ( t , ghc . RunnerPerflog , "/dev/null" )
assert . Equal ( t , ghc . Token , rc . Config . Secrets [ "GITHUB_TOKEN" ] )
2023-02-27 13:10:31 -06:00
assert . Equal ( t , ghc . Job , "job1" )
2021-05-06 15:02:29 -05:00
}
2021-12-08 14:57:42 -06:00
2022-09-26 17:49:19 -05:00
func TestGetGithubContextRef ( t * testing . T ) {
table := [ ] struct {
event string
json string
ref string
} {
{ event : "push" , json : ` { "ref":"0000000000000000000000000000000000000000"} ` , ref : "0000000000000000000000000000000000000000" } ,
{ event : "create" , json : ` { "ref":"0000000000000000000000000000000000000000"} ` , ref : "0000000000000000000000000000000000000000" } ,
{ event : "workflow_dispatch" , json : ` { "ref":"0000000000000000000000000000000000000000"} ` , ref : "0000000000000000000000000000000000000000" } ,
{ event : "delete" , json : ` { "repository": { "default_branch": "main"}} ` , ref : "refs/heads/main" } ,
{ event : "pull_request" , json : ` { "number":123} ` , ref : "refs/pull/123/merge" } ,
{ event : "pull_request_review" , json : ` { "number":123} ` , ref : "refs/pull/123/merge" } ,
{ event : "pull_request_review_comment" , json : ` { "number":123} ` , ref : "refs/pull/123/merge" } ,
{ event : "pull_request_target" , json : ` { "pull_request": { "base": { "ref": "main"}}} ` , ref : "refs/heads/main" } ,
{ event : "deployment" , json : ` { "deployment": { "ref": "tag-name"}} ` , ref : "tag-name" } ,
{ event : "deployment_status" , json : ` { "deployment": { "ref": "tag-name"}} ` , ref : "tag-name" } ,
2023-03-09 14:03:13 -06:00
{ event : "release" , json : ` { "release": { "tag_name": "tag-name"}} ` , ref : "refs/tags/tag-name" } ,
2022-09-26 17:49:19 -05:00
}
for _ , data := range table {
data := data
t . Run ( data . event , func ( t * testing . T ) {
rc := & RunContext {
EventJSON : data . json ,
Config : & Config {
EventName : data . event ,
Workdir : "" ,
} ,
Run : & model . Run {
Workflow : & model . Workflow {
Name : "GitHubContextTest" ,
} ,
} ,
}
ghc := rc . getGithubContext ( context . Background ( ) )
assert . Equal ( t , data . ref , ghc . Ref )
} )
}
}
2021-12-08 14:57:42 -06:00
func createIfTestRunContext ( jobs map [ string ] * model . Job ) * RunContext {
rc := & RunContext {
Config : & Config {
Workdir : "." ,
Platforms : map [ string ] string {
"ubuntu-latest" : "ubuntu-latest" ,
} ,
} ,
Env : map [ string ] string { } ,
Run : & model . Run {
JobID : "job1" ,
Workflow : & model . Workflow {
Name : "test-workflow" ,
Jobs : jobs ,
} ,
} ,
}
2022-06-17 10:55:21 -05:00
rc . ExprEval = rc . NewExpressionEvaluator ( context . Background ( ) )
2021-12-08 14:57:42 -06:00
return rc
}
func createJob ( t * testing . T , input string , result string ) * model . Job {
var job * model . Job
err := yaml . Unmarshal ( [ ] byte ( input ) , & job )
assert . NoError ( t , err )
job . Result = result
return job
}
2023-11-12 11:40:06 -06:00
func TestRunContextRunsOnPlatformNames ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
assertObject := assert . New ( t )
rc := createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "" ) ,
} )
assertObject . Equal ( [ ] string { "ubuntu-latest" } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: $ {{ ' ubuntu - latest ' }} ` , "" ) ,
} )
assertObject . Equal ( [ ] string { "ubuntu-latest" } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: [self-hosted, my-runner] ` , "" ) ,
} )
assertObject . Equal ( [ ] string { "self-hosted" , "my-runner" } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: [self-hosted, "$ {{ ' my - runner ' }} "] ` , "" ) ,
} )
assertObject . Equal ( [ ] string { "self-hosted" , "my-runner" } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: $ {{ fromJSON ( ' [ "ubuntu-latest" ] ' ) }} ` , "" ) ,
} )
assertObject . Equal ( [ ] string { "ubuntu-latest" } , rc . runsOnPlatformNames ( context . Background ( ) ) )
// test missing / invalid runs-on
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` name: something ` , "" ) ,
} )
assertObject . Equal ( [ ] string { } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs - on :
mapping : value ` , "" ) ,
} )
assertObject . Equal ( [ ] string { } , rc . runsOnPlatformNames ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: $ {{ invalid expression }} ` , "" ) ,
} )
assertObject . Equal ( [ ] string { } , rc . runsOnPlatformNames ( context . Background ( ) ) )
}
2021-12-08 14:57:42 -06:00
func TestRunContextIsEnabled ( t * testing . T ) {
log . SetLevel ( log . DebugLevel )
assertObject := assert . New ( t )
// success()
rc := createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs - on : ubuntu - latest
if : success ( ) ` , "" ) ,
} )
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "failure" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : success ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . False ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "success" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : success ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "failure" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
if : success ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
// failure()
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs - on : ubuntu - latest
if : failure ( ) ` , "" ) ,
} )
assertObject . False ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "failure" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : failure ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "success" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : failure ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . False ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "failure" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
if : failure ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . False ( rc . isEnabled ( context . Background ( ) ) )
// always()
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs - on : ubuntu - latest
if : always ( ) ` , "" ) ,
} )
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "failure" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : always ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "success" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
needs : [ job1 ]
if : always ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` runs-on: ubuntu-latest ` , "success" ) ,
"job2" : createJob ( t , ` runs - on : ubuntu - latest
if : always ( ) ` , "" ) ,
} )
rc . Run . JobID = "job2"
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
2023-11-12 14:01:32 -06:00
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` uses: ./.github/workflows/reusable.yml ` , "" ) ,
} )
assertObject . True ( rc . isEnabled ( context . Background ( ) ) )
rc = createIfTestRunContext ( map [ string ] * model . Job {
"job1" : createJob ( t , ` uses : . / . github / workflows / reusable . yml
if : false ` , "" ) ,
} )
assertObject . False ( rc . isEnabled ( context . Background ( ) ) )
2021-12-08 14:57:42 -06:00
}
2022-04-04 12:24:49 -05:00
func TestRunContextGetEnv ( t * testing . T ) {
tests := [ ] struct {
description string
rc * RunContext
targetEnv string
want string
} {
{
description : "Env from Config should overwrite" ,
rc : & RunContext {
Config : & Config {
Env : map [ string ] string { "OVERWRITTEN" : "true" } ,
} ,
Run : & model . Run {
Workflow : & model . Workflow {
Jobs : map [ string ] * model . Job { "test" : { Name : "test" } } ,
Env : map [ string ] string { "OVERWRITTEN" : "false" } ,
} ,
JobID : "test" ,
} ,
} ,
targetEnv : "OVERWRITTEN" ,
want : "true" ,
} ,
{
description : "No overwrite occurs" ,
rc : & RunContext {
Config : & Config {
Env : map [ string ] string { "SOME_OTHER_VAR" : "true" } ,
} ,
Run : & model . Run {
Workflow : & model . Workflow {
Jobs : map [ string ] * model . Job { "test" : { Name : "test" } } ,
Env : map [ string ] string { "OVERWRITTEN" : "false" } ,
} ,
JobID : "test" ,
} ,
} ,
targetEnv : "OVERWRITTEN" ,
want : "false" ,
} ,
}
for _ , test := range tests {
t . Run ( test . description , func ( t * testing . T ) {
envMap := test . rc . GetEnv ( )
assert . EqualValues ( t , test . want , envMap [ test . targetEnv ] )
} )
}
}