Consolidate artifact declarations
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/artifact"
|
||||
internalbuild "gitea.maximumdirect.net/eric/ReleaseKit/internal/build"
|
||||
"gitea.maximumdirect.net/eric/ReleaseKit/internal/gitea"
|
||||
)
|
||||
@@ -23,30 +23,26 @@ func declaredBuildAssets(result internalbuild.Result) ([]gitea.ExpectedAsset, er
|
||||
if len(result.Artifacts) != len(result.Targets) {
|
||||
return nil, fmt.Errorf("build result must contain one artifact for each selected target")
|
||||
}
|
||||
wantByTarget := make(map[string]string, len(result.Targets))
|
||||
for _, target := range result.Targets {
|
||||
if target.OS == "" || target.Arch == "" || strings.ContainsAny(target.OS+target.Arch, "/\\\r\n") {
|
||||
declarations := artifact.ExpectedBuild(result.BinaryName, result.Version, result.Targets)
|
||||
wantByTarget := make(map[artifact.Target]string, len(declarations))
|
||||
for _, declaration := range declarations {
|
||||
target := declaration.Target
|
||||
if !artifact.ValidComponent(target.OS) || !artifact.ValidComponent(target.Arch) {
|
||||
return nil, fmt.Errorf("build result contains an invalid target")
|
||||
}
|
||||
key := target.OS + "/" + target.Arch
|
||||
if _, exists := wantByTarget[key]; exists {
|
||||
return nil, fmt.Errorf("build result repeats target %q", key)
|
||||
if _, exists := wantByTarget[target]; exists {
|
||||
return nil, fmt.Errorf("build result repeats target %q", target.OS+"/"+target.Arch)
|
||||
}
|
||||
name := result.BinaryName + "-" + result.Version + "-" + target.OS + "-" + target.Arch
|
||||
if target.OS == "windows" {
|
||||
name += ".exe"
|
||||
}
|
||||
wantByTarget[key] = name
|
||||
wantByTarget[target] = declaration.Filename
|
||||
}
|
||||
assets := make([]gitea.ExpectedAsset, 0, len(result.Artifacts)+1)
|
||||
seenTargets := make(map[string]bool, len(result.Artifacts))
|
||||
seenTargets := make(map[artifact.Target]bool, len(result.Artifacts))
|
||||
for _, artifact := range result.Artifacts {
|
||||
key := artifact.Target.OS + "/" + artifact.Target.Arch
|
||||
wantName, ok := wantByTarget[key]
|
||||
if !ok || seenTargets[key] || artifact.Filename != wantName {
|
||||
wantName, ok := wantByTarget[artifact.Target]
|
||||
if !ok || seenTargets[artifact.Target] || artifact.Filename != wantName {
|
||||
return nil, fmt.Errorf("build artifact %q does not match a selected target and its declared name", artifact.Filename)
|
||||
}
|
||||
seenTargets[key] = true
|
||||
seenTargets[artifact.Target] = true
|
||||
if filepath.Base(artifact.Path) != artifact.Filename {
|
||||
return nil, fmt.Errorf("build artifact path %q does not end with filename %q", artifact.Path, artifact.Filename)
|
||||
}
|
||||
|
||||
@@ -61,6 +61,23 @@ func TestDeclaredBuildAssetsRejectsTamperedArtifact(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclaredBuildAssetsRejectsArtifactDeclarationMismatch(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "app-v1.2.3-linux-arm64")
|
||||
contents := []byte("built application")
|
||||
if err := os.WriteFile(path, contents, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := declaredBuildAssets(internalbuild.Result{
|
||||
Version: "v1.2.3", BinaryName: "app", Targets: []internalbuild.Target{{OS: "linux", Arch: "amd64"}},
|
||||
Artifacts: []internalbuild.Artifact{{
|
||||
Path: path, Filename: filepath.Base(path), Target: internalbuild.Target{OS: "linux", Arch: "amd64"}, SHA256: assetDigest(contents),
|
||||
}},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "does not match a selected target") {
|
||||
t.Fatalf("declaredBuildAssets() error = %v, want declaration mismatch", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeclaredBuildAssetsKeepsSourceOnlyBuildManifestFree(t *testing.T) {
|
||||
assets, err := declaredBuildAssets(internalbuild.Result{})
|
||||
if err != nil || len(assets) != 0 {
|
||||
|
||||
Reference in New Issue
Block a user