Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/run-tests-in-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
90 changes: 90 additions & 0 deletions lib/reverse-string/analysis.bash
Original file line number Diff line number Diff line change
@@ -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 "$@"
9 changes: 9 additions & 0 deletions tests/reverse-string/expected_analysis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"comments": [
{
"comment": "bash.reverse-string.uses_rev",
"type": "actionable"
}
],
"summary": "Some comments"
}
1 change: 1 addition & 0 deletions tests/reverse-string/reverse_string.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rev <<< "$1"