package normalizer import ( "testing" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/yaml" ) func normalizeAndValidate(t *testing.T, unformatted []*yaml.RNode, expected string) { if err := Normalize(unformatted); err != nil { t.Fatal(err) } else if output, err := kio.StringAll(unformatted); err != nil { t.Fatal(err) } else if output != expected { t.Logf("Expected output:\n%s\n", expected) t.Logf("Normalize function output:\n%s\n", output) t.Fatalf("Normalize function output does not equal expected output.") } else { t.Logf("Formatted output:\n%s\n", output) } t.Logf("Successfully normalized yaml") } func verifyKioParsesAndPrintsYamlIdempotently(t *testing.T, unformatted []*yaml.RNode) { input, err := kio.StringAll(unformatted) if err != nil { t.Fatal(err) } t.Logf("Unformatted input:\n%s", input) // duplicate the input. duplicated, err := kio.ParseAll(input) if err != nil { t.Fatal(err) } if input2, err := kio.StringAll(duplicated); err != nil { t.Fatal(err) } else if input2 != input { t.Fatalf("Got differing input on reserialization. Function is not idempotent") } t.Logf("Kio parses and prints itself idempotently") } func TestCommentsDoNotGetClobbered(t *testing.T) { var unformatted = []*yaml.RNode{ yaml.MustParse("apiVersion: v1\nspec:\n name: Foo\nkind: test # hello comments\n"), yaml.MustParse("spec:\n name: Baz\napiVersion: v1\n# comment line 1\n# comment line 2\n# comment line 3\nkind: test\n"), yaml.MustParse("kind: test\nspec:\n name: Bar\napiVersion: v1"), } verifyKioParsesAndPrintsYamlIdempotently(t, unformatted) var normalizedData = `apiVersion: v1 # comment line 1 # comment line 2 # comment line 3 kind: test spec: name: Baz --- apiVersion: v1 kind: test spec: name: Bar --- apiVersion: v1 kind: test # hello comments spec: name: Foo ` normalizeAndValidate(t, unformatted, normalizedData) } func TestHeadingCommentsDoNotGetClobbered(t *testing.T) { var unformatted = []*yaml.RNode{ yaml.MustParse("# field comment\n# field comment line 2\napiVersion: v1\nspec:\n name: Foo\nkind: test # hello comments\n"), yaml.MustParse("# field comment\n# field comment 2\nspec:\n name: Baz\napiVersion: v1\n# comment line 1\n# comment line 2\n# comment line 3\nkind: test\n"), yaml.MustParse("kind: test\nspec:\n name: Bar\napiVersion: v1"), // notice the second newline ---------------------------V yaml.MustParse("# Heading comment\n# Heading comment 2\n\nspec:\n name: Bit\napiVersion: v1\n# comment line 1\n# comment line 2\n# comment line 3\nkind: test\n"), yaml.MustParse("kind: test\nspec:\n name: Barn\napiVersion: v1\n\n# footer line 1\n# footer line 2\n"), } verifyKioParsesAndPrintsYamlIdempotently(t, unformatted) var normalizedData = `# Heading comment # Heading comment 2 apiVersion: v1 # comment line 1 # comment line 2 # comment line 3 kind: test spec: name: Bit --- # field comment # field comment line 2 apiVersion: v1 kind: test # hello comments spec: name: Foo --- apiVersion: v1 # comment line 1 # comment line 2 # comment line 3 kind: test # field comment # field comment 2 spec: name: Baz --- apiVersion: v1 kind: test spec: name: Bar --- apiVersion: v1 kind: test spec: name: Barn # footer line 1 # footer line 2 ` normalizeAndValidate(t, unformatted, normalizedData) }