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

     1  package yam
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/chainguard-dev/yam/pkg/rwfs/os"
     7  	"github.com/chainguard-dev/yam/pkg/yam/formatted"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func didNotPassLintCheck(t assert.TestingT, err error, i ...interface{}) bool {
    12  	return assert.ErrorIs(t, err, ErrDidNotPassLintCheck)
    13  }
    14  
    15  func TestLint(t *testing.T) {
    16  	cases := []struct {
    17  		name      string
    18  		paths     []string
    19  		opts      FormatOptions
    20  		assertErr assert.ErrorAssertionFunc
    21  	}{
    22  		{
    23  			name:  "single correct file",
    24  			paths: []string{"fruits.yaml"},
    25  			opts: FormatOptions{
    26  				EncodeOptions: formatted.EncodeOptions{
    27  					Indent:         4,
    28  					GapExpressions: []string{".fruits"},
    29  				},
    30  				FinalNewline:           true,
    31  				TrimTrailingWhitespace: true,
    32  			},
    33  			assertErr: assert.NoError,
    34  		},
    35  		{
    36  			name:  "single incorrect file",
    37  			paths: []string{"vegetables.yaml"},
    38  			opts: FormatOptions{
    39  				EncodeOptions: formatted.EncodeOptions{
    40  					Indent:         4,
    41  					GapExpressions: []string{".vegetables"},
    42  				},
    43  				FinalNewline:           true,
    44  				TrimTrailingWhitespace: true,
    45  			},
    46  			assertErr: didNotPassLintCheck,
    47  		},
    48  		{
    49  			name:  "directory with correct files",
    50  			paths: []string{"all-correct-files"},
    51  			opts: FormatOptions{
    52  				EncodeOptions: formatted.EncodeOptions{
    53  					Indent:         4,
    54  					GapExpressions: []string{".fruits", ".vegetables"},
    55  				},
    56  				FinalNewline:           true,
    57  				TrimTrailingWhitespace: true,
    58  			},
    59  			assertErr: assert.NoError,
    60  		},
    61  		{
    62  			name:  "directory with an incorrect file",
    63  			paths: []string{"has-incorrect-file"},
    64  			opts: FormatOptions{
    65  				EncodeOptions: formatted.EncodeOptions{
    66  					Indent:         2,
    67  					GapExpressions: []string{".fruits", ".vegetables", ".candies.colorful"},
    68  				},
    69  				FinalNewline:           true,
    70  				TrimTrailingWhitespace: true,
    71  			},
    72  			assertErr: didNotPassLintCheck,
    73  		},
    74  	}
    75  
    76  	for _, tt := range cases {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			fsys := os.DirFS("testdata/lint")
    79  
    80  			err := Lint(fsys, tt.paths, ExecDiff, tt.opts)
    81  			tt.assertErr(t, err)
    82  		})
    83  	}
    84  }