get.porter.sh/porter@v1.3.0/pkg/mixin/helpers.go (about)

     1  package mixin
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"fmt"
     8  	"os"
     9  
    10  	"get.porter.sh/porter/pkg/pkgmgmt"
    11  	"get.porter.sh/porter/pkg/pkgmgmt/client"
    12  	"get.porter.sh/porter/pkg/portercontext"
    13  	"github.com/Masterminds/semver/v3"
    14  )
    15  
    16  type TestMixinProvider struct {
    17  	client.TestPackageManager
    18  
    19  	// LintResults allows you to provide linter.Results for your unit tests.
    20  	// It isn't of type linter.Results directly to avoid package cycles
    21  	LintResults interface{}
    22  
    23  	// ReturnBuildError will force the TestMixinProvider to return a build error
    24  	// if set to true
    25  	ReturnBuildError bool
    26  }
    27  
    28  // hoist these into variables so tests can reference them safely
    29  var ExampleMixinName = "testmixin"
    30  var ExampleMixinSemver = semver.New(0, 1, 0, "", "")
    31  
    32  // NewTestMixinProvider helps us test Porter.Mixins in our unit tests without actually hitting any real plugins on the file system.
    33  func NewTestMixinProvider() *TestMixinProvider {
    34  	packages := []pkgmgmt.PackageMetadata{
    35  		&Metadata{
    36  			Name: "exec",
    37  			VersionInfo: pkgmgmt.VersionInfo{
    38  				Version: "v1.0",
    39  				Commit:  "abc123",
    40  				Author:  "Porter Authors",
    41  			},
    42  		},
    43  		&Metadata{
    44  			Name: ExampleMixinName,
    45  			VersionInfo: pkgmgmt.VersionInfo{
    46  				Version: fmt.Sprintf("v%s", ExampleMixinSemver.String()),
    47  				Commit:  "abc123",
    48  				Author:  "Porter Authors",
    49  			},
    50  		},
    51  	}
    52  
    53  	provider := TestMixinProvider{
    54  		TestPackageManager: client.TestPackageManager{
    55  			PkgType:  "mixins",
    56  			Packages: packages,
    57  		},
    58  	}
    59  
    60  	provider.RunAssertions = []func(pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error{
    61  		provider.PrintMixinOutput,
    62  	}
    63  
    64  	return &provider
    65  }
    66  
    67  func (p *TestMixinProvider) PrintMixinOutput(pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error {
    68  	switch commandOpts.Command {
    69  	case "build":
    70  		if p.ReturnBuildError {
    71  			return errors.New("encountered build error")
    72  		}
    73  		fmt.Fprintf(pkgContext.Out, "# %s mixin has no buildtime dependencies\n", name)
    74  	case "lint":
    75  		b, _ := json.Marshal(p.LintResults)
    76  		fmt.Fprintln(pkgContext.Out, string(b))
    77  	}
    78  	return nil
    79  }
    80  
    81  func (p *TestMixinProvider) GetSchema(ctx context.Context, name string) (string, error) {
    82  	var schemaFile string
    83  	switch name {
    84  	case "exec":
    85  		schemaFile = "../exec/schema/exec.json"
    86  	case ExampleMixinName:
    87  		schemaFile = "../../cmd/testmixin/schema.json"
    88  	default:
    89  		return "", nil
    90  	}
    91  	b, err := os.ReadFile(schemaFile)
    92  	return string(b), err
    93  }