Skip to content

Commit 944cce8

Browse files
authoredNov 30, 2021
Merge branch 'main' into bflad-testing-T-Setenv
2 parents b494b72 + 6196e63 commit 944cce8

10 files changed

+424
-32
lines changed
 

‎build/git_revision_test.go

+89
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package build
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/hashicorp/go-version"
@@ -73,3 +74,91 @@ func TestGitRevision_consul(t *testing.T) {
7374
latestConstraint, v)
7475
}
7576
}
77+
78+
func TestGitRevisionValidate(t *testing.T) {
79+
t.Parallel()
80+
81+
testCases := map[string]struct {
82+
gr GitRevision
83+
expectedErr error
84+
}{
85+
"Product-incorrect-binary-name": {
86+
gr: GitRevision{
87+
Product: product.Product{
88+
BinaryName: func() string { return "invalid!" },
89+
Name: product.Terraform.Name,
90+
},
91+
},
92+
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
93+
},
94+
"Product-incorrect-name": {
95+
gr: GitRevision{
96+
Product: product.Product{
97+
BinaryName: product.Terraform.BinaryName,
98+
Name: "invalid!",
99+
},
100+
},
101+
expectedErr: fmt.Errorf("invalid product name: \"invalid!\""),
102+
},
103+
"Product-missing-build-instructions": {
104+
gr: GitRevision{
105+
Product: product.Product{
106+
BinaryName: product.Terraform.BinaryName,
107+
Name: product.Terraform.Name,
108+
},
109+
},
110+
expectedErr: fmt.Errorf("no build instructions"),
111+
},
112+
"Product-missing-build-instructions-build": {
113+
gr: GitRevision{
114+
Product: product.Product{
115+
BinaryName: product.Terraform.BinaryName,
116+
BuildInstructions: &product.BuildInstructions{
117+
GitRepoURL: product.Terraform.BuildInstructions.GitRepoURL,
118+
},
119+
Name: product.Terraform.Name,
120+
},
121+
},
122+
expectedErr: fmt.Errorf("missing build instructions"),
123+
},
124+
"Product-missing-build-instructions-gitrepourl": {
125+
gr: GitRevision{
126+
Product: product.Product{
127+
BinaryName: product.Terraform.BinaryName,
128+
BuildInstructions: &product.BuildInstructions{
129+
Build: product.Terraform.BuildInstructions.Build,
130+
},
131+
Name: product.Terraform.Name,
132+
},
133+
},
134+
expectedErr: fmt.Errorf("missing repository URL"),
135+
},
136+
"Product-valid": {
137+
gr: GitRevision{
138+
Product: product.Terraform,
139+
},
140+
},
141+
}
142+
143+
for name, testCase := range testCases {
144+
name, testCase := name, testCase
145+
146+
t.Run(name, func(t *testing.T) {
147+
t.Parallel()
148+
149+
err := testCase.gr.Validate()
150+
151+
if err == nil && testCase.expectedErr != nil {
152+
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
153+
}
154+
155+
if err != nil && testCase.expectedErr == nil {
156+
t.Fatalf("expected no error, got error: %s", err)
157+
}
158+
159+
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
160+
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
161+
}
162+
})
163+
}
164+
}

‎checkpoint/latest_version_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package checkpoint
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67

78
"github.com/hashicorp/go-version"
@@ -46,3 +47,58 @@ func TestLatestVersion(t *testing.T) {
4647
latestConstraint, v)
4748
}
4849
}
50+
51+
func TestLatestVersionValidate(t *testing.T) {
52+
t.Parallel()
53+
54+
testCases := map[string]struct {
55+
lv LatestVersion
56+
expectedErr error
57+
}{
58+
"Product-incorrect-binary-name": {
59+
lv: LatestVersion{
60+
Product: product.Product{
61+
BinaryName: func() string { return "invalid!" },
62+
Name: product.Terraform.Name,
63+
},
64+
},
65+
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
66+
},
67+
"Product-incorrect-name": {
68+
lv: LatestVersion{
69+
Product: product.Product{
70+
BinaryName: product.Terraform.BinaryName,
71+
Name: "invalid!",
72+
},
73+
},
74+
expectedErr: fmt.Errorf("invalid product name: \"invalid!\""),
75+
},
76+
"Product-valid": {
77+
lv: LatestVersion{
78+
Product: product.Terraform,
79+
},
80+
},
81+
}
82+
83+
for name, testCase := range testCases {
84+
name, testCase := name, testCase
85+
86+
t.Run(name, func(t *testing.T) {
87+
t.Parallel()
88+
89+
err := testCase.lv.Validate()
90+
91+
if err == nil && testCase.expectedErr != nil {
92+
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
93+
}
94+
95+
if err != nil && testCase.expectedErr == nil {
96+
t.Fatalf("expected no error, got error: %s", err)
97+
}
98+
99+
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
100+
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
101+
}
102+
})
103+
}
104+
}

‎fs/exact_version_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package fs
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/go-version"
8+
"github.com/hashicorp/hc-install/product"
9+
)
10+
11+
func TestExactVersionValidate(t *testing.T) {
12+
t.Parallel()
13+
14+
testCases := map[string]struct {
15+
ev ExactVersion
16+
expectedErr error
17+
}{
18+
"Product-incorrect-binary-name": {
19+
ev: ExactVersion{
20+
Product: product.Product{
21+
BinaryName: func() string { return "invalid!" },
22+
},
23+
},
24+
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
25+
},
26+
"Product-missing-get-version": {
27+
ev: ExactVersion{
28+
Product: product.Product{
29+
BinaryName: product.Terraform.BinaryName,
30+
},
31+
Version: version.Must(version.NewVersion("1.0.0")),
32+
},
33+
expectedErr: fmt.Errorf("undeclared version getter"),
34+
},
35+
"Product-and-Version": {
36+
ev: ExactVersion{
37+
Product: product.Terraform,
38+
Version: version.Must(version.NewVersion("1.0.0")),
39+
},
40+
},
41+
"Version-missing": {
42+
ev: ExactVersion{
43+
Product: product.Terraform,
44+
},
45+
expectedErr: fmt.Errorf("undeclared version"),
46+
},
47+
}
48+
49+
for name, testCase := range testCases {
50+
name, testCase := name, testCase
51+
52+
t.Run(name, func(t *testing.T) {
53+
t.Parallel()
54+
55+
err := testCase.ev.Validate()
56+
57+
if err == nil && testCase.expectedErr != nil {
58+
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
59+
}
60+
61+
if err != nil && testCase.expectedErr == nil {
62+
t.Fatalf("expected no error, got error: %s", err)
63+
}
64+
65+
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
66+
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
67+
}
68+
})
69+
}
70+
}

‎fs/fs_test.go

-24
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package fs
33
import (
44
"context"
55
"os"
6-
"path/filepath"
7-
"runtime"
86
"testing"
97

108
"github.com/hashicorp/go-version"
@@ -39,25 +37,3 @@ func TestExactVersion(t *testing.T) {
3937
t.Fatal(err)
4038
}
4139
}
42-
43-
func createTempFile(t *testing.T, content string) (string, string) {
44-
tmpDir := t.TempDir()
45-
fileName := t.Name()
46-
47-
if runtime.GOOS == "windows" {
48-
fileName += ".exe"
49-
}
50-
51-
filePath := filepath.Join(tmpDir, fileName)
52-
f, err := os.Create(filePath)
53-
if err != nil {
54-
t.Fatal(err)
55-
}
56-
defer f.Close()
57-
_, err = f.WriteString(content)
58-
if err != nil {
59-
t.Fatal(err)
60-
}
61-
62-
return tmpDir, fileName
63-
}

‎fs/fs_unix_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
func TestAnyVersion_notExecutable(t *testing.T) {
1818
testutil.EndToEndTest(t)
1919

20-
dirPath, fileName := createTempFile(t, "")
20+
dirPath, fileName := testutil.CreateTempFile(t, "")
2121
t.Setenv("PATH", dirPath)
2222

2323
av := &AnyVersion{
@@ -35,7 +35,7 @@ func TestAnyVersion_notExecutable(t *testing.T) {
3535
func TestAnyVersion_executable(t *testing.T) {
3636
testutil.EndToEndTest(t)
3737

38-
dirPath, fileName := createTempFile(t, "")
38+
dirPath, fileName := testutil.CreateTempFile(t, "")
3939
t.Setenv("PATH", dirPath)
4040

4141
fullPath := filepath.Join(dirPath, fileName)
@@ -59,7 +59,7 @@ func TestAnyVersion_executable(t *testing.T) {
5959
func TestAnyVersion_exactBinPath(t *testing.T) {
6060
testutil.EndToEndTest(t)
6161

62-
dirPath, fileName := createTempFile(t, "")
62+
dirPath, fileName := testutil.CreateTempFile(t, "")
6363
fullPath := filepath.Join(dirPath, fileName)
6464
err := os.Chmod(fullPath, 0700)
6565
if err != nil {
@@ -79,7 +79,7 @@ func TestAnyVersion_exactBinPath(t *testing.T) {
7979
func TestAnyVersion_exactBinPath_notExecutable(t *testing.T) {
8080
testutil.EndToEndTest(t)
8181

82-
dirPath, fileName := createTempFile(t, "")
82+
dirPath, fileName := testutil.CreateTempFile(t, "")
8383
fullPath := filepath.Join(dirPath, fileName)
8484
err := os.Chmod(fullPath, 0600)
8585
if err != nil {

‎fs/fs_windows_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
func TestAnyVersion_executable(t *testing.T) {
1515
testutil.EndToEndTest(t)
1616

17-
dirPath, fileName := createTempFile(t, "")
17+
dirPath, fileName := testutil.CreateTempFile(t, "")
1818
t.Setenv("path", dirPath)
1919

2020
av := &AnyVersion{
@@ -32,7 +32,7 @@ func TestAnyVersion_executable(t *testing.T) {
3232
func TestAnyVersion_exactBinPath(t *testing.T) {
3333
testutil.EndToEndTest(t)
3434

35-
dirPath, fileName := createTempFile(t, "")
35+
dirPath, fileName := testutil.CreateTempFile(t, "")
3636
fullPath := filepath.Join(dirPath, fileName)
3737

3838
av := &AnyVersion{
@@ -48,7 +48,7 @@ func TestAnyVersion_exactBinPath(t *testing.T) {
4848
func TestAnyVersion_exactBinPath_notFound(t *testing.T) {
4949
testutil.EndToEndTest(t)
5050

51-
dirPath, fileName := createTempFile(t, "")
51+
dirPath, fileName := testutil.CreateTempFile(t, "")
5252
fullPath := filepath.Join(dirPath, fileName)
5353

5454
err := os.Remove(fullPath)

‎installer_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package install
22

33
import (
44
"context"
5+
"os"
6+
"path/filepath"
57
"testing"
68

9+
"github.com/hashicorp/hc-install/fs"
710
"github.com/hashicorp/hc-install/internal/testutil"
811
"github.com/hashicorp/hc-install/product"
912
"github.com/hashicorp/hc-install/releases"
1013
"github.com/hashicorp/hc-install/src"
1114
)
1215

13-
func TestInstaller_Ensure(t *testing.T) {
16+
func TestInstaller_Ensure_installable(t *testing.T) {
1417
testutil.EndToEndTest(t)
1518

1619
// most of this logic is already tested within individual packages
@@ -34,6 +37,40 @@ func TestInstaller_Ensure(t *testing.T) {
3437
}
3538
}
3639

40+
func TestInstaller_Ensure_findable(t *testing.T) {
41+
testutil.EndToEndTest(t)
42+
43+
dirPath, fileName := testutil.CreateTempFile(t, "")
44+
45+
fullPath := filepath.Join(dirPath, fileName)
46+
err := os.Chmod(fullPath, 0700)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
t.Setenv("PATH", dirPath)
52+
53+
// most of this logic is already tested within individual packages
54+
// so this is just a simple E2E test to ensure the public API
55+
// also works and continues working
56+
57+
i := NewInstaller()
58+
i.SetLogger(testutil.TestLogger())
59+
ctx := context.Background()
60+
_, err = i.Ensure(ctx, []src.Source{
61+
&fs.AnyVersion{
62+
Product: &product.Product{
63+
BinaryName: func() string {
64+
return fileName
65+
},
66+
},
67+
},
68+
})
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
}
73+
3774
func TestInstaller_Install(t *testing.T) {
3875
testutil.EndToEndTest(t)
3976

‎internal/testutil/temp_file.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package testutil
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
func CreateTempFile(t *testing.T, content string) (string, string) {
11+
tmpDir := t.TempDir()
12+
fileName := t.Name()
13+
14+
if runtime.GOOS == "windows" {
15+
fileName += ".exe"
16+
}
17+
18+
filePath := filepath.Join(tmpDir, fileName)
19+
f, err := os.Create(filePath)
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
defer f.Close()
24+
_, err = f.WriteString(content)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
return tmpDir, fileName
30+
}

‎releases/exact_version_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package releases
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/go-version"
8+
"github.com/hashicorp/hc-install/product"
9+
)
10+
11+
func TestExactVersionValidate(t *testing.T) {
12+
t.Parallel()
13+
14+
testCases := map[string]struct {
15+
ev ExactVersion
16+
expectedErr error
17+
}{
18+
"Product-incorrect-binary-name": {
19+
ev: ExactVersion{
20+
Product: product.Product{
21+
BinaryName: func() string { return "invalid!" },
22+
Name: product.Terraform.Name,
23+
},
24+
},
25+
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
26+
},
27+
"Product-incorrect-name": {
28+
ev: ExactVersion{
29+
Product: product.Product{
30+
BinaryName: product.Terraform.BinaryName,
31+
Name: "invalid!",
32+
},
33+
},
34+
expectedErr: fmt.Errorf("invalid product name: \"invalid!\""),
35+
},
36+
"Product-and-Version": {
37+
ev: ExactVersion{
38+
Product: product.Terraform,
39+
Version: version.Must(version.NewVersion("1.0.0")),
40+
},
41+
},
42+
"Version-missing": {
43+
ev: ExactVersion{
44+
Product: product.Terraform,
45+
},
46+
expectedErr: fmt.Errorf("unknown version"),
47+
},
48+
}
49+
50+
for name, testCase := range testCases {
51+
name, testCase := name, testCase
52+
53+
t.Run(name, func(t *testing.T) {
54+
t.Parallel()
55+
56+
err := testCase.ev.Validate()
57+
58+
if err == nil && testCase.expectedErr != nil {
59+
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
60+
}
61+
62+
if err != nil && testCase.expectedErr == nil {
63+
t.Fatalf("expected no error, got error: %s", err)
64+
}
65+
66+
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
67+
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
68+
}
69+
})
70+
}
71+
}

‎releases/latest_version_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package releases
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/hc-install/product"
8+
)
9+
10+
func TestLatestVersionValidate(t *testing.T) {
11+
t.Parallel()
12+
13+
testCases := map[string]struct {
14+
lv LatestVersion
15+
expectedErr error
16+
}{
17+
"Product-incorrect-binary-name": {
18+
lv: LatestVersion{
19+
Product: product.Product{
20+
BinaryName: func() string { return "invalid!" },
21+
Name: product.Terraform.Name,
22+
},
23+
},
24+
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
25+
},
26+
"Product-incorrect-name": {
27+
lv: LatestVersion{
28+
Product: product.Product{
29+
BinaryName: product.Terraform.BinaryName,
30+
Name: "invalid!",
31+
},
32+
},
33+
expectedErr: fmt.Errorf("invalid product name: \"invalid!\""),
34+
},
35+
"Product-valid": {
36+
lv: LatestVersion{
37+
Product: product.Terraform,
38+
},
39+
},
40+
}
41+
42+
for name, testCase := range testCases {
43+
name, testCase := name, testCase
44+
45+
t.Run(name, func(t *testing.T) {
46+
t.Parallel()
47+
48+
err := testCase.lv.Validate()
49+
50+
if err == nil && testCase.expectedErr != nil {
51+
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
52+
}
53+
54+
if err != nil && testCase.expectedErr == nil {
55+
t.Fatalf("expected no error, got error: %s", err)
56+
}
57+
58+
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
59+
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
60+
}
61+
})
62+
}
63+
}

0 commit comments

Comments
 (0)
Please sign in to comment.