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 checks for approaches and articles #704

Merged
merged 13 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
78 changes: 78 additions & 0 deletions src/lint/approaches_and_articles.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import std/[json, strformat, strutils]
import ".."/helpers
import "."/validators

type
DirKind = enum
dkApproaches = "approaches"
dkArticles = "articles"
Copy link
Member Author

Choose a reason for hiding this comment

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

DirKind isn't a great name. Do we have a name for something that is an approach or article?

Copy link
Member

Choose a reason for hiding this comment

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

They are part of the Dig Deeper section, maybe something like that?

Copy link
Member Author

Choose a reason for hiding this comment

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

We could do something like DigDeeperKind, but then dots in the enum string representations are bad.

If this is blocking, please suggest a name.

Copy link
Member

Choose a reason for hiding this comment

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

DigDeeperDirKind? That's bad too. This is not blocking

ee7 marked this conversation as resolved.
Show resolved Hide resolved

proc hasValidIntroduction(data: JsonNode, path: Path): bool =
ee7 marked this conversation as resolved.
Show resolved Hide resolved
const k = "introduction"
if hasObject(data, k, path):
let d = data[k]
let checks = [
hasArrayOfStrings(d, "authors", path, k, uniqueValues = true),
hasArrayOfStrings(d, "contributors", path, k, isRequired = false),
]
result = allTrue(checks)

proc isValidApproachOrArticle(data: JsonNode, context: string,
path: Path): bool =
if isObject(data, context, path):
let checks = [
hasString(data, "uuid", path, context, checkIsUuid = true),
hasString(data, "slug", path, context, checkIsKebab = true),
ee7 marked this conversation as resolved.
Show resolved Hide resolved
hasString(data, "title", path, context, maxLen = 255),
hasString(data, "blurb", path, context, maxLen = 280),
hasArrayOfStrings(data, "authors", path, context, uniqueValues = true),
hasArrayOfStrings(data, "contributors", path, context,
isRequired = false),
]
result = allTrue(checks)

proc isValidConfig(data: JsonNode, path: Path, dk: DirKind): bool =
if isObject(data, jsonRoot, path):
let checks = [
if dk == dkApproaches: hasValidIntroduction(data, path) else: true,
hasArrayOf(data, $dk, path, isValidApproachOrArticle, isRequired = false),
]
result = allTrue(checks)

proc isConfigMissingOrValid(dir: Path, dk: DirKind): bool =
result = true
let configPath = dir / &".{dk}" / "config.json"
if fileExists(configPath):
let j = parseJsonFile(configPath, result)
if j != nil:
if not isValidConfig(j, configPath, dk):
result = false

proc isEverySnippetValid(exerciseDir: Path, dk: DirKind): bool =
result = true
for dir in getSortedSubdirs(exerciseDir / &".{dk}"):
let snippetPath = block:
let ext = if dk == dkApproaches: "txt" else: "md"
dir / &"snippet.{ext}"
if fileExists(snippetPath):
let contents = readFile(snippetPath)
var numLines = 0
for line in contents.splitLines():
if not (line.startsWith("```") and dk == dkArticles):
ee7 marked this conversation as resolved.
Show resolved Hide resolved
inc numLines
dec numLines # Allow 8 lines with a final newline.
ee7 marked this conversation as resolved.
Show resolved Hide resolved
const maxNumLines = 8
if numLines > maxNumLines:
let msg = &"The file is {numLines} lines long, but it must be at " &
&"most {maxNumLines} lines long"
result.setFalseAndPrint(msg, snippetPath)

proc isEveryApproachAndArticleValid*(trackDir: Path): bool =
result = true
for exerciseKind in ["concept", "practice"]:
for exerciseDir in getSortedSubdirs(trackDir / "exercises" / exerciseKind):
for dk in DirKind:
if not isConfigMissingOrValid(exerciseDir, dk):
result = false
if not isEverySnippetValid(exerciseDir, dk):
result = false
5 changes: 3 additions & 2 deletions src/lint/lint.nim
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import std/[strformat, strutils]
import ".."/[cli, helpers]
import "."/[concept_exercises, concepts, docs, practice_exercises,
track_config, validators]
import "."/[approaches_and_articles, concept_exercises, concepts, docs,
practice_exercises, track_config, validators]

proc allChecksPass(trackDir: Path): bool =
## Returns true if all the linting checks pass for the track at `trackDir`.
Expand All @@ -16,6 +16,7 @@ proc allChecksPass(trackDir: Path): bool =
isEveryConceptConfigValid(trackDir),
isEveryConceptExerciseConfigValid(trackDir),
isEveryPracticeExerciseConfigValid(trackDir),
isEveryApproachAndArticleValid(trackDir),
sharedExerciseDocsExist(trackDir),
trackDocsExist(trackDir),
]
Expand Down