|
| 1 | +import std/[os, osproc, strformat, strutils] |
| 2 | + |
| 3 | +iterator walkExerciseDirs: string = |
| 4 | + const repoRootDir = currentSourcePath().parentDir().parentDir() |
| 5 | + for exerciseKind in ["concept", "practice"]: |
| 6 | + for exerciseDir in walkDirs(repoRootDir / "exercises" / exerciseKind / "*"): |
| 7 | + yield exerciseDir |
| 8 | + |
| 9 | +proc checkStubs: seq[string] = |
| 10 | + ## Compiles and runs the test file for every exercise, using the user-facing |
| 11 | + ## solution stub. |
| 12 | + ## |
| 13 | + ## Returns the exercise slugs for which the corresponding test file either: |
| 14 | + ## |
| 15 | + ## - compiles with an error |
| 16 | + ## |
| 17 | + ## - runs without an error (the solution stub is supposed to fail the tests) |
| 18 | + result = @[] |
| 19 | + stderr.writeLine "Checking stubs..." |
| 20 | + for exerciseDir in walkExerciseDirs(): |
| 21 | + let slug = exerciseDir.lastPathPart() |
| 22 | + let testPath = exerciseDir / &"test_{slug.replace('-', '_')}.nim" |
| 23 | + stderr.writeLine &"{slug}" |
| 24 | + const nimOptions = "--hints:off --usenimcache --filenames:canonical " & |
| 25 | + "--spellSuggest:0 --styleCheck:error" |
| 26 | + let (outpCompile, errCompile) = execCmdEx(&"nim c {nimOptions} {testPath}") |
| 27 | + if errCompile == 0: |
| 28 | + let (outpRun, errRun) = execCmdEx(&"nim r --hints:off {testPath}") |
| 29 | + if errRun == 0: |
| 30 | + stderr.write outpRun |
| 31 | + stderr.writeLine &"Error: the {slug} stub passed the tests\n" |
| 32 | + result.add slug |
| 33 | + else: |
| 34 | + stderr.writeLine outpCompile |
| 35 | + result.add slug |
| 36 | + |
| 37 | +proc main = |
| 38 | + let errorSlugs = checkStubs() |
| 39 | + if errorSlugs.len > 0: |
| 40 | + let msg = fmt""" |
| 41 | +
|
| 42 | + Error: there were {errorSlugs.len} exercises with a problematic stub: |
| 43 | + {errorSlugs.join(", ")}""".unindent() |
| 44 | + echo msg |
| 45 | + quit 1 |
| 46 | + else: |
| 47 | + const msg = """ |
| 48 | +
|
| 49 | + Success. Every exercise has a test file and stub that: |
| 50 | + - compiles without error |
| 51 | + - runs with an error (we want the stub to fail the tests)""".unindent() |
| 52 | + echo msg |
| 53 | + |
| 54 | +when isMainModule: |
| 55 | + main() |
0 commit comments