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

     1  package porter
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"get.porter.sh/porter/pkg/mixin"
     9  	"get.porter.sh/porter/pkg/pkgmgmt"
    10  	"get.porter.sh/porter/pkg/printer"
    11  	"get.porter.sh/porter/pkg/test"
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  )
    15  
    16  func TestPorter_PrintMixins(t *testing.T) {
    17  	ctx := context.Background()
    18  	p := NewTestPorter(t)
    19  	defer p.Close()
    20  
    21  	opts := PrintMixinsOptions{
    22  		PrintOptions: printer.PrintOptions{
    23  			Format: printer.FormatPlaintext,
    24  		},
    25  	}
    26  	err := p.PrintMixins(ctx, opts)
    27  
    28  	require.Nil(t, err)
    29  	gotOutput := p.TestConfig.TestContext.GetOutput()
    30  	test.CompareGoldenFile(t, "mixins/list-output.txt", gotOutput)
    31  }
    32  
    33  func TestPorter_InstallMixin(t *testing.T) {
    34  	p := NewTestPorter(t)
    35  	defer p.Close()
    36  
    37  	opts := mixin.InstallOptions{}
    38  	opts.Name = "exec"
    39  	opts.URL = "https://example.com"
    40  
    41  	err := p.InstallMixin(context.Background(), opts)
    42  
    43  	require.NoError(t, err)
    44  
    45  	wantOutput := "installed exec mixin v1.0 (abc123)\n"
    46  	gotOutput := p.TestConfig.TestContext.GetOutput()
    47  	assert.Contains(t, wantOutput, gotOutput)
    48  }
    49  
    50  func TestPorter_UninstallMixin(t *testing.T) {
    51  	ctx := context.Background()
    52  	p := NewTestPorter(t)
    53  	defer p.Close()
    54  
    55  	opts := pkgmgmt.UninstallOptions{}
    56  	err := opts.Validate([]string{"exec"})
    57  	require.NoError(t, err, "Validate failed")
    58  
    59  	err = p.UninstallMixin(ctx, opts)
    60  	require.NoError(t, err, "UninstallMixin failed")
    61  
    62  	wantOutput := "Uninstalled exec mixin"
    63  	gotoutput := p.TestConfig.TestContext.GetOutput()
    64  	assert.Contains(t, wantOutput, gotoutput)
    65  }
    66  
    67  func TestPorter_CreateMixin(t *testing.T) {
    68  	p := NewTestPorter(t)
    69  
    70  	tempDir, err := p.FileSystem.TempDir("", "porter")
    71  	require.NoError(t, err)
    72  
    73  	defer os.RemoveAll(tempDir)
    74  
    75  	opts := MixinsCreateOptions{
    76  		MixinName:      "MyMixin",
    77  		AuthorName:     "Author Name",
    78  		AuthorUsername: "username",
    79  		DirPath:        tempDir,
    80  	}
    81  
    82  	err = p.CreateMixin(opts)
    83  	require.NoError(t, err)
    84  
    85  	wantOutput := "Created MyMixin mixin\n"
    86  	gotOutput := p.TestConfig.TestContext.GetOutput()
    87  	assert.Contains(t, wantOutput, gotOutput)
    88  }