blob: 354b177e69207eea8c3e949735725466349d9d92 [file] [log] [blame]
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildbinary
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
const (
localTestData = "../testdata"
)
func less(a, b string) bool {
return a < b
}
func TestFindBinaries(t *testing.T) {
tests := []struct {
name string
dir string
want []string
wantErr bool
}{
{
name: "local test",
dir: filepath.Join(localTestData, "module"),
want: []string{"golang.org/vuln"},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := findBinaries(tt.dir)
if (err != nil) != tt.wantErr {
t.Errorf("findBinaries() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got, cmpopts.SortSlices(less)); diff != "" {
t.Errorf("mismatch (-want, +got):%s", diff)
}
})
}
}
func TestRunBuild(t *testing.T) {
if testing.Short() {
t.Skip("skipping test that uses internet in short mode")
}
tests := []struct {
name string
modulePath string
importPath string
want string
wantErr bool
}{
{
name: "local test",
modulePath: filepath.Join(localTestData, "module"),
importPath: "golang.org/vuln",
want: filepath.Join(localTestData, "module", "bin1"),
},
{
name: "multiple binaries",
modulePath: filepath.Join(localTestData, "multipleBinModule"),
importPath: "example.com/test/multipleBinModule",
want: filepath.Join(localTestData, "multipleBinModule", "bin1"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := runBuild(tt.modulePath, tt.importPath, 1)
defer os.Remove(got)
if (err != nil) != tt.wantErr {
t.Errorf("runBuild() error = %v, wantErr %v", err, tt.wantErr)
return
}
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("mismatch (-want, +got):%s", diff)
}
_, err = os.Stat(got)
if err != nil && os.IsNotExist(err) {
t.Errorf("runBuild did not produce the expected binary")
}
})
}
}