Skip to content

Commit

Permalink
Merge pull request #9 from drlau/matchany-test-ci
Browse files Browse the repository at this point in the history
Make MatchAny clear and add CI testing and releasing
  • Loading branch information
drlau authored Feb 1, 2021
2 parents a221236 + 701d9e0 commit 93a1909
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 8 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: goreleaser

# TODO: automatic based on merged PRs
on:
push:
tags:
- '*'

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test
on:
push:
pull_request:
workflow_dispatch:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
go: [1.15.x]
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Run test
run: go test -v ./...
2 changes: 1 addition & 1 deletion pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *resource) CompareResult(values map[string]interface{}) *CompareResult {
}
if !found {
failedArgs[k] = FailedArg{
Expected: enforced.MatchAny,
Expected: fmt.Sprintf("one of: %v", enforced.MatchAny),
Actual: v,
MatchAny: true,
}
Expand Down
81 changes: 74 additions & 7 deletions pkg/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,38 @@ func TestResourceCompare(t *testing.T) {
},
expected: false,
},
"enforced value in matchAny": {
resource: &resource{
Enforced: map[string]ruleset.EnforceChange{
"key": {
MatchAny: []interface{}{"value1", "value2"},
},
},
CompareOptions: &CompareOptions{},
},
values: ResourceValues{
Values: map[string]interface{}{
"key": "value1",
},
},
expected: true,
},
"enforced value not in matchAny": {
resource: &resource{
Enforced: map[string]ruleset.EnforceChange{
"key": {
MatchAny: []interface{}{"value1", "value2"},
},
},
CompareOptions: &CompareOptions{},
},
values: ResourceValues{
Values: map[string]interface{}{
"key": "value",
},
},
expected: false,
},
}

for name, tc := range cases {
Expand Down Expand Up @@ -524,7 +556,7 @@ func TestResourceDiff(t *testing.T) {
"key": "value",
},
},
expected: []string{""},
expected: []string{},
},
"enforced value does not match": {
resource: &resource{
Expand Down Expand Up @@ -565,7 +597,7 @@ func TestResourceDiff(t *testing.T) {
"ignored": "ignored",
},
},
expected: []string{""},
expected: []string{},
},
"extra value": {
resource: &resource{
Expand Down Expand Up @@ -604,7 +636,7 @@ func TestResourceDiff(t *testing.T) {
"ignored": "ignored",
},
},
expected: []string{""},
expected: []string{},
},
"missing enforced value": {
resource: &resource{
Expand All @@ -623,7 +655,7 @@ func TestResourceDiff(t *testing.T) {
"key": "value",
},
},
expected: []string{""},
expected: []string{},
},
"missing enforced value with EnforceAll": {
resource: &resource{
Expand Down Expand Up @@ -661,7 +693,7 @@ func TestResourceDiff(t *testing.T) {
"key": "value",
},
},
expected: []string{""},
expected: []string{},
},
"ignored arg with extra value": {
resource: &resource{
Expand Down Expand Up @@ -696,7 +728,7 @@ func TestResourceDiff(t *testing.T) {
"second": "value",
},
},
expected: []string{""},
expected: []string{},
},
"values is missing a key from ignored or enforced": {
resource: &resource{
Expand All @@ -717,7 +749,7 @@ func TestResourceDiff(t *testing.T) {
"enforced": "value",
},
},
expected: []string{""},
expected: []string{},
},
"values is missing a key from ignored or enforced and requireAll is enabled": {
resource: &resource{
Expand Down Expand Up @@ -776,11 +808,46 @@ func TestResourceDiff(t *testing.T) {
},
expected: []string{"AutoFail set to true"},
},
"enforced value in matchAny": {
resource: &resource{
Enforced: map[string]ruleset.EnforceChange{
"key": {
MatchAny: []interface{}{"value1", "value2"},
},
},
CompareOptions: &CompareOptions{},
},
values: ResourceValues{
Values: map[string]interface{}{
"key": "value1",
},
},
expected: []string{},
},
"enforced value not in matchAny": {
resource: &resource{
Enforced: map[string]ruleset.EnforceChange{
"key": {
MatchAny: []interface{}{"value1", "value2"},
},
},
CompareOptions: &CompareOptions{},
},
values: ResourceValues{
Values: map[string]interface{}{
"key": "value",
},
},
expected: []string{"one of: [value1 value2]"},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := tc.resource.Diff(tc.values)
if len(tc.expected) == 0 && got != "" {
t.Errorf("Expected no diff but got: \n%v", got)
}
for _, s := range tc.expected {
if !strings.Contains(got, s) {
t.Errorf("Result string did not contain %v", s)
Expand Down

0 comments on commit 93a1909

Please sign in to comment.