-
-
Notifications
You must be signed in to change notification settings - Fork 59
bin/verify-exercises-in-docker #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
keiravillekode
wants to merge
5
commits into
exercism:main
Choose a base branch
from
keiravillekode:verify-exercises-in-docker
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67a4344
bin/verify-exercises-in-docker
keiravillekode 90e0ae8
support bash 3
keiravillekode 2c273ed
omit bash version check
keiravillekode 3cf977d
exemplar not examplar
keiravillekode 156f9bf
verify-exercises-in-docker.ps1
keiravillekode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| #!/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 | ||
|
|
||
| # Example: verify all exercises against specified test runner | ||
| # bin/verify-exercises-in-docker -i my-local-image | ||
|
|
||
| set -e | ||
| shopt -s nullglob | ||
|
|
||
| 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." | ||
| } | ||
|
|
||
| copy_example_or_exemplar_to_solution() { | ||
| local dir="${1}" | ||
| jq -r '[.files.solution, .files.exemplar // .files.example] | transpose | map(select(.[0] and .[1]))[][]' "${dir}/.meta/config.json" \ | ||
| | while read -r dst; read -r src; do | ||
| cp "${dir}/${src}" "${dir}/${dst}" | ||
| done | ||
| } | ||
|
|
||
| run_tests() { | ||
| local slug="${1}" dir="${2}" | ||
| local -a docker_args | ||
|
|
||
| docker_args+=( --rm --network none ) | ||
| docker_args+=( --mount "type=bind,src=${dir},dst=/solution" ) | ||
| # /tmp needs to be a proper volume to run the compiled executable; tmpfs is not executable. | ||
| docker_args+=( --mount "type=volume,dst=/tmp" ) | ||
|
|
||
| # /solution is used both as the location to read the code from and as a destination for the results.json file. | ||
| docker run "${docker_args[@]}" "${image}" "${slug}" /solution /solution | ||
| jq -e '.status == "pass"' "${dir}/results.json" >/dev/null 2>&1 | ||
| } | ||
|
|
||
| verify_exercise() { | ||
| local dir slug tmpdir | ||
| dir="${1%/}" | ||
| slug="${dir##*/}" | ||
| tmpdir="$(mktemp -d -t "exercism-verify-${slug}-XXXXX")" | ||
|
|
||
| echo "Verifying ${slug} exercise..." | ||
| ( | ||
| trap 'rm -rf "${tmpdir}"' EXIT # remove tempdir when subshell ends | ||
| cp -r "${dir}/." "${tmpdir}" || exit | ||
| copy_example_or_exemplar_to_solution "${tmpdir}" | ||
| run_tests "${slug}" "${tmpdir}" || { cat "${tmpdir}/results.json"; exit 1; } | ||
| ) | ||
| } | ||
|
|
||
| verify_exercises() { | ||
| local -a exercises | ||
| local parent path | ||
| if (( $# )); then | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern looks familiar to me 😆 |
||
| for slug; do | ||
| for parent in concept practice; do | ||
| path="./exercises/${parent}/${slug}" | ||
| [[ -d "${path}" ]] && exercises+=( "${path}" ) | ||
| done | ||
| done | ||
| else | ||
| exercises=( ./exercises/{concept,practice}/* ) | ||
| fi | ||
| (( ${#exercises[@]} )) || die "No matching exercises found" | ||
|
|
||
| rc=0 | ||
| for exercise_dir in "${exercises[@]}"; do | ||
| verify_exercise "${exercise_dir}" || rc=$? | ||
| done | ||
| return "$rc" | ||
| } | ||
|
|
||
|
|
||
| required_tool docker | ||
| required_tool jq | ||
|
|
||
| image='' | ||
| while getopts i: opt; do | ||
| case "${opt}" in | ||
| i) image="${OPTARG}" ;; | ||
| ?) die "Unknown option: -$OPTARG" ;; | ||
| esac | ||
| done | ||
| shift "$((OPTIND - 1))" | ||
|
|
||
| if [[ -z "${image}" ]]; then | ||
| image="exercism/racket-test-runner" | ||
| docker pull "${image}" || | ||
| die "docker pull ${image} failed. Check the test runner docs at https://exercism.org/docs/building/tooling/test-runners for more information." | ||
| fi | ||
|
|
||
| verify_exercises "$@" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # 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.ps1 | ||
|
|
||
| # Example: verify single exercise in Docker | ||
| # .\bin\verify-exercises-in-docker.ps1 two-fer | ||
|
|
||
| # Example: verify all exercises against specified test runner | ||
| # .\bin\verify-exercises-in-docker.ps1 -Image my-local-image | ||
|
|
||
| param( | ||
| [Parameter(Position=0, ValueFromRemainingArguments)] | ||
| [string[]]$Slugs, | ||
| [string]$Image = "" | ||
| ) | ||
|
|
||
| $ErrorActionPreference = "Stop" | ||
|
|
||
| Function Test-RequiredTool([string]$Name) { | ||
| if (-not (Get-Command $Name -ErrorAction SilentlyContinue)) { | ||
| Write-Error "${Name} is required but not installed. Please install it and make sure it's in your PATH." | ||
| } | ||
| } | ||
|
|
||
| Function Copy-ReferenceSolution([string]$Dir) { | ||
| $config = Get-Content (Join-Path $Dir ".meta" "config.json") | ConvertFrom-Json | ||
| $solutions = @($config.files.solution) | ||
| $sources = @(if ($config.files.exemplar) { $config.files.exemplar } else { $config.files.example }) | ||
| for ($i = 0; $i -lt $solutions.Count; $i++) { | ||
| $dst = Join-Path $Dir $solutions[$i] | ||
| $src = Join-Path $Dir $sources[$i] | ||
| Copy-Item -Path $src -Destination $dst -Force | ||
| } | ||
| } | ||
|
|
||
| Function Invoke-Tests([string]$Slug, [string]$Dir) { | ||
| $dockerArgs = @( | ||
| "run", "--rm", "--network", "none", | ||
| "--mount", "type=bind,src=${Dir},dst=/solution", | ||
| "--mount", "type=volume,dst=/tmp", | ||
| $Image, $Slug, "/solution", "/solution" | ||
| ) | ||
| docker @dockerArgs | Out-Null | ||
| $resultsPath = Join-Path $Dir "results.json" | ||
| $results = Get-Content $resultsPath | ConvertFrom-Json | ||
| return $results.status -eq "pass" | ||
| } | ||
|
|
||
| Function Test-Exercise([string]$ExerciseDir) { | ||
| $dir = (Resolve-Path $ExerciseDir).Path | ||
| $slug = Split-Path $dir -Leaf | ||
| $tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "exercism-verify-${slug}-$([System.IO.Path]::GetRandomFileName())" | ||
| New-Item -ItemType Directory -Path $tmpDir | Out-Null | ||
|
|
||
| Write-Host "Verifying ${slug} exercise..." | ||
| try { | ||
| Copy-Item -Path (Join-Path $dir "*") -Destination $tmpDir -Recurse -Force | ||
| Copy-ReferenceSolution $tmpDir | ||
| $passed = Invoke-Tests $slug $tmpDir | ||
| if (-not $passed) { | ||
| Get-Content (Join-Path $tmpDir "results.json") | Write-Host | ||
| return $false | ||
| } | ||
| return $true | ||
| } | ||
| finally { | ||
| Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue | ||
| } | ||
| } | ||
|
|
||
| # Main | ||
|
|
||
| Test-RequiredTool "docker" | ||
| Test-RequiredTool "jq" | ||
|
|
||
| if (-not $Image) { | ||
| $Image = "exercism/racket-test-runner" | ||
| docker pull $Image | ||
| if ($LASTEXITCODE -ne 0) { | ||
| Write-Error "docker pull ${Image} failed. Check the test runner docs at https://exercism.org/docs/building/tooling/test-runners for more information." | ||
| } | ||
| } | ||
|
|
||
| $exercises = @() | ||
| if ($Slugs.Count -gt 0) { | ||
| foreach ($slug in $Slugs) { | ||
| foreach ($parent in "concept", "practice") { | ||
| $path = Join-Path "." "exercises" $parent $slug | ||
| if (Test-Path $path -PathType Container) { | ||
| $exercises += $path | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| foreach ($parent in "concept", "practice") { | ||
| $parentDir = Join-Path "." "exercises" $parent | ||
| if (Test-Path $parentDir -PathType Container) { | ||
| $exercises += Get-ChildItem -Path $parentDir -Directory | ForEach-Object { $_.FullName } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($exercises.Count -eq 0) { | ||
| Write-Error "No matching exercises found" | ||
| } | ||
|
|
||
| $rc = 0 | ||
| foreach ($exerciseDir in $exercises) { | ||
| $passed = Test-Exercise $exerciseDir | ||
| if (-not $passed) { $rc = 1 } | ||
| } | ||
| exit $rc |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
+=needsbash 3.1+I think Mac uses 3.2