Skip to content

Commit

Permalink
fix: incorrect validation of product & binary names (#28)
Browse files Browse the repository at this point in the history
* fix: incorrect validation of product & binary names

* installer: add E2E test for the public API too
  • Loading branch information
radeksimko committed Nov 11, 2021
1 parent f29401f commit e677aa4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
28 changes: 28 additions & 0 deletions installer_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
package install

import (
"context"
"testing"

"github.com/hashicorp/hc-install/internal/testutil"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
)

func TestInstaller_Ensure(t *testing.T) {
testutil.EndToEndTest(t)

// most of this logic is already tested within individual packages
// so this is just a simple E2E test to ensure the public API
// also works and continues working

i := NewInstaller()
_, err := i.Ensure(context.Background(), []src.Source{
&releases.LatestVersion{
Product: product.Terraform,
},
})
if err != nil {
t.Fatal(err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package validators
import "regexp"

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

// IsProductNameValid provides early user-facing validation of a product name
Expand Down
65 changes: 65 additions & 0 deletions internal/validators/validators_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package validators

import "testing"

func TestIsProductNameValid(t *testing.T) {
testCases := []struct {
name string
expectedValid bool
}{
{
"terraform",
true,
},
{
"in.valid",
false,
},
}
for _, tc := range testCases {
isValid := IsProductNameValid(tc.name)
if !isValid && tc.expectedValid {
t.Fatalf("expected %q to be valid", tc.name)
}
if isValid && !tc.expectedValid {
t.Fatalf("expected %q to be invalid", tc.name)
}
}
}

func TestIsBinaryNameValid(t *testing.T) {
testCases := []struct {
name string
expectedValid bool
}{
{
"terraform",
true,
},
{
"Terraform",
true,
},
{
"va_lid",
true,
},
{
"va.lid",
true,
},
{
"in/valid",
false,
},
}
for _, tc := range testCases {
isValid := IsBinaryNameValid(tc.name)
if !isValid && tc.expectedValid {
t.Fatalf("expected %q to be valid", tc.name)
}
if isValid && !tc.expectedValid {
t.Fatalf("expected %q to be invalid", tc.name)
}
}
}

0 comments on commit e677aa4

Please sign in to comment.