Report only fully hashed build artifacts
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -588,6 +589,65 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCommandReportsOnlyHashedArtifactsAfterReadFailure(t *testing.T) {
|
||||
repository := newCLIBuildRepository(t, "package main\nfunc main() {}\n")
|
||||
secondArch := "arm64"
|
||||
if runtime.GOARCH == secondArch {
|
||||
secondArch = "amd64"
|
||||
}
|
||||
settings := cli.Settings{Product: "Example Product", Build: &cli.BuildSettings{MainPackage: "./cmd/app", BinaryName: "fixture", Targets: []string{runtime.GOOS + "/" + runtime.GOARCH, runtime.GOOS + "/" + secondArch}}}
|
||||
profile, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeRepositoryFile(t, repository, "releasekit.json", string(profile))
|
||||
realGo, err := exec.LookPath("go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bin := t.TempDir()
|
||||
script := `#!/bin/sh
|
||||
if [ "$1" != build ]; then exec "$RELEASEKIT_REAL_GO" "$@"; fi
|
||||
output=
|
||||
previous=
|
||||
for arg in "$@"; do
|
||||
if [ "$previous" = -o ]; then output=$arg; fi
|
||||
previous=$arg
|
||||
done
|
||||
"$RELEASEKIT_REAL_GO" "$@" || exit $?
|
||||
if [ -f "$RELEASEKIT_BUILD_COUNT" ]; then
|
||||
rm "$output"
|
||||
mkdir "$output"
|
||||
else
|
||||
: > "$RELEASEKIT_BUILD_COUNT"
|
||||
fi
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(bin, "go"), []byte(script), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
|
||||
t.Setenv("RELEASEKIT_REAL_GO", realGo)
|
||||
t.Setenv("RELEASEKIT_BUILD_COUNT", filepath.Join(t.TempDir(), "count"))
|
||||
var stdout, stderr bytes.Buffer
|
||||
status := run(context.Background(), []string{"build", "--repo", repository, "v1.2.3"}, &stdout, &stderr)
|
||||
first := fmt.Sprintf("fixture-v1.2.3-%s-%s", runtime.GOOS, runtime.GOARCH)
|
||||
second := fmt.Sprintf("fixture-v1.2.3-%s-%s", runtime.GOOS, secondArch)
|
||||
if runtime.GOOS == "windows" {
|
||||
first, second = first+".exe", second+".exe"
|
||||
}
|
||||
content, err := os.ReadFile(filepath.Join(repository, "dist", first))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
digest := sha256.Sum256(content)
|
||||
if status != 1 || stdout.Len() != 0 || !strings.Contains(stderr.String(), fmt.Sprintf("built %s %x", first, digest)) || strings.Contains(stderr.String(), "built "+second) || !strings.Contains(stderr.String(), "hash build output") || !strings.Contains(stderr.String(), second) {
|
||||
t.Fatalf("status = %d, stdout = %q, stderr = %q", status, stdout.String(), stderr.String())
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(repository, "dist", cli.BuildReceiptName)); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("partial build left receipt: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildCommandLeavesNoReceiptAfterSmokeFailure(t *testing.T) {
|
||||
repository := newCLIBuildRepository(t, `package main
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user