Skip to content

Commit

Permalink
lint(validators): support checking that files exist (#314)
Browse files Browse the repository at this point in the history
The `.meta/config.json` file of a Concept Exercise or a Practice
Exercise contains a `files` property, which contains the (relative)
paths to each file of the exercise. This commit adds a `hasArrayOfFiles`
procedure that:

1. Checks if the `files` value is a valid array of strings using
   `hasArrayOfStrings`.
2. Prepends each string value with the exercise directory, and produces
   an error message if a file does not exist at that path.

Co-authored-by: ee7 <[email protected]>
  • Loading branch information
ErikSchierboom and ee7 authored May 8, 2021
1 parent 2b70195 commit b90b71f
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/lint/validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,24 @@ proc hasArrayOfStrings*(data: JsonNode;
elif not isRequired:
result = true

proc fileExists(path: Path): bool {.borrow.}

proc hasArrayOfFiles*(data: JsonNode;
key: string;
path: Path;
context = "";
relativeToPath: Path): bool =
if hasArrayOfStrings(data, key, path, context):
result = true

for item in data[key]:
let relativeFilePath = item.getStr()
let absoluteFilePath = relativeToPath / relativeFilePath
if not fileExists(absoluteFilePath):
let msg = &"The {q context} array contains value " &
&"{q relativeFilePath} but {q $absoluteFilePath} could not be found"
result.setFalseAndPrint(msg, path)

type
ItemCall = proc(data: JsonNode; context: string; path: Path): bool {.nimcall.}

Expand Down Expand Up @@ -437,7 +455,6 @@ proc hasInteger*(data: JsonNode; key: string; path: Path; context = "";
result = true

proc dirExists*(path: Path): bool {.borrow.}
proc fileExists(path: Path): bool {.borrow.}
proc readFile(path: Path): string {.borrow.}
proc parseJson(s: Stream; filename: Path; rawIntegers = false;
rawFloats = false): JsonNode {.borrow.}
Expand Down

0 comments on commit b90b71f

Please sign in to comment.