Skip to content

Commit e677aa4

Browse files
authoredNov 11, 2021
fix: incorrect validation of product & binary names (#28)
* fix: incorrect validation of product & binary names * installer: add E2E test for the public API too
1 parent f29401f commit e677aa4

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed
 

‎installer_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -1 +1,29 @@
11
package install
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/hashicorp/hc-install/internal/testutil"
8+
"github.com/hashicorp/hc-install/product"
9+
"github.com/hashicorp/hc-install/releases"
10+
"github.com/hashicorp/hc-install/src"
11+
)
12+
13+
func TestInstaller_Ensure(t *testing.T) {
14+
testutil.EndToEndTest(t)
15+
16+
// most of this logic is already tested within individual packages
17+
// so this is just a simple E2E test to ensure the public API
18+
// also works and continues working
19+
20+
i := NewInstaller()
21+
_, err := i.Ensure(context.Background(), []src.Source{
22+
&releases.LatestVersion{
23+
Product: product.Terraform,
24+
},
25+
})
26+
if err != nil {
27+
t.Fatal(err)
28+
}
29+
}

‎internal/validators/productname.go ‎internal/validators/validators.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package validators
33
import "regexp"
44

55
var (
6-
productNameRe = regexp.MustCompile(`/[a-z0-9-]+/`)
7-
binaryNameRe = regexp.MustCompile(`/[a-zA-Z0-9-_.]+/`)
6+
productNameRe = regexp.MustCompile(`^[a-z0-9-]+$`)
7+
binaryNameRe = regexp.MustCompile(`^[a-zA-Z0-9-_.]+$`)
88
)
99

1010
// IsProductNameValid provides early user-facing validation of a product name
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package validators
2+
3+
import "testing"
4+
5+
func TestIsProductNameValid(t *testing.T) {
6+
testCases := []struct {
7+
name string
8+
expectedValid bool
9+
}{
10+
{
11+
"terraform",
12+
true,
13+
},
14+
{
15+
"in.valid",
16+
false,
17+
},
18+
}
19+
for _, tc := range testCases {
20+
isValid := IsProductNameValid(tc.name)
21+
if !isValid && tc.expectedValid {
22+
t.Fatalf("expected %q to be valid", tc.name)
23+
}
24+
if isValid && !tc.expectedValid {
25+
t.Fatalf("expected %q to be invalid", tc.name)
26+
}
27+
}
28+
}
29+
30+
func TestIsBinaryNameValid(t *testing.T) {
31+
testCases := []struct {
32+
name string
33+
expectedValid bool
34+
}{
35+
{
36+
"terraform",
37+
true,
38+
},
39+
{
40+
"Terraform",
41+
true,
42+
},
43+
{
44+
"va_lid",
45+
true,
46+
},
47+
{
48+
"va.lid",
49+
true,
50+
},
51+
{
52+
"in/valid",
53+
false,
54+
},
55+
}
56+
for _, tc := range testCases {
57+
isValid := IsBinaryNameValid(tc.name)
58+
if !isValid && tc.expectedValid {
59+
t.Fatalf("expected %q to be valid", tc.name)
60+
}
61+
if isValid && !tc.expectedValid {
62+
t.Fatalf("expected %q to be invalid", tc.name)
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)
Please sign in to comment.