Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving escape values control on quality report #37

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions decoration/azuredevops/models/template_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ type TemplateModel struct {
pullRequest string `dummy:"pullrequest"`
status string `dummy:"status"`
statusColor string `dummy:"status-color"`
coverage string `dummy:"cov"`
coverage string `dummy:"cov" escape:"true"`
coverageStatus string `dummy:"cov-status"`
coverageStatusColor string `dummy:"cov-status-color"`
duplication string `dummy:"dup"`
duplication string `dummy:"dup" escape:"true"`
duplicationStatus string `dummy:"dup-status"`
duplicationStatusColor string `dummy:"dup-status-color"`
reliabilityStatus string `dummy:"rel-status"`
Expand Down
18 changes: 14 additions & 4 deletions decoration/template/engine/dummy/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"sonarci/decoration/template/engine/dummy/resources"
"strconv"
)

type Engine struct {
Expand Down Expand Up @@ -42,15 +43,24 @@ func (eng *Engine) processDataSource(template string, dataSource interface{}) (s

t := reflect.TypeOf(dataSource)
for i := 0; i < v.NumField(); i++ {
template = processDataSourceField(t.Field(i).Tag.Get("dummy"), v.Field(i).String(), template)
name := t.Field(i).Tag.Get("dummy")
escape, err := strconv.ParseBool(t.Field(i).Tag.Get("escape"))
if err != nil {
escape = false
}
template = processDataSourceField(name, v.Field(i).String(), template, escape)
}
return template, nil
}

func processDataSourceField(name string, value string, template string) string {
func processDataSourceField(name string, value string, template string, escape bool) string {
reg := regexp.MustCompile(`\$\{` + name + `\}`)
escapedValue := escapeValue(value)
return reg.ReplaceAllString(template, escapedValue)

if escape {
value = escapeValue(value)
}

return reg.ReplaceAllString(template, value)
}

func escapeValue(value string) string {
Expand Down
47 changes: 44 additions & 3 deletions decoration/template/engine/dummy/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ func Test_dummyEngine_ProcessTemplate(t *testing.T) {
}
}

func Test_dummyEngine_ProcessTemplate_WithEscape(t *testing.T) {
eng := NewEngine()
got, _ := eng.ProcessTemplate("Value1 = \"${field1}\"\nValue2 = \"${field2}\"", struct {
field1 string `dummy:"field1"`
field2 string `dummy:"field2" escape:"true"`
}{
field1: "value1",
field2: "value2 test with escape",
})

want := "Value1 = \"value1\"\nValue2 = \"value2%20test%20with%20escape\""
if got != want {
t.Errorf("Result invalid, want \"%s\" but got \"%s\"", want, got)
}
}

func Test_dummyEngine_ProcessTemplate_WithoutEscape(t *testing.T) {
eng := NewEngine()
got, _ := eng.ProcessTemplate("Value1 = \"${field1}\"\nValue2 = \"${field2}\"", struct {
field1 string `dummy:"field1"`
field2 string `dummy:"field2"`
}{
field1: "value1",
field2: "value2 test without escape",
})

want := "Value1 = \"value1\"\nValue2 = \"value2 test without escape\""
if got != want {
t.Errorf("Result invalid, want \"%s\" but got \"%s\"", want, got)
}
}

func Test_dummyEngine_ProcessTemplate_WithEmptyTemplate_ShouldReturnError(t *testing.T) {
eng := NewEngine()
_, err := eng.ProcessTemplate("", struct{}{})
Expand Down Expand Up @@ -110,9 +142,18 @@ func Test_dummyEngine_ProcessTemplate_WithInvalidDataSource_ShouldNotReturnValue
}
}

func Test_processDataSourceField_ShouldReturnExpectedValue(t *testing.T) {
got := processDataSourceField("test", "testing-message", "Testing message: \"${test}\"")
want := "Testing message: \"testing-message\""
func Test_processDataSourceField_WithEscape_ShouldReturnExpectedValue(t *testing.T) {
got := processDataSourceField("test", "Hi, i'm testing here", "Testing message: \"${test}\"", true)
want := "Testing message: \"Hi%2C%20i%27m%20testing%20here\""

if got != want {
t.Errorf("Should process right value. Got \"%s\" but want \"%s\"", got, want)
}
}

func Test_processDataSourceField_WithoutEscape_ShouldReturnExpectedValue(t *testing.T) {
got := processDataSourceField("test", "Hi, i'm testing here", "Testing message: \"${test}\"", false)
want := "Testing message: \"Hi, i'm testing here\""

if got != want {
t.Errorf("Should process right value. Got \"%s\" but want \"%s\"", got, want)
Expand Down