diff --git a/bin/run-tests-in-docker.sh b/bin/run-tests-in-docker.sh index 0ad605c..7bb26e2 100755 --- a/bin/run-tests-in-docker.sh +++ b/bin/run-tests-in-docker.sh @@ -21,7 +21,7 @@ docker run \ --network none \ --read-only \ --mount type=bind,src="${PWD}/tests",dst=/opt/analyzer/tests \ - --mount type=tmpfs,dst=/tmp \ + --mount type=volume,dst=/tmp \ --workdir /opt/analyzer \ --entrypoint /opt/analyzer/bin/run-tests.sh \ exercism/bash-analyzer diff --git a/lib/reverse-string/analysis.bash b/lib/reverse-string/analysis.bash new file mode 100644 index 0000000..9871836 --- /dev/null +++ b/lib/reverse-string/analysis.bash @@ -0,0 +1,90 @@ +#!/usr/bin/env bash + +# Check for using the `rev` executable. +uses_rev() { + local tmp output + + tmp=$(mktemp -d) + trap "rm -r ${tmp}" EXIT + { + echo '#!/usr/bin/env bash' + echo 'echo USES_REV' + } > "${tmp}/rev" + chmod 755 "${tmp}/rev" + + output=$( + PATH="${tmp}:${PATH}" + bash "${in_dir}/reverse_string.sh" "input" + ) + + [[ $output = *USES_REV* ]] && echo '{"comment": "bash.reverse-string.uses_rev", "type": "actionable"}' +} + +# Other check functions can go here: +# ... + + +# ------------------------------------------------------------ +# Merge results into analysis.json generated by the shellcheck analysis. + +comments_array_as_json() { + jq -n --jsonargs '{comments: $ARGS.positional}' "$@" +} + +write_json() { + local -i shellcheck_comments + local temp + + # How many shellcheck comments were found? + shellcheck_comments=$(jq '.comments | length' "$out_dir/analysis.json") + + case "$shellcheck_comments,${#general_comments[@]}" in + 0,0) + jq -n '{ + summary: "Congrats! No suggestions", + comments: [] + }' > "$out_dir/analysis.json" + ;; + 0,*) + comments_array_as_json "${general_comments[@]}" \ + | jq '.summary = "Some comments"' \ + > "$out_dir"/analysis.json + ;; + *,0) + # no need to alter analysis.json + : ;; + *,*) + temp=$(mktemp) + + # grab the summary from the shellcheck analysis + summary=$(jq -r .summary "$out_dir/analysis.json") + + # Thanks to https://stackoverflow.com/a/36218044/7552 + jq --arg sum "$summary" --slurp '{ + summary: $sum, + comments: (reduce .[] as $item ([]; . + $item.comments)) + }' \ + "$out_dir/analysis.json" \ + <(comments_array_as_json "${general_comments[@]}") \ + > "$temp" && mv "$temp" "$out_dir/analysis.json" + ;; + esac +} + +# ------------------------------------------------------------ +analyze() { + local in_dir=$1 out_dir=$2 snake_slug=$3 + local filename="$in_dir/${snake_slug}.sh" + local general_comments=() + local IFS=, + + json=$(uses_rev) + [[ -n $json ]] && general_comments+=("$json") + + # other checks can go here to add to the "general_comments" array + # .... + + write_json +} + +analyze "$@" diff --git a/tests/reverse-string/expected_analysis.json b/tests/reverse-string/expected_analysis.json new file mode 100644 index 0000000..05bed59 --- /dev/null +++ b/tests/reverse-string/expected_analysis.json @@ -0,0 +1,9 @@ +{ + "comments": [ + { + "comment": "bash.reverse-string.uses_rev", + "type": "actionable" + } + ], + "summary": "Some comments" +} diff --git a/tests/reverse-string/reverse_string.sh b/tests/reverse-string/reverse_string.sh new file mode 100644 index 0000000..b261883 --- /dev/null +++ b/tests/reverse-string/reverse_string.sh @@ -0,0 +1 @@ +rev <<< "$1"