Skip to content

Commit

Permalink
feat: support output vendor archive in server
Browse files Browse the repository at this point in the history
  • Loading branch information
kulti committed Nov 25, 2024
1 parent 70d8e7f commit b780ed6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
1 change: 1 addition & 0 deletions api/rockamalg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ message AmalgRequest {

message AmalgResponse {
bytes lua = 1;
bytes vendor = 2;
}
35 changes: 22 additions & 13 deletions internal/api/rockamalgrpc/rockamalg.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/archive/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func UnzipFileToFilesMap(zipFile string) (map[string][]byte, error) {
return unzipToFilesMap(&archive.Reader)
}

func UnzipBytesToFilesMap(data []byte) (map[string][]byte, error) {
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
return nil, fmt.Errorf("create zip reader: %w", err)
}
return unzipToFilesMap(r)
}

func unzipToFilesMap(archive *zip.Reader) (map[string][]byte, error) {
files := make(map[string][]byte, len(archive.File))
for _, f := range archive.File {
Expand Down
11 changes: 10 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -57,6 +58,7 @@ func (s *Server) Amalg(

amalgParams := rockamalg.AmalgParams{
Output: filepath.Join(amalgDir, "out.lua"),
Vendor: filepath.Join(amalgDir, "vendor.zip"),
Isolate: req.GetIsolate(),
DisableDebug: req.GetDisableDebug(),
AllowDevDeps: req.GetAllowDevDependencies(),
Expand Down Expand Up @@ -95,7 +97,14 @@ func (s *Server) Amalg(
return nil, status.Errorf(codes.Internal, "reading result output: %v", err)
}

return &rockamalgrpc.AmalgResponse{Lua: out}, nil
vendor, err := os.ReadFile(amalgParams.Vendor)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
return nil, status.Errorf(codes.Internal, "reading result vendor: %v", err)
}
}

return &rockamalgrpc.AmalgResponse{Lua: out, Vendor: vendor}, nil
}

func (s *Server) writeDependenciesFile(deps []string, path string) error {
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/amalg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,31 @@ func checkExpectedDirWithZippedFile(t *testing.T, expectedDirName, actualZipFile
}
}

func checkExpectedDirWithZippedBytes(t *testing.T, expectedDirName string, zipBytes []byte) {
t.Helper()

if update {
require.NoError(t, os.RemoveAll(expectedDirName))
if len(zipBytes) != 0 {
require.NoError(t, archive.UnzipBytesToDir(zipBytes, expectedDirName))
}
} else {
if len(zipBytes) == 0 {
require.False(t, isExist(t, expectedDirName), "empty vendor, but expected")
return
}

expected := dirToFilesMap(t, expectedDirName)
actual, err := archive.UnzipBytesToFilesMap(zipBytes)
require.NoError(t, err)

require.Equal(t, expected, actual)
if !reflect.DeepEqual(expected, actual) {
require.Fail(t, "vendor differs — run test with update to see differences")
}
}
}

func checkExpectedWithFile(t *testing.T, exepctedFileName, actualFileName string) {
t.Helper()

Expand Down
1 change: 1 addition & 0 deletions tests/integration/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func testServer(t *testing.T, testdataDir string, port int, rt rockstype) {
require.NoError(t, err)

checkExpectedWithBytes(t, testOpts.expectedLua, resp.GetLua())
checkExpectedDirWithZippedBytes(t, testOpts.expectedVendor, resp.GetVendor())

stdoutBytes := execDockerCommand(t, testOpts.luaExecArgs...)
checkExpectedWithBytes(t, testOpts.expectedLuaExec, stdoutBytes)
Expand Down

0 comments on commit b780ed6

Please sign in to comment.