Skip to content

Commit

Permalink
Test : Included tests for build.go (#351)
Browse files Browse the repository at this point in the history
- Included tests for build.go

Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan authored Jun 22, 2022
1 parent 668a339 commit 46d87be
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
121 changes: 121 additions & 0 deletions internal/builders/go/pkg/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package pkg
import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -1047,3 +1048,123 @@ func Test_generateCommand(t *testing.T) {
func asPointer(s string) *string {
return &s
}

func TestGoBuild_Run(t *testing.T) {
type fields struct {
cfg *GoReleaserConfig
goc string
argEnv map[string]string
ldflags string
}
type args struct {
dry bool
}
tests := []struct {
name string
fields fields
args args
wantErr bool
err error
}{
{
name: "dry run valid flags",
fields: fields{
cfg: &GoReleaserConfig{
Goos: "linux",
Goarch: "amd64",
Binary: "binary",
Main: asPointer("../builders/go/main.go"),
Dir: asPointer("../builders/go"),
Ldflags: []string{
"-X main.version=1.0.0",
},
},
},
args: args{
dry: true,
},
},
{
name: "non-dry valid flags",
fields: fields{
cfg: &GoReleaserConfig{
Goos: "linux",
Goarch: "amd64",
Binary: "/tmp/binary",
Main: asPointer("main.go"),
Dir: asPointer("./testdata/go"),
Ldflags: []string{
"-X main.version=1.0.0",
},
},
},
args: args{
dry: false,
},
},
{
name: "slash in the binary name",
fields: fields{
cfg: &GoReleaserConfig{
Goos: "linux",
Goarch: "amd64",
Binary: "tmp/binary",
Main: asPointer("../builders/go/main.go"),
Dir: asPointer("../builders/go"),
},
},
args: args{
dry: true,
},
wantErr: true,
err: errorInvalidFilename,
},
{
name: "dry run - invalid flags",
fields: fields{
cfg: &GoReleaserConfig{
Goos: "linux",
Goarch: "amd64",
Binary: "binary",
Main: asPointer("../builders/go/main.go"),
Dir: asPointer("../builders/go"),
Ldflags: []string{},
},
},
args: args{
dry: true,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &GoBuild{
cfg: tt.fields.cfg,
goc: tt.fields.goc,
argEnv: tt.fields.argEnv,
ldflags: tt.fields.ldflags,
}
t.Setenv("OUTPUT_BINARY", tt.fields.cfg.Binary)
// if the test is not dry run , then code has to look for golang binary
if !tt.args.dry {
path, err := exec.LookPath("go")
if err != nil {
t.Errorf("exec.LookPath: %v", err)
}
b.goc = path
}
err := b.Run(tt.args.dry)
if err != nil != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
if tt.err != nil {
if err == nil {
t.Errorf("Run() error = nil, wantErr %v", tt.err)
} else if errCmp(err, tt.err) {
t.Errorf("Run() error = %v, wantErr %v %v", err, tt.err, cmp.Diff(err, tt.err))
}
}
})
}
}
3 changes: 3 additions & 0 deletions internal/builders/go/pkg/testdata/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/slsa-framework/slsa-github-generator/internal/builders/go/pkg/testdata/go

go 1.18
7 changes: 7 additions & 0 deletions internal/builders/go/pkg/testdata/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "fmt"

func main() {
fmt.Println("Hello world!")
}

0 comments on commit 46d87be

Please sign in to comment.