github.com/chainguard-dev/yam@v0.0.7/pkg/yam/format_test.go (about)

     1  package yam
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/chainguard-dev/yam/pkg/rwfs/tester"
     7  	"github.com/chainguard-dev/yam/pkg/yam/formatted"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  var testOptions = FormatOptions{
    13  	EncodeOptions: formatted.EncodeOptions{
    14  		Indent: 2,
    15  		GapExpressions: []string{
    16  			".",
    17  		},
    18  	},
    19  	TrimTrailingWhitespace: true,
    20  	FinalNewline:           true,
    21  }
    22  
    23  func Test_formatSingleFile(t *testing.T) {
    24  	cases := []struct {
    25  		fixture string
    26  	}{
    27  		{
    28  			fixture: "testdata/format/simple.yaml",
    29  		},
    30  		{
    31  			fixture: "testdata/format/acl.yaml",
    32  		},
    33  		{
    34  			fixture: "testdata/format/comments.yaml",
    35  		},
    36  		{
    37  			fixture: "testdata/format/whitespace_issues.yaml",
    38  		},
    39  	}
    40  
    41  	for _, tt := range cases {
    42  		t.Run(tt.fixture, func(t *testing.T) {
    43  			fsys, err := tester.NewFS(tt.fixture)
    44  			require.NoError(t, err)
    45  
    46  			err = formatSingleFile(fsys, tt.fixture, testOptions)
    47  			assert.NoError(t, err)
    48  
    49  			if diff := fsys.Diff(tt.fixture); diff != "" {
    50  				t.Errorf(diff)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func TestFormat(t *testing.T) {
    57  	cases := []struct {
    58  		name  string
    59  		paths []string
    60  	}{
    61  		{
    62  			name: "multiple files and dirs",
    63  			paths: []string{
    64  				"testdata/dir-scenario-1/a.yaml",
    65  				"testdata/dir-scenario-1/subdir2",
    66  			},
    67  		},
    68  	}
    69  
    70  	for _, tt := range cases {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			fsys, err := tester.NewFS("testdata/dir-scenario-1")
    73  			require.NoError(t, err)
    74  
    75  			err = Format(fsys, tt.paths, testOptions)
    76  			require.NoError(t, err)
    77  
    78  			if diff := fsys.DiffAll(); diff != "" {
    79  				t.Errorf(diff)
    80  			}
    81  		})
    82  	}
    83  }