forked from hashicorp/hc-install
/
version_test.go
70 lines (60 loc) · 1.64 KB
/
version_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
package fs
import (
"fmt"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/hc-install/product"
)
func TestVersionValidate(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
v Version
expectedErr error
}{
"Product-incorrect-binary-name": {
v: Version{
Product: product.Product{
BinaryName: func() string { return "invalid!" },
},
},
expectedErr: fmt.Errorf("invalid binary name: \"invalid!\""),
},
"Product-missing-get-version": {
v: Version{
Product: product.Product{
BinaryName: product.Terraform.BinaryName,
},
Constraints: version.MustConstraints(version.NewConstraint(">= 1.0")),
},
expectedErr: fmt.Errorf("undeclared version getter"),
},
"Product-missing-version-constraint": {
v: Version{
Product: product.Terraform,
},
expectedErr: fmt.Errorf("undeclared version constraints"),
},
"Product-and-version-constraint": {
v: Version{
Product: product.Terraform,
Constraints: version.MustConstraints(version.NewConstraint(">= 1.0")),
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
err := testCase.v.Validate()
if err == nil && testCase.expectedErr != nil {
t.Fatalf("expected error: %s, got no error", testCase.expectedErr)
}
if err != nil && testCase.expectedErr == nil {
t.Fatalf("expected no error, got error: %s", err)
}
if err != nil && testCase.expectedErr != nil && err.Error() != testCase.expectedErr.Error() {
t.Fatalf("expected error: %s, got error: %s", testCase.expectedErr, err)
}
})
}
}