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
31 changes: 31 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Contributions for Exercism Zig Track

We 💙 our community but **this repository does not accept unsolicited pull requests at this time**.

Please read this [community blog post][guidelines] for details.

If you want to contribute or have a bug to report, please open a topic [in the forum][zig-forum] first, so we can discuss things before we do things.

Once you have a go-ahead from one of the maintainers, a pull request must adhere to [Exercism's style guide][style].

If the PR touches an existing exercise, please also consider [this warning][unnecessary-test-runs] in the documentation for [building tracks][building-tracks].

## Running Tests

Exercises can be tested against the locally installed Zig compliler using

```bash
bin/run-tests
```

Exercises can be tested against the test runner using

```bash
bin/verify-exercises-in-docker
```

[guidelines]: https://exercism.org/blog/contribution-guidelines-nov-2023
[zig-forum]: https://forum.exercism.org/c/programming/zig/199
[building-tracks]: https://exercism.org/docs/building/tracks
[style]: https://exercism.org/docs/building/markdown/style-guide
[unnecessary-test-runs]: https://exercism.org/docs/building/tracks#h-avoiding-triggering-unnecessary-test-runs
102 changes: 102 additions & 0 deletions bin/verify-exercises-in-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash

# Synopsis:
# Verify that each exercise's example/exemplar solution passes the tests
# using the track's test runner Docker image.
# You can either verify all exercises or a single exercise.

# Example: verify all exercises in Docker
# bin/verify-exercises-in-docker

# Example: verify single exercise in Docker
# bin/verify-exercises-in-docker two-fer

set -eo pipefail

die() { echo "$*" >&2; exit 1; }

required_tool() {
command -v "${1}" >/dev/null 2>&1 ||
die "${1} is required but not installed. Please install it and make sure it's in your PATH."
}

required_tool docker

copy_example_or_examplar_to_solution() {
jq -c '[.files.solution, .files.exemplar // .files.example] | transpose | map({src: .[1], dst: .[0]}) | .[]' .meta/config.json \
| while read -r src_and_dst; do
cp "$(jq -r '.src' <<< "${src_and_dst}")" "$(jq -r '.dst' <<< "${src_and_dst}")"
done
}

pull_docker_image() {
docker pull "${image}" ||
die $'Could not find the `'"${image}"$'` Docker image.\nCheck the test runner docs at https://exercism.org/docs/building/tooling/test-runners for more information.'
}

run_tests() {
local slug
slug="${1}"

docker run \
--rm \
--network none \
--mount type=bind,src="${PWD}",dst=/solution \
--mount type=bind,src="${PWD}",dst=/output \
--mount type=tmpfs,dst=/tmp \
"${image}" "${slug}" /solution /output
jq -re '.message // .status' "${PWD}/results.json"
jq -e '.status == "pass"' "${PWD}/results.json" >/dev/null 2>&1
}

verify_exercise() {
local dir
local slug
local tmp_dir
dir=$(realpath "${1}")
slug=$(basename "${dir}")
tmp_dir=$(mktemp -d -t "exercism-verify-${slug}-XXXXX")

echo "Verifying ${slug} exercise..."

(
trap 'rm -rf "$tmp_dir"' EXIT # remove tempdir when subshell ends
cp -r "${dir}/." "${tmp_dir}"
cd "${tmp_dir}"

copy_example_or_examplar_to_solution
run_tests "${slug}"
)
}

verify_exercises() {
local exercise_slug
exercise_slug="${1}"

shopt -s nullglob
count=0
for exercise_dir in ./exercises/{concept,practice}/${exercise_slug}/; do
if [[ -d "${exercise_dir}" ]]; then
verify_exercise "${exercise_dir}"
((++count))
fi
done
((count > 0)) || die 'no matching exercises found!'
}

image=''
while getopts :i: opt; do
case $opt in
i) image=$OPTARG ;;
?) echo >&2 "Unknown option: -$OPTARG"; exit 1 ;;
esac
done
shift "$((OPTIND - 1))"

if [[ -z "${image}" ]]; then
image="exercism/zig-test-runner"
pull_docker_image
fi

exercise_slug="${1:-*}"
verify_exercises "${exercise_slug}"