github.com/speakeasy-api/sdk-gen-config@v1.14.2/lint/lint_test.go (about)

     1  package lint_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/daveshanley/vacuum/model"
     9  	"github.com/speakeasy-api/sdk-gen-config/lint"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestLint_Load_Success(t *testing.T) {
    15  	type args struct {
    16  		lintLocation string
    17  		lintContents string
    18  		workingDir   string
    19  	}
    20  	tests := []struct {
    21  		name string
    22  		args args
    23  		want *lint.Lint
    24  	}{
    25  		{
    26  			name: "loads simple lint file",
    27  			args: args{
    28  				lintLocation: "test/.speakeasy",
    29  				lintContents: `lintVersion: 1.0.0
    30  defaultRuleset: default
    31  rulesets:
    32    default:
    33      rulesets:
    34        - test
    35      rules:
    36        test: {}`,
    37  				workingDir: "test",
    38  			},
    39  			want: &lint.Lint{
    40  				Version:        "1.0.0",
    41  				DefaultRuleset: "default",
    42  				Rulesets: map[string]lint.Ruleset{
    43  					"default": {
    44  						Rulesets: []string{"test"},
    45  						Rules: map[string]*model.Rule{
    46  							"test": {},
    47  						},
    48  					},
    49  				},
    50  			},
    51  		},
    52  	}
    53  	for _, tt := range tests {
    54  		t.Run(tt.name, func(t *testing.T) {
    55  			basePath, err := os.MkdirTemp("", "lint*")
    56  			require.NoError(t, err)
    57  			defer os.RemoveAll(basePath)
    58  
    59  			err = createTempFile(filepath.Join(basePath, tt.args.lintLocation), "lint.yaml", tt.args.lintContents)
    60  			require.NoError(t, err)
    61  
    62  			workflowFile, workflowPath, err := lint.Load([]string{filepath.Join(basePath, tt.args.workingDir)})
    63  			require.NoError(t, err)
    64  
    65  			assert.Equal(t, tt.want, workflowFile)
    66  			assert.Contains(t, workflowPath, filepath.Join(tt.args.lintLocation, "lint.yaml"))
    67  		})
    68  	}
    69  }
    70  
    71  func createTempFile(dir string, fileName, contents string) error {
    72  	if err := os.MkdirAll(dir, os.ModePerm); err != nil {
    73  		return err
    74  	}
    75  
    76  	if contents != "" {
    77  		tmpFile := filepath.Join(dir, fileName)
    78  		if err := os.WriteFile(tmpFile, []byte(contents), os.ModePerm); err != nil {
    79  			return err
    80  		}
    81  	}
    82  
    83  	return nil
    84  }