github.com/nektos/act@v0.2.63/pkg/container/host_environment_test.go (about)

     1  package container
     2  
     3  import (
     4  	"archive/tar"
     5  	"context"
     6  	"io"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  // Type assert HostEnvironment implements ExecutionsEnvironment
    16  var _ ExecutionsEnvironment = &HostEnvironment{}
    17  
    18  func TestCopyDir(t *testing.T) {
    19  	dir, err := os.MkdirTemp("", "test-host-env-*")
    20  	assert.NoError(t, err)
    21  	defer os.RemoveAll(dir)
    22  	ctx := context.Background()
    23  	e := &HostEnvironment{
    24  		Path:      filepath.Join(dir, "path"),
    25  		TmpDir:    filepath.Join(dir, "tmp"),
    26  		ToolCache: filepath.Join(dir, "tool_cache"),
    27  		ActPath:   filepath.Join(dir, "act_path"),
    28  		StdOut:    os.Stdout,
    29  		Workdir:   path.Join("testdata", "scratch"),
    30  	}
    31  	_ = os.MkdirAll(e.Path, 0700)
    32  	_ = os.MkdirAll(e.TmpDir, 0700)
    33  	_ = os.MkdirAll(e.ToolCache, 0700)
    34  	_ = os.MkdirAll(e.ActPath, 0700)
    35  	err = e.CopyDir(e.Workdir, e.Path, true)(ctx)
    36  	assert.NoError(t, err)
    37  }
    38  
    39  func TestGetContainerArchive(t *testing.T) {
    40  	dir, err := os.MkdirTemp("", "test-host-env-*")
    41  	assert.NoError(t, err)
    42  	defer os.RemoveAll(dir)
    43  	ctx := context.Background()
    44  	e := &HostEnvironment{
    45  		Path:      filepath.Join(dir, "path"),
    46  		TmpDir:    filepath.Join(dir, "tmp"),
    47  		ToolCache: filepath.Join(dir, "tool_cache"),
    48  		ActPath:   filepath.Join(dir, "act_path"),
    49  		StdOut:    os.Stdout,
    50  		Workdir:   path.Join("testdata", "scratch"),
    51  	}
    52  	_ = os.MkdirAll(e.Path, 0700)
    53  	_ = os.MkdirAll(e.TmpDir, 0700)
    54  	_ = os.MkdirAll(e.ToolCache, 0700)
    55  	_ = os.MkdirAll(e.ActPath, 0700)
    56  	expectedContent := []byte("sdde/7sh")
    57  	err = os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0600)
    58  	assert.NoError(t, err)
    59  	archive, err := e.GetContainerArchive(ctx, e.Path)
    60  	assert.NoError(t, err)
    61  	defer archive.Close()
    62  	reader := tar.NewReader(archive)
    63  	h, err := reader.Next()
    64  	assert.NoError(t, err)
    65  	assert.Equal(t, "action.yml", h.Name)
    66  	content, err := io.ReadAll(reader)
    67  	assert.NoError(t, err)
    68  	assert.Equal(t, expectedContent, content)
    69  	_, err = reader.Next()
    70  	assert.ErrorIs(t, err, io.EOF)
    71  }