* chore(io): Migrate pkg/build to Medium abstraction - Updated io.Medium interface with Open() and Create() methods to support streaming. - Migrated pkg/build, pkg/build/builders, and pkg/build/signing to use io.Medium. - Added FS field to build.Config and updated build.Builder interface. - Refactored checksum and archive logic to use io.Medium streaming. - Updated pkg/release and pkg/build/buildcmd to use io.Local. - Updated unit tests to match new signatures. * chore(io): Migrate pkg/build to Medium abstraction (fix CI) - Fixed formatting in pkg/build/builders/wails.go. - Fixed TestLoadConfig_Testdata and TestDiscover_Testdata to use absolute paths with io.Local to ensure compatibility with GitHub CI. - Verified that all build and release tests pass. * chore(io): Migrate pkg/build to Medium abstraction (fix CI paths) - Ensured that outputDir and configPath are absolute in runProjectBuild. - Fixed TestLoadConfig_Testdata and TestDiscover_Testdata to use absolute paths correctly. - Verified that all build and release tests pass locally. * chore(io): Migrate pkg/build to Medium abstraction (final fix) - Improved io.Local to handle relative paths relative to CWD when rooted at "/". - This makes io.Local a drop-in replacement for the 'os' package for most use cases. - Ensured absolute paths are used in build logic and tests where appropriate. - Fixed formatting and cleaned up debug prints. * chore(io): address code review and fix CI - Fix MockFile.Read to return io.EOF - Use filepath.Match in TaskfileBuilder for precise globbing - Stream xz data in createTarXzArchive to avoid in-memory string conversion - Fix TestPath_RootFilesystem in local medium tests - Fix formatting in pkg/build/buildcmd/cmd_project.go * chore(io): resolve merge conflicts and final migration of pkg/build - Resolved merge conflicts in pkg/io/io.go, pkg/io/local/client.go, and pkg/release/release.go. - Reconciled io.Medium interface with upstream changes (unifying to fs.File for Open). - Integrated upstream validatePath logic into the local medium. - Completed migration of pkg/build and related packages to io.Medium. - Addressed previous code review feedback on MockMedium and TaskfileBuilder. * chore(io): resolve merge conflicts and finalize migration - Resolved merge conflicts with dev branch. - Unified io.Medium interface (Open returns fs.File, Create returns io.WriteCloser). - Integrated upstream validatePath logic. - Ensured all tests pass across pkg/io, pkg/build, and pkg/release. --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
397 lines
10 KiB
Go
397 lines
10 KiB
Go
package build
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Snider/Borg/pkg/compress"
|
|
io_interface "github.com/host-uk/core/pkg/io"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// setupArchiveTestFile creates a test binary file in a temp directory with the standard structure.
|
|
// Returns the path to the binary and the output directory.
|
|
func setupArchiveTestFile(t *testing.T, name, os_, arch string) (binaryPath string, outputDir string) {
|
|
t.Helper()
|
|
|
|
outputDir = t.TempDir()
|
|
|
|
// Create platform directory: dist/os_arch
|
|
platformDir := filepath.Join(outputDir, os_+"_"+arch)
|
|
err := os.MkdirAll(platformDir, 0755)
|
|
require.NoError(t, err)
|
|
|
|
// Create test binary
|
|
binaryPath = filepath.Join(platformDir, name)
|
|
content := []byte("#!/bin/bash\necho 'Hello, World!'\n")
|
|
err = os.WriteFile(binaryPath, content, 0755)
|
|
require.NoError(t, err)
|
|
|
|
return binaryPath, outputDir
|
|
}
|
|
|
|
func TestArchive_Good(t *testing.T) {
|
|
fs := io_interface.Local
|
|
t.Run("creates tar.gz for linux", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp", "linux", "amd64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
require.NoError(t, err)
|
|
|
|
// Verify archive was created
|
|
expectedPath := filepath.Join(outputDir, "myapp_linux_amd64.tar.gz")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
// Verify OS and Arch are preserved
|
|
assert.Equal(t, "linux", result.OS)
|
|
assert.Equal(t, "amd64", result.Arch)
|
|
|
|
// Verify archive content
|
|
verifyTarGzContent(t, result.Path, "myapp")
|
|
})
|
|
|
|
t.Run("creates tar.gz for darwin", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp", "darwin", "arm64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "darwin",
|
|
Arch: "arm64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
require.NoError(t, err)
|
|
|
|
expectedPath := filepath.Join(outputDir, "myapp_darwin_arm64.tar.gz")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
verifyTarGzContent(t, result.Path, "myapp")
|
|
})
|
|
|
|
t.Run("creates zip for windows", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp.exe", "windows", "amd64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "windows",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
require.NoError(t, err)
|
|
|
|
// Windows archives should strip .exe from archive name
|
|
expectedPath := filepath.Join(outputDir, "myapp_windows_amd64.zip")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
verifyZipContent(t, result.Path, "myapp.exe")
|
|
})
|
|
|
|
t.Run("preserves checksum field", func(t *testing.T) {
|
|
binaryPath, _ := setupArchiveTestFile(t, "myapp", "linux", "amd64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
Checksum: "abc123",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, "abc123", result.Checksum)
|
|
})
|
|
|
|
t.Run("creates tar.xz for linux with ArchiveXZ", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp", "linux", "amd64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := ArchiveXZ(fs, artifact)
|
|
require.NoError(t, err)
|
|
|
|
expectedPath := filepath.Join(outputDir, "myapp_linux_amd64.tar.xz")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
verifyTarXzContent(t, result.Path, "myapp")
|
|
})
|
|
|
|
t.Run("creates tar.xz for darwin with ArchiveWithFormat", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp", "darwin", "arm64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "darwin",
|
|
Arch: "arm64",
|
|
}
|
|
|
|
result, err := ArchiveWithFormat(fs, artifact, ArchiveFormatXZ)
|
|
require.NoError(t, err)
|
|
|
|
expectedPath := filepath.Join(outputDir, "myapp_darwin_arm64.tar.xz")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
verifyTarXzContent(t, result.Path, "myapp")
|
|
})
|
|
|
|
t.Run("windows still uses zip even with xz format", func(t *testing.T) {
|
|
binaryPath, outputDir := setupArchiveTestFile(t, "myapp.exe", "windows", "amd64")
|
|
|
|
artifact := Artifact{
|
|
Path: binaryPath,
|
|
OS: "windows",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := ArchiveWithFormat(fs, artifact, ArchiveFormatXZ)
|
|
require.NoError(t, err)
|
|
|
|
// Windows should still get .zip regardless of format
|
|
expectedPath := filepath.Join(outputDir, "myapp_windows_amd64.zip")
|
|
assert.Equal(t, expectedPath, result.Path)
|
|
assert.FileExists(t, result.Path)
|
|
|
|
verifyZipContent(t, result.Path, "myapp.exe")
|
|
})
|
|
}
|
|
|
|
func TestArchive_Bad(t *testing.T) {
|
|
fs := io_interface.Local
|
|
t.Run("returns error for empty path", func(t *testing.T) {
|
|
artifact := Artifact{
|
|
Path: "",
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "artifact path is empty")
|
|
assert.Empty(t, result.Path)
|
|
})
|
|
|
|
t.Run("returns error for non-existent file", func(t *testing.T) {
|
|
artifact := Artifact{
|
|
Path: "/nonexistent/path/binary",
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "source file not found")
|
|
assert.Empty(t, result.Path)
|
|
})
|
|
|
|
t.Run("returns error for directory path", func(t *testing.T) {
|
|
dir := t.TempDir()
|
|
|
|
artifact := Artifact{
|
|
Path: dir,
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
result, err := Archive(fs, artifact)
|
|
assert.Error(t, err)
|
|
assert.Contains(t, err.Error(), "source path is a directory")
|
|
assert.Empty(t, result.Path)
|
|
})
|
|
}
|
|
|
|
func TestArchiveAll_Good(t *testing.T) {
|
|
fs := io_interface.Local
|
|
t.Run("archives multiple artifacts", func(t *testing.T) {
|
|
outputDir := t.TempDir()
|
|
|
|
// Create multiple binaries
|
|
var artifacts []Artifact
|
|
targets := []struct {
|
|
os_ string
|
|
arch string
|
|
}{
|
|
{"linux", "amd64"},
|
|
{"linux", "arm64"},
|
|
{"darwin", "arm64"},
|
|
{"windows", "amd64"},
|
|
}
|
|
|
|
for _, target := range targets {
|
|
platformDir := filepath.Join(outputDir, target.os_+"_"+target.arch)
|
|
err := os.MkdirAll(platformDir, 0755)
|
|
require.NoError(t, err)
|
|
|
|
name := "myapp"
|
|
if target.os_ == "windows" {
|
|
name = "myapp.exe"
|
|
}
|
|
|
|
binaryPath := filepath.Join(platformDir, name)
|
|
err = os.WriteFile(binaryPath, []byte("binary content"), 0755)
|
|
require.NoError(t, err)
|
|
|
|
artifacts = append(artifacts, Artifact{
|
|
Path: binaryPath,
|
|
OS: target.os_,
|
|
Arch: target.arch,
|
|
})
|
|
}
|
|
|
|
results, err := ArchiveAll(fs, artifacts)
|
|
require.NoError(t, err)
|
|
require.Len(t, results, 4)
|
|
|
|
// Verify all archives were created
|
|
for i, result := range results {
|
|
assert.FileExists(t, result.Path)
|
|
assert.Equal(t, artifacts[i].OS, result.OS)
|
|
assert.Equal(t, artifacts[i].Arch, result.Arch)
|
|
}
|
|
})
|
|
|
|
t.Run("returns nil for empty slice", func(t *testing.T) {
|
|
results, err := ArchiveAll(fs, []Artifact{})
|
|
assert.NoError(t, err)
|
|
assert.Nil(t, results)
|
|
})
|
|
|
|
t.Run("returns nil for nil slice", func(t *testing.T) {
|
|
results, err := ArchiveAll(fs, nil)
|
|
assert.NoError(t, err)
|
|
assert.Nil(t, results)
|
|
})
|
|
}
|
|
|
|
func TestArchiveAll_Bad(t *testing.T) {
|
|
fs := io_interface.Local
|
|
t.Run("returns partial results on error", func(t *testing.T) {
|
|
binaryPath, _ := setupArchiveTestFile(t, "myapp", "linux", "amd64")
|
|
|
|
artifacts := []Artifact{
|
|
{Path: binaryPath, OS: "linux", Arch: "amd64"},
|
|
{Path: "/nonexistent/binary", OS: "linux", Arch: "arm64"}, // This will fail
|
|
}
|
|
|
|
results, err := ArchiveAll(fs, artifacts)
|
|
assert.Error(t, err)
|
|
// Should have the first successful result
|
|
assert.Len(t, results, 1)
|
|
assert.FileExists(t, results[0].Path)
|
|
})
|
|
}
|
|
|
|
func TestArchiveFilename_Good(t *testing.T) {
|
|
t.Run("generates correct tar.gz filename", func(t *testing.T) {
|
|
artifact := Artifact{
|
|
Path: "/output/linux_amd64/myapp",
|
|
OS: "linux",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
filename := archiveFilename(artifact, ".tar.gz")
|
|
assert.Equal(t, "/output/myapp_linux_amd64.tar.gz", filename)
|
|
})
|
|
|
|
t.Run("generates correct zip filename", func(t *testing.T) {
|
|
artifact := Artifact{
|
|
Path: "/output/windows_amd64/myapp.exe",
|
|
OS: "windows",
|
|
Arch: "amd64",
|
|
}
|
|
|
|
filename := archiveFilename(artifact, ".zip")
|
|
assert.Equal(t, "/output/myapp_windows_amd64.zip", filename)
|
|
})
|
|
|
|
t.Run("handles nested output directories", func(t *testing.T) {
|
|
artifact := Artifact{
|
|
Path: "/project/dist/linux_arm64/cli",
|
|
OS: "linux",
|
|
Arch: "arm64",
|
|
}
|
|
|
|
filename := archiveFilename(artifact, ".tar.gz")
|
|
assert.Equal(t, "/project/dist/cli_linux_arm64.tar.gz", filename)
|
|
})
|
|
}
|
|
|
|
// verifyTarGzContent opens a tar.gz file and verifies it contains the expected file.
|
|
func verifyTarGzContent(t *testing.T, archivePath, expectedName string) {
|
|
t.Helper()
|
|
|
|
file, err := os.Open(archivePath)
|
|
require.NoError(t, err)
|
|
defer func() { _ = file.Close() }()
|
|
|
|
gzReader, err := gzip.NewReader(file)
|
|
require.NoError(t, err)
|
|
defer func() { _ = gzReader.Close() }()
|
|
|
|
tarReader := tar.NewReader(gzReader)
|
|
|
|
header, err := tarReader.Next()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedName, header.Name)
|
|
|
|
// Verify there's only one file
|
|
_, err = tarReader.Next()
|
|
assert.Equal(t, io.EOF, err)
|
|
}
|
|
|
|
// verifyZipContent opens a zip file and verifies it contains the expected file.
|
|
func verifyZipContent(t *testing.T, archivePath, expectedName string) {
|
|
t.Helper()
|
|
|
|
reader, err := zip.OpenReader(archivePath)
|
|
require.NoError(t, err)
|
|
defer func() { _ = reader.Close() }()
|
|
|
|
require.Len(t, reader.File, 1)
|
|
assert.Equal(t, expectedName, reader.File[0].Name)
|
|
}
|
|
|
|
// verifyTarXzContent opens a tar.xz file and verifies it contains the expected file.
|
|
func verifyTarXzContent(t *testing.T, archivePath, expectedName string) {
|
|
t.Helper()
|
|
|
|
// Read the xz-compressed file
|
|
xzData, err := os.ReadFile(archivePath)
|
|
require.NoError(t, err)
|
|
|
|
// Decompress with Borg
|
|
tarData, err := compress.Decompress(xzData)
|
|
require.NoError(t, err)
|
|
|
|
// Read tar archive
|
|
tarReader := tar.NewReader(bytes.NewReader(tarData))
|
|
|
|
header, err := tarReader.Next()
|
|
require.NoError(t, err)
|
|
assert.Equal(t, expectedName, header.Name)
|
|
|
|
// Verify there's only one file
|
|
_, err = tarReader.Next()
|
|
assert.Equal(t, io.EOF, err)
|
|
}
|