Translate artifact paths to local absolute directory path

This commit is contained in:
Pieter Noordhuis 2025-01-13 14:51:54 +01:00
parent 6856a3916e
commit de011c4018
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
3 changed files with 30 additions and 5 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt"
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
"strings"
@ -34,6 +35,10 @@ const (
// It returns an error if the path does not exist or is a directory.
TranslateModeLocalAbsoluteFile
// TranslateModeLocalAbsoluteDirectory translates a path to the local absolute directory path.
// It returns an error if the path does not exist or is not a directory.
TranslateModeLocalAbsoluteDirectory
// TranslateModeLocalRelative translates a path to be relative to the bundle sync root path.
// It does not check if the path exists, nor care if it is a file or directory.
TranslateModeLocalRelative
@ -157,6 +162,8 @@ func (t *translateContext) rewritePath(
interp, err = t.translateDirectoryPath(ctx, input, localPath, localRelPath)
case TranslateModeLocalAbsoluteFile:
interp, err = t.translateLocalAbsoluteFilePath(ctx, input, localPath, localRelPath)
case TranslateModeLocalAbsoluteDirectory:
interp, err = t.translateLocalAbsoluteDirectoryPath(ctx, input, localPath, localRelPath)
case TranslateModeLocalRelative:
interp, err = t.translateLocalRelativePath(ctx, input, localPath, localRelPath)
case TranslateModeLocalRelativeWithPrefix:
@ -254,6 +261,20 @@ func (t *translateContext) translateLocalAbsoluteFilePath(ctx context.Context, l
return localFullPath, nil
}
func (t *translateContext) translateLocalAbsoluteDirectoryPath(ctx context.Context, literal, localFullPath, _ string) (string, error) {
info, err := os.Stat(localFullPath)
if errors.Is(err, fs.ErrNotExist) {
return "", fmt.Errorf("directory %s not found", literal)
}
if err != nil {
return "", fmt.Errorf("unable to determine if %s is a directory: %w", localFullPath, err)
}
if !info.IsDir() {
return "", fmt.Errorf("expected %s to be a directory but found a file", literal)
}
return localFullPath, nil
}
func (t *translateContext) translateLocalRelativePath(ctx context.Context, literal, localFullPath, localRelPath string) (string, error) {
return localRelPath, nil
}

View File

@ -24,7 +24,7 @@ func (t *translateContext) artifactRewritePatterns() []artifactRewritePattern {
{
base.Append(dyn.Key("path")),
translateOptions{
Mode: TranslateModeLocalRelative,
Mode: TranslateModeLocalAbsoluteDirectory,
// Artifact paths may be outside the sync root.
// They are the working directory for artifact builds.

View File

@ -19,6 +19,8 @@ import (
func TestTranslatePathsArtifacts_InsideSyncRoot(t *testing.T) {
tmp := t.TempDir()
dir := filepath.Join(tmp, "bundle")
lib := filepath.Join(dir, "my_lib")
_ = os.MkdirAll(lib, 0o755)
_ = os.MkdirAll(dir, 0o755)
b := &bundle.Bundle{
@ -44,12 +46,14 @@ func TestTranslatePathsArtifacts_InsideSyncRoot(t *testing.T) {
require.NoError(t, diags.Error())
// Assert that the artifact path has been converted to a path relative to the sync root.
assert.Equal(t, "my_lib", b.Config.Artifacts["my_artifact"].Path)
assert.Equal(t, lib, b.Config.Artifacts["my_artifact"].Path)
}
func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
tmp := t.TempDir()
lib := filepath.Join(tmp, "my_lib")
dir := filepath.Join(tmp, "bundle")
_ = os.MkdirAll(lib, 0o755)
_ = os.MkdirAll(dir, 0o755)
b := &bundle.Bundle{
@ -60,8 +64,8 @@ func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
"my_artifact": {
Type: "wheel",
// Assume this is defined in a subdir to the sync root.
Path: "../../common/my_lib",
// Assume this is defined in a subdir of the bundle root.
Path: "../../my_lib",
},
},
},
@ -75,5 +79,5 @@ func TestTranslatePathsArtifacts_OutsideSyncRoot(t *testing.T) {
require.NoError(t, diags.Error())
// Assert that the artifact path has been converted to a path relative to the sync root.
assert.Equal(t, "../common/my_lib", b.Config.Artifacts["my_artifact"].Path)
assert.Equal(t, lib, b.Config.Artifacts["my_artifact"].Path)
}