...

Source file src/edge-infra.dev/pkg/edge/gitops/fns/normalizer/comment_test.go

Documentation: edge-infra.dev/pkg/edge/gitops/fns/normalizer

     1  package normalizer
     2  
     3  import (
     4  	"testing"
     5  
     6  	"sigs.k8s.io/kustomize/kyaml/kio"
     7  	"sigs.k8s.io/kustomize/kyaml/yaml"
     8  )
     9  
    10  func normalizeAndValidate(t *testing.T, unformatted []*yaml.RNode, expected string) {
    11  	if err := Normalize(unformatted); err != nil {
    12  		t.Fatal(err)
    13  	} else if output, err := kio.StringAll(unformatted); err != nil {
    14  		t.Fatal(err)
    15  	} else if output != expected {
    16  		t.Logf("Expected output:\n%s\n", expected)
    17  		t.Logf("Normalize function output:\n%s\n", output)
    18  		t.Fatalf("Normalize function output does not equal expected output.")
    19  	} else {
    20  		t.Logf("Formatted output:\n%s\n", output)
    21  	}
    22  	t.Logf("Successfully normalized yaml")
    23  }
    24  
    25  func verifyKioParsesAndPrintsYamlIdempotently(t *testing.T, unformatted []*yaml.RNode) {
    26  	input, err := kio.StringAll(unformatted)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	t.Logf("Unformatted input:\n%s", input)
    31  
    32  	// duplicate the input.
    33  	duplicated, err := kio.ParseAll(input)
    34  	if err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	if input2, err := kio.StringAll(duplicated); err != nil {
    39  		t.Fatal(err)
    40  	} else if input2 != input {
    41  		t.Fatalf("Got differing input on reserialization. Function is not idempotent")
    42  	}
    43  	t.Logf("Kio parses and prints itself idempotently")
    44  }
    45  
    46  func TestCommentsDoNotGetClobbered(t *testing.T) {
    47  	var unformatted = []*yaml.RNode{
    48  		yaml.MustParse("apiVersion: v1\nspec:\n  name: Foo\nkind: test # hello comments\n"),
    49  		yaml.MustParse("spec:\n  name: Baz\napiVersion: v1\n# comment line 1\n# comment line 2\n# comment line 3\nkind: test\n"),
    50  		yaml.MustParse("kind: test\nspec:\n  name: Bar\napiVersion: v1"),
    51  	}
    52  
    53  	verifyKioParsesAndPrintsYamlIdempotently(t, unformatted)
    54  
    55  	var normalizedData = `apiVersion: v1
    56  # comment line 1
    57  # comment line 2
    58  # comment line 3
    59  kind: test
    60  spec:
    61    name: Baz
    62  ---
    63  apiVersion: v1
    64  kind: test
    65  spec:
    66    name: Bar
    67  ---
    68  apiVersion: v1
    69  kind: test # hello comments
    70  spec:
    71    name: Foo
    72  `
    73  
    74  	normalizeAndValidate(t, unformatted, normalizedData)
    75  }
    76  
    77  func TestHeadingCommentsDoNotGetClobbered(t *testing.T) {
    78  	var unformatted = []*yaml.RNode{
    79  		yaml.MustParse("# field comment\n# field comment line 2\napiVersion: v1\nspec:\n  name: Foo\nkind: test # hello comments\n"),
    80  		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"),
    81  		yaml.MustParse("kind: test\nspec:\n  name: Bar\napiVersion: v1"),
    82  		// notice the second newline ---------------------------V
    83  		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"),
    84  		yaml.MustParse("kind: test\nspec:\n  name: Barn\napiVersion: v1\n\n# footer line 1\n# footer line 2\n"),
    85  	}
    86  
    87  	verifyKioParsesAndPrintsYamlIdempotently(t, unformatted)
    88  
    89  	var normalizedData = `# Heading comment
    90  # Heading comment 2
    91  
    92  apiVersion: v1
    93  # comment line 1
    94  # comment line 2
    95  # comment line 3
    96  kind: test
    97  spec:
    98    name: Bit
    99  ---
   100  # field comment
   101  # field comment line 2
   102  apiVersion: v1
   103  kind: test # hello comments
   104  spec:
   105    name: Foo
   106  ---
   107  apiVersion: v1
   108  # comment line 1
   109  # comment line 2
   110  # comment line 3
   111  kind: test
   112  # field comment
   113  # field comment 2
   114  spec:
   115    name: Baz
   116  ---
   117  apiVersion: v1
   118  kind: test
   119  spec:
   120    name: Bar
   121  ---
   122  apiVersion: v1
   123  kind: test
   124  spec:
   125    name: Barn
   126  
   127  # footer line 1
   128  # footer line 2
   129  `
   130  
   131  	normalizeAndValidate(t, unformatted, normalizedData)
   132  }
   133  

View as plain text