Update test workflows and improve expression_test.go/run_context_test.go (#560)

* fix: give case insensitive secret more meanigful name

* refactor: use `string` in generating `env:` and `steps:` for workflows

Smaller text generation is much better to read with normal strings than
raw string literals.

* feat: sort keys for `env:` so it's always in specific order

* fix: update test workflows
This commit is contained in:
hackercat 2021-03-13 01:25:10 +01:00 committed by GitHub
parent eb2774275f
commit 09679f0156
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 475 additions and 469 deletions

View file

@ -3,12 +3,12 @@ name: "Test how expressions are handled on Github"
on: push on: push
env: env:
KEYWITHNOTHING: valuewithnothing
KEY-WITH-HYPHENS: value-with-hyphens
KEY_WITH_UNDERSCORES: value_with_underscores
SOMETHING_TRUE: true
SOMETHING_FALSE: false
ACT: true ACT: true
KEY-WITH-HYPHENS: value-with-hyphens
KEYWITHNOTHING: valuewithnothing
KEY_WITH_UNDERSCORES: value_with_underscores
SOMETHING_FALSE: false
SOMETHING_TRUE: true
jobs: jobs:
@ -28,6 +28,12 @@ jobs:
- name: €{{ env.KEY_WITH_UNDERSCORES }} -> ${{ env.KEY_WITH_UNDERSCORES }} should be equal to value_with_underscores - name: €{{ env.KEY_WITH_UNDERSCORES }} -> ${{ env.KEY_WITH_UNDERSCORES }} should be equal to value_with_underscores
run: echo "Done " run: echo "Done "
- name: €{{ secrets.CASE_INSENSITIVE_SECRET }} -> ${{ secrets.CASE_INSENSITIVE_SECRET }} should be equal to value
run: echo "Done "
- name: €{{ secrets.case_insensitive_secret }} -> ${{ secrets.case_insensitive_secret }} should be equal to value
run: echo "Done "
- name: €{{ env.UNKNOWN }} -> ${{ env.UNKNOWN }} should be equal to - name: €{{ env.UNKNOWN }} -> ${{ env.UNKNOWN }} should be equal to
run: echo "Done " run: echo "Done "

View file

@ -3,10 +3,10 @@ name: "Test what expressions result in true and false on Github"
on: push on: push
env: env:
SOMETHING_TRUE: true
SOMETHING_FALSE: false
SOME_TEXT: text
ACT: true ACT: true
SOMETHING_FALSE: false
SOMETHING_TRUE: true
SOME_TEXT: text
jobs: jobs:

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"sort"
"testing" "testing"
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
@ -15,7 +16,7 @@ func TestEvaluate(t *testing.T) {
Config: &Config{ Config: &Config{
Workdir: ".", Workdir: ".",
Secrets: map[string]string{ Secrets: map[string]string{
"LOWER_CASE_SECRET": "value", "CASE_INSENSITIVE_SECRET": "value",
}, },
}, },
Env: map[string]string{ Env: map[string]string{
@ -107,7 +108,8 @@ func TestEvaluate(t *testing.T) {
{"matrix.os", "Linux", ""}, {"matrix.os", "Linux", ""},
{"matrix.foo", "bar", ""}, {"matrix.foo", "bar", ""},
{"env.key", "value", ""}, {"env.key", "value", ""},
{"secrets.lower_case_secret", "value", ""}, {"secrets.CASE_INSENSITIVE_SECRET", "value", ""},
{"secrets.case_insensitive_secret", "value", ""},
} }
for _, table := range tables { for _, table := range tables {
@ -131,7 +133,7 @@ func TestInterpolate(t *testing.T) {
Config: &Config{ Config: &Config{
Workdir: ".", Workdir: ".",
Secrets: map[string]string{ Secrets: map[string]string{
"LOWER_CASE_SECRET": "value", "CASE_INSENSITIVE_SECRET": "value",
}, },
}, },
Env: map[string]string{ Env: map[string]string{
@ -160,7 +162,8 @@ func TestInterpolate(t *testing.T) {
{" ${{ env.KEYWITHNOTHING }} ", " valuewithnothing "}, {" ${{ env.KEYWITHNOTHING }} ", " valuewithnothing "},
{" ${{ env.KEY-WITH-HYPHENS }} ", " value-with-hyphens "}, {" ${{ env.KEY-WITH-HYPHENS }} ", " value-with-hyphens "},
{" ${{ env.KEY_WITH_UNDERSCORES }} ", " value_with_underscores "}, {" ${{ env.KEY_WITH_UNDERSCORES }} ", " value_with_underscores "},
{" ${{ secrets.lower_case_secret }} ", " value "}, {"${{ secrets.CASE_INSENSITIVE_SECRET }}", "value"},
{"${{ secrets.case_insensitive_secret }}", "value"},
{"${{ env.UNKNOWN }}", ""}, {"${{ env.UNKNOWN }}", ""},
{"${{ env.SOMETHING_TRUE }}", "true"}, {"${{ env.SOMETHING_TRUE }}", "true"},
{"${{ env.SOMETHING_FALSE }}", "false"}, {"${{ env.SOMETHING_FALSE }}", "false"},
@ -198,11 +201,15 @@ func updateTestExpressionWorkflow(t *testing.T, tables []struct {
}, rc *RunContext) { }, rc *RunContext) {
var envs string var envs string
for k, v := range rc.Env { keys := make([]string, 0, len(rc.Env))
envs += fmt.Sprintf( for k := range rc.Env {
` %s: %s keys = append(keys, k)
`, k, v)
} }
sort.Strings(keys)
for _, k := range keys {
envs += fmt.Sprintf(" %s: %s\n", k, rc.Env[k])
}
workflow := fmt.Sprintf(` workflow := fmt.Sprintf(`
name: "Test how expressions are handled on Github" name: "Test how expressions are handled on Github"
on: push on: push
@ -223,10 +230,7 @@ jobs:
}) })
name := fmt.Sprintf(`%s -> %s should be equal to %s`, expr, table.in, table.out) name := fmt.Sprintf(`%s -> %s should be equal to %s`, expr, table.in, table.out)
echo := `run: echo "Done "` echo := `run: echo "Done "`
workflow += fmt.Sprintf(` workflow += fmt.Sprintf("\n - name: %s\n %s\n", name, echo)
- name: %s
%s
`, name, echo)
} }
file, err := os.Create("../../.github/workflows/test-expressions.yml") file, err := os.Create("../../.github/workflows/test-expressions.yml")

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"os" "os"
"regexp" "regexp"
"sort"
"strings" "strings"
"testing" "testing"
@ -158,11 +159,15 @@ func updateTestIfWorkflow(t *testing.T, tables []struct {
}, rc *RunContext) { }, rc *RunContext) {
var envs string var envs string
for k, v := range rc.Env { keys := make([]string, 0, len(rc.Env))
envs += fmt.Sprintf( for k := range rc.Env {
` %s: %s keys = append(keys, k)
`, k, v)
} }
sort.Strings(keys)
for _, k := range keys {
envs += fmt.Sprintf(" %s: %s\n", k, rc.Env[k])
}
workflow := fmt.Sprintf(` workflow := fmt.Sprintf(`
name: "Test what expressions result in true and false on Github" name: "Test what expressions result in true and false on Github"
on: push on: push
@ -191,18 +196,9 @@ jobs:
echo = `run: echo OK` echo = `run: echo OK`
name = fmt.Sprintf(`"✅ I should run, expr: %s"`, expr) name = fmt.Sprintf(`"✅ I should run, expr: %s"`, expr)
} }
workflow += fmt.Sprintf(` workflow += fmt.Sprintf("\n - name: %s\n id: step%d\n if: %s\n %s\n", name, i, table.in, echo)
- name: %s
id: step%d
if: %s
%s
`, name, i, table.in, echo)
if table.out { if table.out {
workflow += fmt.Sprintf(` 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)
- name: "Double checking expr: %s"
if: steps.step%d.conclusion == 'skipped'
run: echo "%s should have been true, but wasn't"
`, expr, i, table.in)
} }
} }