Skip to content

Commit

Permalink
lint(validators): extract required file checks to modules
Browse files Browse the repository at this point in the history
The validation that checked if required files were present was done in the `lint.nim` file. To better separate concerns, these file checks have been moved to their own modules.

The validation helper proc to check for required files has been moved to the validators module.
  • Loading branch information
ErikSchierboom committed Mar 17, 2021
1 parent 9344646 commit 81df2d5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 57 deletions.
14 changes: 14 additions & 0 deletions src/lint/concept_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,17 @@ proc isEveryConceptExerciseConfigValid*(trackDir: string): bool =
continue
if not isValidConceptExerciseConfig(j, configPath):
result = false

proc conceptExerciseFilesExist*(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/exercises/concept` has the
## required files.
const
requiredConceptExerciseFiles = [
".docs" / "hints.md",
".docs" / "instructions.md",
".docs" / "introduction.md",
".meta" / "config.json",
]

let conceptExercisesDir = trackDir / "exercises" / "concept"
result = subdirsContain(conceptExercisesDir, requiredConceptExerciseFiles)
15 changes: 15 additions & 0 deletions src/lint/concepts.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import std/os
import "."/validators

proc conceptFilesExist*(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/concepts` has the required
## files.
const
requiredConceptFiles = [
"about.md",
"introduction.md",
"links.json",
]

let conceptsDir = trackDir / "concepts"
result = subdirsContain(conceptsDir, requiredConceptFiles)
58 changes: 2 additions & 56 deletions src/lint/lint.nim
Original file line number Diff line number Diff line change
@@ -1,59 +1,5 @@
import std/os
import ".."/[cli, helpers]
import "."/[concept_exercises, practice_exercises, track_config]

proc subdirsContain(dir: string, files: openArray[string]): bool =
## Returns `true` if every file in `files` exists in every subdirectory of
## `dir`.
##
## Returns `true` if `dir` does not exist or has no subdirectories.
result = true

if dirExists(dir):
for subdir in getSortedSubdirs(dir):
for file in files:
let path = subdir / file
if not fileExists(path):
result.setFalseAndPrint("Missing file", path)

proc conceptExerciseFilesExist(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/exercises/concept` has the
## required files.
const
conceptExerciseFiles = [
".docs" / "hints.md",
".docs" / "instructions.md",
".docs" / "introduction.md",
".meta" / "config.json",
]

let conceptDir = trackDir / "exercises" / "concept"
result = subdirsContain(conceptDir, conceptExerciseFiles)

proc practiceExerciseFilesExist(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/exercises/practice` has the
## required files.
const
requiredPracticeExerciseFiles = [
".docs" / "instructions.md",
".meta" / "config.json",
]

let practiceExercisesDir = trackDir / "exercises" / "practice"
result = subdirsContain(practiceExercisesDir, requiredPracticeExerciseFiles)

proc conceptFilesExist(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/concepts` has the required
## files.
const
conceptFiles = [
"about.md",
"introduction.md",
"links.json",
]

let conceptsDir = trackDir / "concepts"
result = subdirsContain(conceptsDir, conceptFiles)
import ".."/cli
import "."/[concept_exercises, concepts, practice_exercises, track_config]

proc lint*(conf: Conf) =
echo "The lint command is under development.\n" &
Expand Down
12 changes: 12 additions & 0 deletions src/lint/practice_exercises.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ proc isEveryPracticeExerciseConfigValid*(trackDir: string): bool =
continue
if not isValidPracticeExerciseConfig(j, configPath):
result = false

proc practiceExerciseFilesExist*(trackDir: string): bool =
## Returns true if every subdirectory in `trackDir/exercises/practice` has the
## required files.
const
requiredPracticeExerciseFiles = [
".docs" / "instructions.md",
".meta" / "config.json",
]

let practiceExercisesDir = trackDir / "exercises" / "practice"
result = subdirsContain(practiceExercisesDir, requiredPracticeExerciseFiles)
16 changes: 15 additions & 1 deletion src/lint/validators.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[json, strutils]
import std/[json, os, strutils]
import ".."/helpers

func q(s: string): string =
Expand Down Expand Up @@ -97,3 +97,17 @@ proc checkInteger*(data: JsonNode; key, path: string; isRequired = true): bool =
result.setFalseAndPrint("Not an integer: " & q(key) & ": " & $data[key], path)
elif isRequired:
result.setFalseAndPrint("Missing key: " & q(key), path)

proc subdirsContain*(dir: string, files: openArray[string]): bool =
## Returns `true` if every file in `files` exists in every subdirectory of
## `dir`.
##
## Returns `true` if `dir` does not exist or has no subdirectories.
result = true

if dirExists(dir):
for subdir in getSortedSubdirs(dir):
for file in files:
let path = subdir / file
if not fileExists(path):
result.setFalseAndPrint("Missing file", path)

0 comments on commit 81df2d5

Please sign in to comment.