Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint: add initial linting of practice exercises #219

Merged
merged 3 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/lint/lint.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ".."/cli
import "."/[concept_exercises, concepts, track_config]
import "."/[concept_exercises, concepts, practice_exercises, track_config]

proc lint*(conf: Conf) =
echo "The lint command is under development.\n" &
Expand All @@ -9,16 +9,20 @@ proc lint*(conf: Conf) =
let trackDir = conf.trackDir
let b1 = isTrackConfigValid(trackDir)
let b2 = conceptExerciseFilesExist(trackDir)
let b3 = conceptFilesExist(trackDir)
let b4 = isEveryConceptExerciseConfigValid(trackDir)
let b3 = practiceExerciseFilesExist(trackDir)
let b4 = conceptFilesExist(trackDir)
let b5 = isEveryConceptExerciseConfigValid(trackDir)
let b6 = isEveryPracticeExerciseConfigValid(trackDir)

if b1 and b2 and b3 and b4:
if b1 and b2 and b3 and b4 and b5 and b6:
echo """
Basic linting finished successfully:
- config.json exists and is valid JSON
- config.json has these valid fields: language, slug, active, blurb, version, tags
- Every concept has the required .md files and links.json file
- Every concept exercise has the required .md files and a .meta/config.json file
- Every concept exercise .meta/config.json file is valid"""
- Every concept exercise .meta/config.json file is valid
- Every practice exercise has the required .md files and a .meta/config.json file
- Every practice exercise .meta/config.json file is valid"""
else:
quit(1)
70 changes: 70 additions & 0 deletions src/lint/practice_exercises.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import std/[json, os]
import ".."/helpers
import "."/validators

proc isValidAuthorOrContributor(data: JsonNode, context: string, path: string): bool =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is currently duplicated between the practice_exercises and concept_exercises modules. We could extract it to some shared module.

if isObject(data, context, path):
result = true
if not checkString(data, "github_username", path):
result = false
if not checkString(data, "exercism_username", path, isRequired = false):
result = false

proc checkFiles(data: JsonNode, context, path: string): bool =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is virtually identical to the corresponding concept exercise code, except for exemplar being named example here. We could extract this functionality into a shared helper.

result = true
if hasObject(data, context, path):
if not checkArrayOfStrings(data, context, "solution", path):
result = false
if not checkArrayOfStrings(data, context, "test", path):
result = false
if not checkArrayOfStrings(data, context, "example", path):
result = false
else:
result = false

proc isValidPracticeExerciseConfig(data: JsonNode, path: string): bool =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again, this logic is almost identical to the concept exercises' logic. For now, the only difference is that there is no forked_from property for practice exercises. There will be more difference later when more practice exercise checks are implemented. We could consider extracting the shared logic to a helper.

if isObject(data, "root", path):
result = true
# Temporarily disable the checking of authors as we'll be doing bulk PRs
# to pre-populate this field for all tracks
# if not checkArrayOf(data, "authors", path, isValidAuthorOrContributor):
# result = false
if not checkArrayOf(data, "contributors", path, isValidAuthorOrContributor,
isRequired = false):
result = false
# Temporarily disable the checking of the files to give tracks the chance
# to update this manually
# if not checkFiles(data, "files", path):
# result = false
if not checkString(data, "language_versions", path, isRequired = false):
result = false

proc isEveryPracticeExerciseConfigValid*(trackDir: string): bool =
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And again, this logic is very similar to the concept exercises logic. We could extract it to a helper method.

let practiceExercisesDir = trackDir / "exercises" / "practice"
result = true
# Return true even if the directory does not exist - this allows a future
# track to have concept exercises and no practice exercises.
if dirExists(practiceExercisesDir):
for exerciseDir in getSortedSubdirs(practiceExercisesDir):
let configPath = exerciseDir / ".meta" / "config.json"
if fileExists(configPath):
let j =
try:
parseFile(configPath)
except:
result.setFalseAndPrint("JSON parsing error", getCurrentExceptionMsg())
continue
if not isValidPracticeExerciseConfig(j, configPath):
result = false

ee7 marked this conversation as resolved.
Show resolved Hide resolved
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)