2020-02-12 01:38:30 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-03-02 02:29:34 -06:00
|
|
|
"bytes"
|
2020-02-12 01:38:30 -06:00
|
|
|
"context"
|
2022-03-02 02:29:34 -06:00
|
|
|
"io"
|
|
|
|
"os"
|
2020-02-12 01:38:30 -06:00
|
|
|
"testing"
|
|
|
|
|
2021-05-15 16:11:16 -05:00
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
2020-02-12 01:38:30 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-06-23 13:09:27 -05:00
|
|
|
|
|
|
|
"github.com/nektos/act/pkg/common"
|
2021-12-22 13:52:09 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
2020-02-12 01:38:30 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetEnv(t *testing.T) {
|
2021-01-12 00:39:43 -06:00
|
|
|
a := assert.New(t)
|
2020-02-12 01:38:30 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
rc := new(RunContext)
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
|
|
|
|
handler("::set-env name=x::valz\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("valz", rc.Env["x"])
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetOutput(t *testing.T) {
|
2021-01-12 00:39:43 -06:00
|
|
|
a := assert.New(t)
|
2020-02-12 01:38:30 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
rc := new(RunContext)
|
2021-12-22 13:52:09 -06:00
|
|
|
rc.StepResults = make(map[string]*model.StepResult)
|
2020-02-12 01:38:30 -06:00
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
|
2020-02-14 02:41:20 -06:00
|
|
|
rc.CurrentStep = "my-step"
|
2021-12-22 13:52:09 -06:00
|
|
|
rc.StepResults[rc.CurrentStep] = &model.StepResult{
|
2020-02-14 02:41:20 -06:00
|
|
|
Outputs: make(map[string]string),
|
|
|
|
}
|
2020-02-12 01:38:30 -06:00
|
|
|
handler("::set-output name=x::valz\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("valz", rc.StepResults["my-step"].Outputs["x"])
|
2020-11-02 07:40:46 -06:00
|
|
|
|
|
|
|
handler("::set-output name=x::percent2%25\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("percent2%", rc.StepResults["my-step"].Outputs["x"])
|
2020-11-02 07:40:46 -06:00
|
|
|
|
|
|
|
handler("::set-output name=x::percent2%25%0Atest\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x"])
|
2020-11-02 07:40:46 -06:00
|
|
|
|
|
|
|
handler("::set-output name=x::percent2%25%0Atest another3%25test\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("percent2%\ntest another3%test", rc.StepResults["my-step"].Outputs["x"])
|
2020-11-02 07:40:46 -06:00
|
|
|
|
|
|
|
handler("::set-output name=x%3A::percent2%25%0Atest\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:"])
|
2020-11-02 07:40:46 -06:00
|
|
|
|
|
|
|
handler("::set-output name=x%3A%2C%0A%25%0D%3A::percent2%25%0Atest\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:,\n%\r:"])
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddpath(t *testing.T) {
|
2021-01-12 00:39:43 -06:00
|
|
|
a := assert.New(t)
|
2020-02-12 01:38:30 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
rc := new(RunContext)
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
|
2020-02-12 01:55:20 -06:00
|
|
|
handler("::add-path::/zoo\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("/zoo", rc.ExtraPath[0])
|
2020-02-12 01:38:30 -06:00
|
|
|
|
2020-02-13 13:47:38 -06:00
|
|
|
handler("::add-path::/boo\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("/boo", rc.ExtraPath[1])
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopCommands(t *testing.T) {
|
2021-12-09 09:10:56 -06:00
|
|
|
logger, hook := test.NewNullLogger()
|
|
|
|
|
2021-01-12 00:39:43 -06:00
|
|
|
a := assert.New(t)
|
2021-12-09 09:10:56 -06:00
|
|
|
ctx := common.WithLogger(context.Background(), logger)
|
2020-02-12 01:38:30 -06:00
|
|
|
rc := new(RunContext)
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
|
|
|
|
handler("::set-env name=x::valz\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("valz", rc.Env["x"])
|
2020-02-12 01:38:30 -06:00
|
|
|
handler("::stop-commands::my-end-token\n")
|
|
|
|
handler("::set-env name=x::abcd\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("valz", rc.Env["x"])
|
2020-02-12 01:38:30 -06:00
|
|
|
handler("::my-end-token::\n")
|
|
|
|
handler("::set-env name=x::abcd\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("abcd", rc.Env["x"])
|
2021-12-09 09:10:56 -06:00
|
|
|
|
|
|
|
messages := make([]string, 0)
|
|
|
|
for _, entry := range hook.AllEntries() {
|
|
|
|
messages = append(messages, entry.Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
a.Contains(messages, " \U00002699 ::set-env name=x::abcd\n")
|
2020-02-12 01:38:30 -06:00
|
|
|
}
|
2020-02-24 14:48:12 -06:00
|
|
|
|
|
|
|
func TestAddpathADO(t *testing.T) {
|
2021-01-12 00:39:43 -06:00
|
|
|
a := assert.New(t)
|
2020-02-24 14:48:12 -06:00
|
|
|
ctx := context.Background()
|
|
|
|
rc := new(RunContext)
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
|
|
|
|
handler("##[add-path]/zoo\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("/zoo", rc.ExtraPath[0])
|
2020-02-24 14:48:12 -06:00
|
|
|
|
|
|
|
handler("##[add-path]/boo\n")
|
2021-01-12 00:39:43 -06:00
|
|
|
a.Equal("/boo", rc.ExtraPath[1])
|
2020-02-24 14:48:12 -06:00
|
|
|
}
|
2021-05-15 16:11:16 -05:00
|
|
|
|
|
|
|
func TestAddmask(t *testing.T) {
|
|
|
|
logger, hook := test.NewNullLogger()
|
|
|
|
|
|
|
|
a := assert.New(t)
|
|
|
|
ctx := context.Background()
|
|
|
|
loggerCtx := common.WithLogger(ctx, logger)
|
|
|
|
|
|
|
|
rc := new(RunContext)
|
|
|
|
handler := rc.commandHandler(loggerCtx)
|
|
|
|
handler("::add-mask::my-secret-value\n")
|
|
|
|
|
2022-03-02 02:29:34 -06:00
|
|
|
a.Equal(" \U00002699 ***", hook.LastEntry().Message)
|
2021-05-15 16:11:16 -05:00
|
|
|
a.NotEqual(" \U00002699 *my-secret-value", hook.LastEntry().Message)
|
|
|
|
}
|
2022-03-02 02:29:34 -06:00
|
|
|
|
|
|
|
// based on https://stackoverflow.com/a/10476304
|
|
|
|
func captureOutput(t *testing.T, f func()) string {
|
|
|
|
old := os.Stdout
|
|
|
|
r, w, _ := os.Pipe()
|
|
|
|
os.Stdout = w
|
|
|
|
|
|
|
|
f()
|
|
|
|
|
|
|
|
outC := make(chan string)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
_, err := io.Copy(&buf, r)
|
|
|
|
if err != nil {
|
|
|
|
a := assert.New(t)
|
|
|
|
a.Fail("io.Copy failed")
|
|
|
|
}
|
|
|
|
outC <- buf.String()
|
|
|
|
}()
|
|
|
|
|
|
|
|
w.Close()
|
|
|
|
os.Stdout = old
|
|
|
|
out := <-outC
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddmaskUsemask(t *testing.T) {
|
|
|
|
rc := new(RunContext)
|
|
|
|
rc.StepResults = make(map[string]*model.StepResult)
|
|
|
|
rc.CurrentStep = "my-step"
|
|
|
|
rc.StepResults[rc.CurrentStep] = &model.StepResult{
|
|
|
|
Outputs: make(map[string]string),
|
|
|
|
}
|
|
|
|
|
|
|
|
a := assert.New(t)
|
|
|
|
|
2022-03-14 10:33:11 -05:00
|
|
|
config := &Config{
|
|
|
|
Secrets: map[string]string{},
|
|
|
|
InsecureSecrets: false,
|
|
|
|
}
|
|
|
|
|
2022-03-02 02:29:34 -06:00
|
|
|
re := captureOutput(t, func() {
|
|
|
|
ctx := context.Background()
|
2022-09-27 10:49:22 -05:00
|
|
|
ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks, map[string]interface{}{})
|
2022-03-02 02:29:34 -06:00
|
|
|
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
handler("::add-mask::secret\n")
|
|
|
|
handler("::set-output:: token=secret\n")
|
|
|
|
})
|
|
|
|
|
|
|
|
a.Equal("[testjob] \U00002699 ***\n[testjob] \U00002699 ::set-output:: = token=***\n", re)
|
|
|
|
}
|
2022-05-24 08:36:06 -05:00
|
|
|
|
|
|
|
func TestSaveState(t *testing.T) {
|
|
|
|
rc := &RunContext{
|
|
|
|
CurrentStep: "step",
|
|
|
|
StepResults: map[string]*model.StepResult{
|
|
|
|
"step": {
|
|
|
|
State: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
handler := rc.commandHandler(ctx)
|
|
|
|
handler("::save-state name=state-name::state-value\n")
|
|
|
|
|
|
|
|
assert.Equal(t, "state-value", rc.StepResults["step"].State["state-name"])
|
|
|
|
}
|