From 4b89d6f8a7d599ba1648f22d6094373a35e053a3 Mon Sep 17 00:00:00 2001 From: Zahiar Ahmed Date: Sat, 11 Sep 2021 23:01:49 +0100 Subject: [PATCH] Unescape HTML entities during state diff (#63) This solves a state inconsistency whereby if you create a Jenkins job using XML that contains HTML entities, on subsequent plans, it will show an unclean diff, bouncing between escaped & unescaped HTML entities. The Jenkins job itself works perfectly fine but this unclean diff can make plans annoying, and if you have many jobs provisioned, the XML template can take up a large portion of the plan. --- jenkins/util.go | 3 +++ jenkins/util_test.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/jenkins/util.go b/jenkins/util.go index 3ebd3ec..4a1159f 100644 --- a/jenkins/util.go +++ b/jenkins/util.go @@ -3,6 +3,7 @@ package jenkins import ( "context" "fmt" + "html" "log" "regexp" "strings" @@ -83,9 +84,11 @@ func templateDiff(k, old, new string, d *schema.ResourceData) bool { old = re.ReplaceAllString(old, "") old = strings.Replace(old, " ", "", -1) old = strings.TrimSpace(old) + old = html.UnescapeString(old) new = re.ReplaceAllString(new, "") new = strings.Replace(new, " ", "", -1) new = strings.TrimSpace(new) + new = html.UnescapeString(new) log.Printf("[DEBUG] jenkins::diff - Old: %q", old) log.Printf("[DEBUG] jenkins::diff - New: %q", new) diff --git a/jenkins/util_test.go b/jenkins/util_test.go index be07151..6aa49e5 100644 --- a/jenkins/util_test.go +++ b/jenkins/util_test.go @@ -108,6 +108,24 @@ func TestTemplateDiff(t *testing.T) { } } +func TestTemplateDiff_HTMLEntities(t *testing.T) { + job := resourceJenkinsFolder() + bag := job.TestResourceData() + _ = bag.Set("description", "Case") + + inputLeft := "'/'" + inputRight := "'/'" + if actual := templateDiff("", inputLeft, inputRight, bag); !actual { + t.Errorf("Expected %s to be considered equal to %s", inputLeft, inputRight) + } + + inputLeft = "'/'" + inputRight = "'/'" + if actual := templateDiff("", inputLeft, inputRight, bag); !actual { + t.Errorf("Expected %s to be considered equal to %s", inputLeft, inputRight) + } +} + func TestGenerateCredentialID(t *testing.T) { inputFolder, inputName := "test-folder", "test-name" actual := generateCredentialID(inputFolder, inputName)