Skip to content

Commit

Permalink
internal/task: change buildVSCGO to produce zip files
Browse files Browse the repository at this point in the history
Signing service expects zip as input. Pack the binary in a zip file.
The file name is <randomnumber>-<platform>-vscgo.zip.

For golang/vscode-go#3186

Change-Id: I7d91dfbd2b5f0c4386421c2abfe77df254b392fb
Reviewed-on: https://go-review.googlesource.com/c/build/+/587735
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
  • Loading branch information
hyangah committed May 24, 2024
1 parent 3a748bd commit d152727
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
19 changes: 17 additions & 2 deletions internal/task/vscodego.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package task

import (
"archive/zip"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -124,6 +125,9 @@ func (t *VSCodeGoReleaseTask) buildVSCGO(ctx *wf.TaskContext, version string) ([
})
return nil
})
if err != nil {
return nil, err
}

var eg errgroup.Group
for i := range artifacts {
Expand All @@ -136,14 +140,25 @@ func (t *VSCodeGoReleaseTask) buildVSCGO(ctx *wf.TaskContext, version string) ([
return err
}
defer in.Close()
name, out, err := t.ScratchFS.OpenWrite(ctx, platform+"-"+filepath.Base(path))
name, out, err := t.ScratchFS.OpenWrite(ctx, platform+"-vscgo.zip")
if err != nil {
out.Close()
return err
}
// write as zip file.
zw := zip.NewWriter(out)
f, err := zw.Create(filepath.Base(path))
if err != nil {
out.Close()
return err
}
if _, err := io.Copy(out, in); err != nil {
if _, err := io.Copy(f, in); err != nil {
out.Close()
return err
}
if err := zw.Close(); err != nil {
return err
}
if err := out.Close(); err != nil {
return err
}
Expand Down
59 changes: 54 additions & 5 deletions internal/task/vscodego_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
package task

import (
"archive/zip"
"bytes"
"context"
"os"
"fmt"
"io"
"path/filepath"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -126,28 +129,74 @@ esac
Platform: p.Platform,
})
}
sortSlice := cmpopts.SortSlices(func(x, y goBuildArtifact) bool { return x.Platform < y.Platform })
sortSlice := cmpopts.SortSlices(
func(x, y goBuildArtifact) bool { return x.Platform < y.Platform },
)
ignoreFilename := cmpopts.IgnoreFields(goBuildArtifact{}, "Filename")
if diff := cmp.Diff(buildArtifacts, want, sortSlice, ignoreFilename); diff != "" {
t.Fatal(diff)
}
// HACK: Reading directly from the scratch filesystem.
// XXX: is there a better way??
envMap := make(map[string][]string)
for _, p := range vscgoPlatforms {
envMap[p.Platform] = p.Env
}
re := regexp.MustCompile(`-([a-z0-9]+-[a-z0-9]+)-vscgo\.zip$`)
for _, a := range buildArtifacts {
data, err := os.ReadFile(filepath.Join(scratchDir, w.ID.String(), a.Filename))
m := re.FindStringSubmatch(a.Filename)
if m == nil {
t.Errorf(
"artifact file with unexpected file name: %q, want <platform>-<arch>-vscgo.zip",
a.Filename,
)
continue
}
platform := m[1]
if platform != a.Platform {
t.Errorf(
"artifact file with unexpected platform: %q, want %q",
a.Platform,
platform,
)
continue
}
// The output files are zip files.
executable := "vscgo"
if strings.HasPrefix(platform, "win32") {
executable = "vscgo.exe"
}
data, err := extractFileZip(t, filepath.Join(scratchDir, w.ID.String(), a.Filename), executable)
if err != nil {
t.Errorf("%v: %v", a.Platform, err)
}
envs := envMap[a.Platform]
if !bytes.Contains(data, []byte(strings.Join(envs, " "))) {
t.Errorf("%v: unexpected contents: %s", a.Platform, data)
}

}
})
}
}

func extractFileZip(t *testing.T, zipfile, fileToExtract string) ([]byte, error) {
r, err := zip.OpenReader(zipfile)
if err != nil {
return nil, err
}
defer r.Close()

// Iterate through the files in the archive,
// return the contents of the requested file.
for _, f := range r.File {
if f.Name != fileToExtract {
t.Errorf("unexpected file in zip: %q", f.Name)
continue
}
rc, err := f.Open()
if err != nil {
return nil, err
}
return io.ReadAll(rc)
}
return nil, fmt.Errorf("file %q not found", fileToExtract)
}

0 comments on commit d152727

Please sign in to comment.