github.1485827954.workers.dev/nektos/act@v0.2.63/pkg/runner/command_test.go (about) 1 package runner 2 3 import ( 4 "bytes" 5 "context" 6 "io" 7 "os" 8 "testing" 9 10 "github.com/sirupsen/logrus/hooks/test" 11 "github.com/stretchr/testify/assert" 12 13 "github.com/nektos/act/pkg/common" 14 "github.com/nektos/act/pkg/model" 15 ) 16 17 func TestSetEnv(t *testing.T) { 18 a := assert.New(t) 19 ctx := context.Background() 20 rc := new(RunContext) 21 handler := rc.commandHandler(ctx) 22 23 handler("::set-env name=x::valz\n") 24 a.Equal("valz", rc.Env["x"]) 25 } 26 27 func TestSetOutput(t *testing.T) { 28 a := assert.New(t) 29 ctx := context.Background() 30 rc := new(RunContext) 31 rc.StepResults = make(map[string]*model.StepResult) 32 handler := rc.commandHandler(ctx) 33 34 rc.CurrentStep = "my-step" 35 rc.StepResults[rc.CurrentStep] = &model.StepResult{ 36 Outputs: make(map[string]string), 37 } 38 handler("::set-output name=x::valz\n") 39 a.Equal("valz", rc.StepResults["my-step"].Outputs["x"]) 40 41 handler("::set-output name=x::percent2%25\n") 42 a.Equal("percent2%", rc.StepResults["my-step"].Outputs["x"]) 43 44 handler("::set-output name=x::percent2%25%0Atest\n") 45 a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x"]) 46 47 handler("::set-output name=x::percent2%25%0Atest another3%25test\n") 48 a.Equal("percent2%\ntest another3%test", rc.StepResults["my-step"].Outputs["x"]) 49 50 handler("::set-output name=x%3A::percent2%25%0Atest\n") 51 a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:"]) 52 53 handler("::set-output name=x%3A%2C%0A%25%0D%3A::percent2%25%0Atest\n") 54 a.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:,\n%\r:"]) 55 } 56 57 func TestAddpath(t *testing.T) { 58 a := assert.New(t) 59 ctx := context.Background() 60 rc := new(RunContext) 61 handler := rc.commandHandler(ctx) 62 63 handler("::add-path::/zoo\n") 64 a.Equal("/zoo", rc.ExtraPath[0]) 65 66 handler("::add-path::/boo\n") 67 a.Equal("/boo", rc.ExtraPath[0]) 68 } 69 70 func TestStopCommands(t *testing.T) { 71 logger, hook := test.NewNullLogger() 72 73 a := assert.New(t) 74 ctx := common.WithLogger(context.Background(), logger) 75 rc := new(RunContext) 76 handler := rc.commandHandler(ctx) 77 78 handler("::set-env name=x::valz\n") 79 a.Equal("valz", rc.Env["x"]) 80 handler("::stop-commands::my-end-token\n") 81 handler("::set-env name=x::abcd\n") 82 a.Equal("valz", rc.Env["x"]) 83 handler("::my-end-token::\n") 84 handler("::set-env name=x::abcd\n") 85 a.Equal("abcd", rc.Env["x"]) 86 87 messages := make([]string, 0) 88 for _, entry := range hook.AllEntries() { 89 messages = append(messages, entry.Message) 90 } 91 92 a.Contains(messages, " \U00002699 ::set-env name=x::abcd\n") 93 } 94 95 func TestAddpathADO(t *testing.T) { 96 a := assert.New(t) 97 ctx := context.Background() 98 rc := new(RunContext) 99 handler := rc.commandHandler(ctx) 100 101 handler("##[add-path]/zoo\n") 102 a.Equal("/zoo", rc.ExtraPath[0]) 103 104 handler("##[add-path]/boo\n") 105 a.Equal("/boo", rc.ExtraPath[0]) 106 } 107 108 func TestAddmask(t *testing.T) { 109 logger, hook := test.NewNullLogger() 110 111 a := assert.New(t) 112 ctx := context.Background() 113 loggerCtx := common.WithLogger(ctx, logger) 114 115 rc := new(RunContext) 116 handler := rc.commandHandler(loggerCtx) 117 handler("::add-mask::my-secret-value\n") 118 119 a.Equal(" \U00002699 ***", hook.LastEntry().Message) 120 a.NotEqual(" \U00002699 *my-secret-value", hook.LastEntry().Message) 121 } 122 123 // based on https://stackoverflow.com/a/10476304 124 func captureOutput(t *testing.T, f func()) string { 125 old := os.Stdout 126 r, w, _ := os.Pipe() 127 os.Stdout = w 128 129 f() 130 131 outC := make(chan string) 132 133 go func() { 134 var buf bytes.Buffer 135 _, err := io.Copy(&buf, r) 136 if err != nil { 137 a := assert.New(t) 138 a.Fail("io.Copy failed") 139 } 140 outC <- buf.String() 141 }() 142 143 w.Close() 144 os.Stdout = old 145 out := <-outC 146 147 return out 148 } 149 150 func TestAddmaskUsemask(t *testing.T) { 151 rc := new(RunContext) 152 rc.StepResults = make(map[string]*model.StepResult) 153 rc.CurrentStep = "my-step" 154 rc.StepResults[rc.CurrentStep] = &model.StepResult{ 155 Outputs: make(map[string]string), 156 } 157 158 a := assert.New(t) 159 160 config := &Config{ 161 Secrets: map[string]string{}, 162 InsecureSecrets: false, 163 } 164 165 re := captureOutput(t, func() { 166 ctx := context.Background() 167 ctx = WithJobLogger(ctx, "0", "testjob", config, &rc.Masks, map[string]interface{}{}) 168 169 handler := rc.commandHandler(ctx) 170 handler("::add-mask::secret\n") 171 handler("::set-output:: token=secret\n") 172 }) 173 174 a.Equal("[testjob] \U00002699 ***\n[testjob] \U00002699 ::set-output:: = token=***\n", re) 175 } 176 177 func TestSaveState(t *testing.T) { 178 rc := &RunContext{ 179 CurrentStep: "step", 180 StepResults: map[string]*model.StepResult{}, 181 } 182 183 ctx := context.Background() 184 185 handler := rc.commandHandler(ctx) 186 handler("::save-state name=state-name::state-value\n") 187 188 assert.Equal(t, "state-value", rc.IntraActionState["step"]["state-name"]) 189 }