-
Notifications
You must be signed in to change notification settings - Fork 3
/
runner_test.go
146 lines (126 loc) · 3.28 KB
/
runner_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package build
import (
"path/filepath"
"testing"
"time"
"github.com/hashicorp/actions-go-build/pkg/crt"
"github.com/hashicorp/composite-action-framework-go/pkg/fs"
"github.com/hashicorp/composite-action-framework-go/pkg/git"
tmp "github.com/hashicorp/composite-action-framework-go/pkg/testhelpers/tmptest"
)
func TestRunner_Run_ok(t *testing.T) {
dir := tmp.Dir(t)
t.Logf("Test dir: %q", dir)
testBuild, err := New("test-build", standardConfig(dir))
if err != nil {
t.Fatal(err)
}
b := testBuild.(*core)
b.createTestProductRepo(t)
r, err := NewRunner(b)
if err != nil {
t.Fatal(err)
}
result := r.Run()
if err := result.Error(); err != nil {
t.Fatal(err)
}
}
func TestRunner_Run_err(t *testing.T) {
dir := tmp.Dir(t)
t.Logf("Test dir: %q", dir)
c := standardConfig(dir)
c.Parameters.Instructions = "echo 'oh no!'; exit 1"
testBuild, err := New("test-build", c)
if err != nil {
t.Fatal(err)
}
b := testBuild.(*core)
b.createTestProductRepo(t)
r, err := NewRunner(b)
if err != nil {
t.Fatal(err)
}
result := r.Run()
gotErr := result.Error()
want := "running build instructions: exit status 1"
if gotErr == nil {
t.Fatalf("got nil error; want %q", want)
}
got := gotErr.Error()
if want != got {
t.Fatalf("got error %q; want %q", got, want)
}
}
const mainDotGo = `
package main
import "fmt"
func main() {
fmt.Println("hello, world")
}
`
const goDotMod = `module github.com/dadgarcorp/lockbox
go 1.18
`
// createTestProductRepo creates a test repo and returns its path.
func (b *core) createTestProductRepo(t *testing.T) {
b.writeTestFile(t, "main.go", mainDotGo)
b.writeTestFile(t, "go.mod", goDotMod)
repo, err := git.Init(b.config.Paths.WorkDir, git.WithAuthor("test", "test@test.com"))
if err != nil {
t.Fatal(err)
}
if err := repo.Add("."); err != nil {
t.Fatal(err)
}
if err := repo.Commit("initial commit"); err != nil {
t.Fatal(err)
}
}
// must is a quick way to fail a test depending on if an error is nil or not.
func must(t *testing.T, err error) {
if err != nil {
t.Fatal(err)
}
}
func (b *core) runTestCommand(t *testing.T, name string, args ...string) {
must(t, b.runCommand(name, args...))
}
func (b *core) writeTestFile(t *testing.T, name, contents string) {
name = filepath.Join(b.config.Paths.WorkDir, name)
must(t, fs.WriteFile(name, contents))
}
func standardCommitTime() (ts time.Time, rfc3339 string) {
ts = time.Date(2022, 7, 4, 11, 33, 33, 0, time.UTC)
rfc3339 = ts.Format(time.RFC3339)
return
}
func standardConfig(workDir string) Config {
_, revisionTimestampRFC3339 := standardCommitTime()
return Config{
Product: crt.Product{
Repository: "dadgarcorp/lockbox",
Name: "lockbox",
Version: crt.ProductVersion{
Full: "1.2.3",
Core: "1.2.3",
Meta: "1.2.3",
},
Revision: "cabba9e",
RevisionTime: revisionTimestampRFC3339,
},
Parameters: Parameters{
Instructions: `echo -n "Building '$BIN_PATH'..." && go build -o $BIN_PATH && echo "Done!"`,
OS: "linux",
Arch: "amd64",
},
Paths: Paths{
WorkDir: workDir,
BinPath: filepath.Join(workDir, "dist", "lockbox"),
ZipPath: filepath.Join(workDir, "out", "lockbox_1.2.3_amd64.zip"),
MetaDir: filepath.Join(workDir, "meta"),
},
}
}