fix: outputs espace (#404)

* fix: outputs espace

* refactor: move unescape to func

* refactor: fix ci lint

* refactor: unescape function
This commit is contained in:
Owen Young 2020-11-02 21:40:46 +08:00 committed by GitHub
parent 1e69525fb4
commit 92067eb1a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View file

@ -38,7 +38,8 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
if resumeCommand != "" && command != resumeCommand {
return false
}
arg = unescapeCommandData(arg)
kvPairs = unescapeKvPairs(kvPairs)
switch command {
case "set-env":
rc.setEnv(ctx, kvPairs, arg)
@ -95,3 +96,33 @@ func parseKeyValuePairs(kvPairs string, separator string) map[string]string {
}
return rtn
}
func unescapeCommandData(arg string) string {
escapeMap := map[string]string{
"%25": "%",
"%0D": "\r",
"%0A": "\n",
}
for k, v := range escapeMap {
arg = strings.Replace(arg, k, v, -1)
}
return arg
}
func unescapeCommandProperty(arg string) string {
escapeMap := map[string]string{
"%25": "%",
"%0D": "\r",
"%0A": "\n",
"%3A": ":",
"%2C": ",",
}
for k, v := range escapeMap {
arg = strings.Replace(arg, k, v, -1)
}
return arg
}
func unescapeKvPairs(kvPairs map[string]string) map[string]string {
for k, v := range kvPairs {
kvPairs[k] = unescapeCommandProperty(v)
}
return kvPairs
}

View file

@ -30,6 +30,21 @@ func TestSetOutput(t *testing.T) {
}
handler("::set-output name=x::valz\n")
assert.Equal("valz", rc.StepResults["my-step"].Outputs["x"])
handler("::set-output name=x::percent2%25\n")
assert.Equal("percent2%", rc.StepResults["my-step"].Outputs["x"])
handler("::set-output name=x::percent2%25%0Atest\n")
assert.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x"])
handler("::set-output name=x::percent2%25%0Atest another3%25test\n")
assert.Equal("percent2%\ntest another3%test", rc.StepResults["my-step"].Outputs["x"])
handler("::set-output name=x%3A::percent2%25%0Atest\n")
assert.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:"])
handler("::set-output name=x%3A%2C%0A%25%0D%3A::percent2%25%0Atest\n")
assert.Equal("percent2%\ntest", rc.StepResults["my-step"].Outputs["x:,\n%\r:"])
}
func TestAddpath(t *testing.T) {