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

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"get.porter.sh/porter/pkg"
     9  	"get.porter.sh/porter/pkg/portercontext"
    10  	"get.porter.sh/porter/pkg/runtime"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  type TestFileOutput struct {
    16  	Name     string
    17  	FilePath string
    18  }
    19  
    20  func (o TestFileOutput) GetName() string {
    21  	return o.Name
    22  }
    23  
    24  func (o TestFileOutput) GetFilePath() string {
    25  	return o.FilePath
    26  }
    27  
    28  func TestFilePathOutputs(t *testing.T) {
    29  	ctx := context.Background()
    30  	c := runtime.NewTestRuntimeConfig(t)
    31  
    32  	step := TestStep{
    33  		Outputs: []Output{
    34  			TestFileOutput{Name: "config", FilePath: "config.txt"},
    35  		},
    36  	}
    37  
    38  	wantCfg := "abc123"
    39  	err := c.FileSystem.WriteFile("config.txt", []byte(wantCfg), pkg.FileModeWritable)
    40  	require.NoError(t, err, "could not write config.txt")
    41  
    42  	err = ProcessFileOutputs(ctx, c.RuntimeConfig, step)
    43  	require.NoError(t, err, "ProcessFileOutputs should not return an error")
    44  
    45  	f := filepath.Join(portercontext.MixinOutputsDir, "config")
    46  	gotOutput, err := c.FileSystem.ReadFile(f)
    47  	require.NoError(t, err, "could not read output file %s", f)
    48  
    49  	assert.Equal(t, wantCfg, string(gotOutput))
    50  }