Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentsimon committed Jul 21, 2022
1 parent c008697 commit da442c6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
6 changes: 2 additions & 4 deletions internal/builders/go/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
Expand Down Expand Up @@ -267,7 +266,6 @@ func Test_runBuild(t *testing.T) {
s := r.end()

if !errCmp(err, tt.err) {
fmt.Println(err, tt.err)
t.Errorf(cmp.Diff(err, tt.err, cmpopts.EquateErrors()))
}

Expand Down Expand Up @@ -389,12 +387,12 @@ func extract(lines string) ([]string, []string, string, string, error) {
return []string{}, []string{}, "", "", err
}

cmd, err := utils.UnmarshallList(scmd)
cmd, err := utils.UnmarshalList(scmd)
if err != nil {
return []string{}, []string{}, "", "", err
}

env, err := utils.UnmarshallList(senv)
env, err := utils.UnmarshalList(senv)
if err != nil {
return []string{}, []string{}, "", "", err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/builders/go/pkg/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (b *GoBuild) Run(dry bool) error {

// Share the resolved name of the binary.
fmt.Printf("::set-output name=go-binary-name::%s\n", filename)
command, err := utils.MarshallToString(com)
command, err := utils.MarshalToString(com)
if err != nil {
return err
}
Expand All @@ -131,7 +131,7 @@ func (b *GoBuild) Run(dry bool) error {
return err
}

menv, err := utils.MarshallToString(env)
menv, err := utils.MarshalToString(env)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/builders/go/pkg/provenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin
return nil, fmt.Errorf("sha256 digest is not valid: %s", digest)
}

com, err := utils.UnmarshallList(command)
com, err := utils.UnmarshalList(command)
if err != nil {
return nil, err
}

env, err := utils.UnmarshallList(envs)
env, err := utils.UnmarshalList(envs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func GenerateProvenance(name, digest, command, envs, workingDir string, s signin

if utils.IsPresubmitTests() {
fmt.Println("Pre-submit tests detected. Skipping signing.")
return utils.MarshallToBytes(*p)
return utils.MarshalToBytes(*p)
}

// Sign the provenance.
Expand Down
13 changes: 8 additions & 5 deletions internal/utils/marshall.go → internal/utils/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import (
"fmt"
)

func UnmarshallList(arg string) ([]string, error) {
// UnmarshalList unmarshals a string into a list of strings.
func UnmarshalList(arg string) ([]string, error) {
var res []string
// If argument is empty, return an empty list early,
// because `json.Unmarshal` would fail.
Expand All @@ -39,7 +40,8 @@ func UnmarshallList(arg string) ([]string, error) {
return res, nil
}

func MarshallToString(args interface{}) (string, error) {
// MarshalToString marshals to a string.
func MarshalToString(args interface{}) (string, error) {
jsonData, err := json.Marshal(args)
if err != nil {
return "", fmt.Errorf("json.Marshal: %w", err)
Expand All @@ -52,10 +54,11 @@ func MarshallToString(args interface{}) (string, error) {
return encoded, nil
}

func MarshallToBytes(args interface{}) ([]byte, error) {
encoded, err := MarshallToString(args)
// MarshalToBytes marshals to a byte array.
func MarshalToBytes(args interface{}) ([]byte, error) {
encoded, err := MarshalToString(args)
if err != nil {
return []byte{}, nil
return nil, err
}
return []byte(encoded), nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/google/go-cmp/cmp"
)

func Test_MarshallToString(t *testing.T) {
func Test_MarshalToString(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -49,9 +49,9 @@ func Test_MarshallToString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

r, err := MarshallToString(tt.variables)
r, err := MarshalToString(tt.variables)
if err != nil {
t.Errorf("marshallToString: %v", err)
t.Errorf("MarshalToString: %v", err)
}
if !cmp.Equal(r, tt.expected) {
t.Errorf(cmp.Diff(r, tt.expected))
Expand All @@ -60,7 +60,7 @@ func Test_MarshallToString(t *testing.T) {
}
}

func Test_UnmarshallList(t *testing.T) {
func Test_UnmarshalList(t *testing.T) {
t.Parallel()

tests := []struct {
Expand Down Expand Up @@ -93,9 +93,9 @@ func Test_UnmarshallList(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

r, err := UnmarshallList(tt.value)
r, err := UnmarshalList(tt.value)
if err != nil && len(tt.expected) != 0 {
t.Errorf("UnmarshallList: %v", err)
t.Errorf("UnmarshalList: %v", err)
}

if !cmp.Equal(r, tt.expected) {
Expand Down

0 comments on commit da442c6

Please sign in to comment.