From b90b71f4cdba69d0cbd6395123befc0c47d2bc7e Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Sat, 8 May 2021 23:34:42 +0200 Subject: [PATCH] lint(validators): support checking that files exist (#314) 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 <45465154+ee7@users.noreply.github.com> --- src/lint/validators.nim | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lint/validators.nim b/src/lint/validators.nim index 77b0c5b1..b9c16f60 100644 --- a/src/lint/validators.nim +++ b/src/lint/validators.nim @@ -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.} @@ -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.}