Skip to content

Commit

Permalink
Add 'expect' assertions, support 'step' in expressions
Browse files Browse the repository at this point in the history
Allow users to add assertions after steps are run using the 'expect'
key, which can be either a string expression or an array of expressions.
We return the error from the first that fails.

Add the results of the step into the context, and allow users to
reference step results using the 'step' context name.
  • Loading branch information
jonsmock committed Jul 1, 2024
1 parent 06b2e06 commit 2e49bae
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
-v $(pwd)/examples:/app/examples \
dctest --results-file /app/examples/results.json examples /app/examples/00-intro.yaml \
|| true
jq --exit-status '[.pass == 3, .fail == 1] | all' examples/results.json
jq --exit-status '[.pass == 4, .fail == 1] | all' examples/results.json
- name: Run Examples with --continue-on-error
run: |
Expand All @@ -61,7 +61,7 @@ jobs:
-v $(pwd)/examples:/app/examples \
dctest --continue-on-error --results-file /app/examples/results.json examples /app/examples/00-intro.yaml \
|| true
jq --exit-status '[.pass == 4, .fail == 3] | all' examples/results.json
jq --exit-status '[.pass == 5, .fail == 3] | all' examples/results.json
- name: Setup Fixtures
Expand Down
12 changes: 12 additions & 0 deletions examples/00-intro.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ tests:
run: |
[ "" == "${FOO}" ]
expect-assertions:
name: Expect test
steps:
- exec: node1
run: echo -n "2"
expect: step.stdout == "2"
- exec: node1
run: echo -n "3"
expect:
- step.stdout != "2"
- step.stdout == "3"

fail-exit-code:
name: Failing due to non-zero exit code
steps:
Expand Down
4 changes: 4 additions & 0 deletions schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ properties:
properties:
env: { "$ref": "#/$defs/env" }
exec: { type: string, expression: InterpolatedText }
expect:
oneOf:
- { type: string, expression: Expression }
- { type: array, items: { type: string, expression: Expression } }
name: { type: string }
if: { type: string, expression: Expression, default: "success()" }
index: { type: integer }
Expand Down
9 changes: 9 additions & 0 deletions src/dctest/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ Options:
interpolate #(expr/interpolate-text step-context %)
step (-> step
(update :exec interpolate)
(update :expect #(if (string? %) [%] %))
(update :index #(or % 1))
(update :run (fn [command]
(if (string? command)
Expand All @@ -204,6 +205,14 @@ Options:
{:delay-ms interval
:check-fn #(not (:error %))})

step-context (assoc step-context :step results)
run-expect (fn [expr]
(when-not (expr/read-eval step-context expr)
{:error {:message (str "Expectation failure: " expr)}}))
expect-results (when-not (:error results)
(first (keep run-expect (:expect step))))
results (merge expect-results results)

outcome (if (:error results) :fail :pass)
results (merge results
{:outcome outcome}
Expand Down
2 changes: 1 addition & 1 deletion src/dctest/expressions.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(declare read-ast)

(def supported-contexts
#{"env" "process"})
#{"env" "process" "step"})

(def stdlib
{
Expand Down
8 changes: 5 additions & 3 deletions test/dctest/expressions_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@

;; Supported contexts
(let [contexts {:env {"FOO" 1}
:process {"pid" 123}}]
:process {"pid" 123}
:step {:code 0 :stdout "hi" :stderr ""}}]
(are [expected text] (= expected (expr/read-eval contexts text))
1 "env.FOO"
123 "process.pid"))
1 "env.FOO"
123 "process.pid"
"hi" "step.stdout"))

;; Coerce keywords to strings inside context
(is (= 123
Expand Down

0 comments on commit 2e49bae

Please sign in to comment.