Skip to content

Commit

Permalink
sync(tests): improve error for invalid TOML (#614)
Browse files Browse the repository at this point in the history
A manually edited `tests.toml` file might contain invalid TOML. For
example:

    [2ee1d9af-1c43-416c-b41b-cefd7d4d2b2a]
    description = encode yes

where `encode yes` is invalid [1] because it is unquoted.

Before this commit, `configlet sync` would not handle the TOML parsing
exception:

    $ cd /tmp
    $ git clone --quiet https://github.com/exercism/common-lisp
    $ cd common-lisp
    $ git checkout f521b1fb0f04ffc2d5baa6cf0bba37c231cc1bd7
    $ bin/fetch-configlet
    $ bin/configlet sync --tests
    Updating cached 'problem-specifications' data...
    Checking exercises...
    parsetoml.nim(908)       parseValue
    Error: unhandled exception: /tmp/common-lisp/exercises/practice/affine-cipher/.meta/tests.toml(6:16) unexpected character "e" [TomlError]

With this commit, configlet still exits immediately, but handles the
exception:

    $ configlet sync --tests
    Updating cached 'problem-specifications' data...
    Checking exercises...

    Error: A 'tests.toml' file contains invalid TOML:
    /tmp/common-lisp/exercises/practice/affine-cipher/.meta/tests.toml(6:16) unexpected character "e"

    The expected 'tests.toml' format is documented in
    https://exercism.org/docs/building/configlet/sync#h-tests

Note that `configlet lint` does not yet lint `tests.toml` files, and so
invalid TOML does not yet cause `configlet lint` to indicate an error.

[1] https://toml.io/en/v1.0.0#string

Fixes: #613
  • Loading branch information
ee7 authored Jun 17, 2022
1 parent 5a2937d commit 3f4193f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/sync/tracks.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[algorithm, json, os, sets]
import std/[algorithm, json, os, sets, strformat, strutils]
import pkg/parsetoml
import ".."/cli

Expand Down Expand Up @@ -65,7 +65,21 @@ proc init(T: typedesc[PracticeExerciseTests], testsPath: string): T =
## included and excluded test case UUIDs.
result = PracticeExerciseTests.init()
if fileExists(testsPath):
let tests = parsetoml.parseFile(testsPath)
let tests =
try:
parsetoml.parseFile(testsPath)
except TomlError: # Note that `TomlError` inherits from `Defect`.
stderr.writeLine fmt"""
Error: A 'tests.toml' file contains invalid TOML:
{getCurrentExceptionMsg()}
The expected 'tests.toml' format is documented in
https://exercism.org/docs/building/configlet/sync#h-tests""".unindent()
quit 1
except CatchableError:
stderr.writeLine "Error: " & getCurrentExceptionMsg()
quit 1

for uuid, val in tests.getTable():
if val.hasKey("include"):
Expand Down

0 comments on commit 3f4193f

Please sign in to comment.