get.porter.sh/porter@v1.3.0/pkg/exec/lint_test.go (about)

     1  package exec
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"testing"
     8  
     9  	"get.porter.sh/porter/pkg/linter"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestMixin_Lint(t *testing.T) {
    15  	ctx := context.Background()
    16  	m := NewTestMixin(t)
    17  
    18  	input, err := os.ReadFile("testdata/lint-input.yaml")
    19  	require.NoError(t, err, "could not read lint testdata")
    20  	m.Config.In = bytes.NewReader(input)
    21  
    22  	results, err := m.Lint(ctx)
    23  	require.NoError(t, err, "Lint failed")
    24  	assert.Len(t, results, 2, "Unexpected number of lint results generated")
    25  
    26  	var gotInstallError linter.Result
    27  	for _, r := range results {
    28  		if r.Location.Action == "install" && r.Code == CodeBashCArgMissingQuotes {
    29  			gotInstallError = r
    30  		}
    31  	}
    32  	wantInstallError := linter.Result{
    33  		Level: linter.LevelError,
    34  		Location: linter.Location{
    35  			Action:          "install",
    36  			Mixin:           "exec",
    37  			StepNumber:      2,
    38  			StepDescription: "Install Hello World",
    39  		},
    40  		Code:  CodeBashCArgMissingQuotes,
    41  		Title: "bash -c argument missing wrapping quotes",
    42  		Message: `The bash -c flag argument must be wrapped in quotes, for example
    43  exec:
    44    description: Say Hello
    45    command: bash
    46    flags:
    47      c: '"echo Hello World"'
    48  `,
    49  		URL: "https://porter.sh/best-practices/exec-mixin/#quoting-escaping-bash-and-yaml",
    50  	}
    51  	assert.Equal(t, wantInstallError, gotInstallError)
    52  }