diff options
author | Lars Wirzenius <liw@liw.fi> | 2020-12-02 07:46:50 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2020-12-02 08:18:21 +0200 |
commit | d9da7a7a1ab5b4199b17770d222fc5f041e4ad3e (patch) | |
tree | 0870b8468df245b43fcef74e0d3b0fc093767f20 /templates | |
parent | ff07da8028390c9834212f8898c64f8fbcf30bd7 (diff) | |
download | subplot-d9da7a7a1ab5b4199b17770d222fc5f041e4ad3e.tar.gz |
test: use shellcheck to check most shell code
Split the Bash template into smaller files that don't use templating,
and check those file. Fix the couple of things shellcheck complained
about.
Diffstat (limited to 'templates')
-rw-r--r-- | templates/bash/assert.sh | 19 | ||||
-rw-r--r-- | templates/bash/cap.sh | 17 | ||||
-rw-r--r-- | templates/bash/ctx.sh | 17 | ||||
-rw-r--r-- | templates/bash/dict.sh | 50 | ||||
-rw-r--r-- | templates/bash/files.sh | 22 | ||||
-rw-r--r-- | templates/bash/template.sh | 130 | ||||
-rw-r--r-- | templates/bash/template.yaml | 6 |
7 files changed, 137 insertions, 124 deletions
diff --git a/templates/bash/assert.sh b/templates/bash/assert.sh new file mode 100644 index 0000000..43bb11b --- /dev/null +++ b/templates/bash/assert.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Check two values for equality and give error if they are not equal +assert_eq() { + if ! diff -u <(echo "$1") <(echo "$2") + then + echo "expected values to be identical, but they're not" + exit 1 + fi +} + +# Check first value contains second value. +assert_contains() { + if ! echo "$1" | grep -F "$2" > /dev/null + then + echo "expected first value to contain second value" + exit 1 + fi +} diff --git a/templates/bash/cap.sh b/templates/bash/cap.sh new file mode 100644 index 0000000..8ea35d8 --- /dev/null +++ b/templates/bash/cap.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Store step captures for calling the corresponding functions. + +cap_new() { + dict_new _cap +} + +cap_set() +{ + dict_set _cap "$1" "$2" +} + +cap_get() +{ + dict_get _cap "$1" +} diff --git a/templates/bash/ctx.sh b/templates/bash/ctx.sh new file mode 100644 index 0000000..c9401c6 --- /dev/null +++ b/templates/bash/ctx.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# A context abstraction using dictionaries. + +ctx_new() { + dict_new _ctx +} + +ctx_set() +{ + dict_set _ctx "$1" "$2" +} + +ctx_get() +{ + dict_get _ctx "$1" +} diff --git a/templates/bash/dict.sh b/templates/bash/dict.sh new file mode 100644 index 0000000..aea5b96 --- /dev/null +++ b/templates/bash/dict.sh @@ -0,0 +1,50 @@ +#!/bin/bash + +# Simple dictionary abstraction. All values are stored in files so +# they can more easily be inspected. + +dict_new() { + local name="$1" + rm -rf "$name" + mkdir "$name" +} + +dict_has() { + local name="$1" + local key="$2" + local f="$name/$key" + test -e "$f" +} + +dict_get() { + local name="$1" + local key="$2" + local f="$name/$key" + cat "$f" +} + +dict_get_default() { + local name="$1" + local key="$2" + local default="$3" + local f="$name/$key" + if [ -e "$f" ] + then + cat "$f" + else + echo "$default" + fi +} + +dict_set() { + local name="$1" + local key="$2" + local value="$3" + local f="$name/$key" + echo "$value" > "$f" +} + +dict_keys() { + local name="$1" + ls -1 "$name" +} diff --git a/templates/bash/files.sh b/templates/bash/files.sh new file mode 100644 index 0000000..50c935d --- /dev/null +++ b/templates/bash/files.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Store files embedded in the markdown input. + +files_new() { + dict_new _files +} + +files_set() { + dict_set _files "$1" "$2" +} + +files_get() { + dict_get _files "$1" +} + + +# Decode a base64 encoded string. + +decode_base64() { + echo "$1" | base64 -d +} diff --git a/templates/bash/template.sh b/templates/bash/template.sh index 565fa66..5e92371 100644 --- a/templates/bash/template.sh +++ b/templates/bash/template.sh @@ -14,131 +14,13 @@ set -eu -o pipefail ############################################################################# -# Helper code generated by Subplot. +# Scaffolding for generated test program. - -# Simple dictionary abstraction. All values are stored in files so -# they can more easily be inspected. - -dict_new() { - local name="$1" - rm -rf "$name" - mkdir "$name" -} - -dict_has() { - local name="$1" - local key="$2" - local f="$name/$key" - test -e "$f" -} - -dict_get() { - local name="$1" - local key="$2" - local f="$name/$key" - cat "$f" -} - -dict_get_default() { - local name="$1" - local key="$2" - local default="$3" - local f="$name/$key" - if [ -e "$f" ] - then - cat "$f" - else - echo "$default" - fi -} - -dict_set() { - local name="$1" - local key="$2" - local value="$3" - local f="$name/$key" - echo "$value" > "$f" -} - -dict_keys() { - local name="$1" - ls -1 "$name" -} - - -# A context abstraction using dictionaries. - -ctx_new() { - dict_new _ctx -} - -ctx_set() -{ - dict_set _ctx "$1" "$2" -} - -ctx_get() -{ - dict_get _ctx "$1" -} - - -# Store step captures for calling the corresponding functions. - -cap_new() { - dict_new _cap -} - -cap_set() -{ - dict_set _cap "$1" "$2" -} - -cap_get() -{ - dict_get _cap "$1" -} - - -# Store files embedded in the markdown input. - -files_new() { - dict_new _files -} - -files_set() { - dict_set _files "$1" "$2" -} - -files_get() { - dict_get _files "$1" -} - - -# Decode a base64 encoded string. - -decode_base64() { - echo "$1" | base64 -d -} - -# Check two values for equality and give error if they are not equal -assert_eq() { - if ! diff -u <(echo "$1") <(echo "$2") - then - echo "expected values to be identical, but they're not" - exit 1 - fi -} - -# Check first value contains second value. -assert_contains() { - if ! echo "$1" | grep -F "$2" > /dev/null - then - echo "expected first value to contain second value" - exit 1 - fi -} +{% include "dict.sh" %} +{% include "ctx.sh" %} +{% include "cap.sh" %} +{% include "files.sh" %} +{% include "assert.sh" %} # Remember where we started from. The step functions may need to refer # to files there. diff --git a/templates/bash/template.yaml b/templates/bash/template.yaml index b542a06..16a7523 100644 --- a/templates/bash/template.yaml +++ b/templates/bash/template.yaml @@ -1,2 +1,8 @@ template: template.sh run: bash +helpers: + - assert.sh + - cap.sh + - ctx.sh + - dict.sh + - files.sh |