Skip to content

Commit

Permalink
Add Regal for linting
Browse files Browse the repository at this point in the history
Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Oct 24, 2023
1 parent 2021529 commit 0b994a8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ jobs:
- name: OPA check strict
run: opa check --strict test/

- name: Check formatting
run: opa fmt --diff --fail test/
- name: Setup Regal
uses: StyraInc/[email protected]
with:
version: latest
- run: regal lint --format github test/
7 changes: 7 additions & 0 deletions .regal/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
rules:
idiomatic:
no-defined-entrypoint:
level: ignore
testing:
print-or-trace-call:
level: ignore
69 changes: 34 additions & 35 deletions test/assert.rego
Original file line number Diff line number Diff line change
Expand Up @@ -6,151 +6,150 @@
package test.assert

import future.keywords.every
import future.keywords.if
import future.keywords.in

# METADATA
# description: Assert expected is equal to result
equals(expected, result) {
equals(expected, result) if {
expected == result
} else := false {
} else := false if {
print("expected equals:", _quote_str(expected), "got:", result)
}

# METADATA
# description: Assert expected is not equal to result
not_equals(expected, result) {
not_equals(expected, result) if {
expected != result
} else := false {
} else := false if {
print("expected not equals:", _quote_str(expected), "got:", result)
}

# METADATA
# description: Assert all items in coll are equal to value
all_equals(coll, value) {
all_equals(coll, value) if {
every item in coll {
item == value
}
} else := false {
} else := false if {
exceptions := [item | some item in coll; item != value]
print("expected all items to have value", _append_comma(value), "failed for", exceptions)
}

# METADATA
# description: Assert no items in coll are equal to value
none_equals(coll, value) {
none_equals(coll, value) if {
every item in coll {
item != value
}
} else := false {
} else := false if {
exceptions := [item | some item in coll; item == value]
print("expected no items to have value", _append_comma(value), "failed for", exceptions)
}

# METADATA
# description: Assert item is in coll
has(item, coll) {
has(item, coll) if {
item in coll
} else := false {
} else := false if {
print("expected", type_name(item), _quote_str(item), "in", type_name(coll), "got:", coll)
}

# METADATA
# description: Assert item is not in coll
not_has(item, coll) {
not_has(item, coll) if {
not item in coll
} else := false {
} else := false if {
print("expected", type_name(item), _quote_str(item), "not in", type_name(coll), "got:", coll)
}

# METADATA
# description: Assert provided collection is empty
empty(coll) {
empty(coll) if {
count(coll) == 0
} else := false {
} else := false if {
print("expected empty", type_name(coll), "got:", coll)
}

# METADATA
# description: Assert provided collection is not empty
not_empty(coll) {
not_empty(coll) if {
count(coll) != 0
} else := false {
} else := false if {
print("expected not empty", type_name(coll))
}

# METADATA
# description: Assert string starts with search
starts_with(str, search) {
starts_with(str, search) if {
startswith(str, search)
} else := false {
} else := false if {
print("expected", _quote_str(str), "to start with", _quote_str(search))
}

# METADATA
# description: Assert string ends with search
ends_with(str, search) {
ends_with(str, search) if {
endswith(str, search)
} else := false {
} else := false if {
print("expected", _quote_str(str), "to end with", _quote_str(search))
}

# METADATA
# description: Assert all strings in coll starts with search
all_starts_with(coll, search) {
all_starts_with(coll, search) if {
every str in coll {
startswith(str, search)
}
} else := false {
} else := false if {
exceptions := [str | some str in coll; not startswith(str, search)]
print("expected all strings to start with", _append_comma(search), "failed for", exceptions)
}

# METADATA
# description: Assert all strings in coll ends with search
all_ends_with(coll, search) {
all_ends_with(coll, search) if {
every str in coll {
endswith(str, search)
}
} else := false {
} else := false if {
exceptions := [str | some str in coll; not endswith(str, search)]
print("expected all strings to end with", _append_comma(search), "failed for", exceptions)
}

# METADATA
# description: Assert no strings in coll starts with search
none_starts_with(coll, search) {
none_starts_with(coll, search) if {
every str in coll {
not startswith(str, search)
}
} else := false {
} else := false if {
exceptions := [str | some str in coll; startswith(str, search)]
print("expected no strings to start with", _append_comma(search), "failed for", exceptions)
}

# METADATA
# description: Assert no strings in coll ends with search
none_ends_with(coll, search) {
none_ends_with(coll, search) if {
every str in coll {
not endswith(str, search)
}
} else := false {
} else := false if {
exceptions := [str | some str in coll; endswith(str, search)]
print("expected no strings to end with", _append_comma(search), "failed for", exceptions)
}

# METADATA
# description: Fail with provided message
fail(msg) {
fail(msg) if {
print(msg)

# regal ignore:constant-condition
false
}

_quote_str(x) := concat("", [`"`, x, `"`]) {
is_string(x)
}
_quote_str(x) := concat("", [`"`, x, `"`]) if is_string(x)

_quote_str(x) := x {
not is_string(x)
}
_quote_str(x) := x if not is_string(x)

_append_comma(str) := sprintf("%v,", [_quote_str(str)])

0 comments on commit 0b994a8

Please sign in to comment.