From 9622b49c457ed3cf72689c78ab94352e1e8e0e89 Mon Sep 17 00:00:00 2001 From: Joern Barthel Date: Fri, 7 Apr 2017 10:41:55 +0200 Subject: [PATCH] Support for Windows newlines. --- config/interpolate_funcs.go | 3 ++- config/interpolate_funcs_test.go | 36 ++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index f70fbdec6..bc2c49f45 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -462,11 +462,12 @@ func interpolationFuncCeil() ast.Function { // interpolationFuncChomp removes trailing newlines from the given string func interpolationFuncChomp() ast.Function { + newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`) return ast.Function{ ArgTypes: []ast.Type{ast.TypeString}, ReturnType: ast.TypeString, Callback: func(args []interface{}) (interface{}, error) { - return strings.TrimRight(args[0].(string), "\n"), nil + return newlines.ReplaceAllString(args[0].(string), ""), nil }, } } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 6c430add1..78816b6dd 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -386,10 +386,38 @@ func TestInterpolateFuncChomp(t *testing.T) { }, { - `${chomp("goodbye\ncruel\nworld\n")}`, - `goodbye -cruel -world`, + fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld"), + "goodbye\ncruel\nworld", + false, + }, + + { + fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld"), + "goodbye\r\nwindows\r\nworld", + false, + }, + + { + fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld\n"), + "goodbye\ncruel\nworld", + false, + }, + + { + fmt.Sprintf(`${chomp("%s")}`, "goodbye\ncruel\nworld\n\n\n\n"), + "goodbye\ncruel\nworld", + false, + }, + + { + fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld\r\n"), + "goodbye\r\nwindows\r\nworld", + false, + }, + + { + fmt.Sprintf(`${chomp("%s")}`, "goodbye\r\nwindows\r\nworld\r\n\r\n\r\n\r\n"), + "goodbye\r\nwindows\r\nworld", false, }, },