Skip to content

Commit

Permalink
lint: check file paths in exercise .meta/config.json files (#315)
Browse files Browse the repository at this point in the history
This commit implements the below checks for each exercise's
`.meta/config.json` file.

- The files listed in the "files.solution" array must exist
- The files listed in the "files.test" array must exist
- The files listed in the "files.exemplar" array must exist
- The files listed in the "files.example" array must exist

Co-authored-by: ee7 <[email protected]>
  • Loading branch information
ErikSchierboom and ee7 authored May 8, 2021
1 parent b90b71f commit d549f6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/lint/concept_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ import std/[json, os]
import ".."/helpers
import "."/validators

proc hasValidFiles(data: JsonNode, path: Path): bool =
proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
const k = "files"
if hasObject(data, k, path):
let d = data[k]
let checks = [
hasArrayOfStrings(d, "solution", path, k),
hasArrayOfStrings(d, "test", path, k),
hasArrayOfStrings(d, "exemplar", path, k),
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
hasArrayOfFiles(d, "test", path, k, exerciseDir),
hasArrayOfFiles(d, "exemplar", path, k, exerciseDir),
]
result = allTrue(checks)

proc isValidConceptExerciseConfig(data: JsonNode, path: Path): bool =
proc isValidConceptExerciseConfig(data: JsonNode;
path, exerciseDir: Path): bool =
if isObject(data, "", path):
let checks = [
hasString(data, "blurb", path, maxLen = 350),
hasArrayOfStrings(data, "authors", path, uniqueValues = true),
hasArrayOfStrings(data, "contributors", path, isRequired = false,
uniqueValues = true),
hasValidFiles(data, path),
hasValidFiles(data, path, exerciseDir),
hasArrayOfStrings(data, "forked_from", path, isRequired = false,
uniqueValues = true),
hasString(data, "language_versions", path, isRequired = false),
Expand All @@ -36,7 +37,7 @@ proc isEveryConceptExerciseConfigValid*(trackDir: Path): bool =
let configPath = exerciseDir / ".meta" / "config.json"
let j = parseJsonFile(configPath, result)
if j != nil:
if not isValidConceptExerciseConfig(j, configPath):
if not isValidConceptExerciseConfig(j, configPath, exerciseDir):
result = false

proc conceptExerciseDocsExist*(trackDir: Path): bool =
Expand Down
16 changes: 8 additions & 8 deletions src/lint/practice_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import std/[json, os]
import ".."/helpers
import "."/validators

proc hasValidFiles(data: JsonNode, path: Path): bool =
proc hasValidFiles(data: JsonNode; path, exerciseDir: Path): bool =
const k = "files"
if hasObject(data, k, path):
let d = data[k]
let checks = [
hasArrayOfStrings(d, "solution", path, k),
hasArrayOfStrings(d, "test", path, k),
hasArrayOfStrings(d, "example", path, k),
hasArrayOfFiles(d, "solution", path, k, exerciseDir),
hasArrayOfFiles(d, "test", path, k, exerciseDir),
hasArrayOfFiles(d, "example", path, k, exerciseDir),
]
result = allTrue(checks)

proc isValidPracticeExerciseConfig(data: JsonNode, path: Path): bool =
proc isValidPracticeExerciseConfig(data: JsonNode;
path, exerciseDir: Path): bool =
if isObject(data, "", path):
# TODO: Enable the `files` checks after the tracks have had some time to update.
let checks = [
hasString(data, "blurb", path, maxLen = 350),
hasArrayOfStrings(data, "authors", path, isRequired = false,
uniqueValues = true),
hasArrayOfStrings(data, "contributors", path, isRequired = false,
uniqueValues = true),
if false: hasValidFiles(data, path) else: true,
hasValidFiles(data, path, exerciseDir),
hasString(data, "language_versions", path, isRequired = false),
]
result = allTrue(checks)
Expand All @@ -37,7 +37,7 @@ proc isEveryPracticeExerciseConfigValid*(trackDir: Path): bool =
let configPath = exerciseDir / ".meta" / "config.json"
let j = parseJsonFile(configPath, result)
if j != nil:
if not isValidPracticeExerciseConfig(j, configPath):
if not isValidPracticeExerciseConfig(j, configPath, exerciseDir):
result = false

proc practiceExerciseDocsExist*(trackDir: Path): bool =
Expand Down

0 comments on commit d549f6d

Please sign in to comment.