Skip to content

Commit

Permalink
lint: allow overlap of files.example and files.exemplar values
Browse files Browse the repository at this point in the history
This commit allows the values in the `files.example` and `files.exemplar`
keys in the `config.json` to overlap, whilst still disallowing the other
keys' values to overlap.
  • Loading branch information
ErikSchierboom committed Feb 24, 2022
1 parent 70f1252 commit fea7bcc
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/lint/track_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,16 @@ type

Concepts* = seq[Concept]

FilePatterns* = object
solution*: seq[string]
test*: seq[string]
example*: seq[string]
exemplar*: seq[string]
editor*: seq[string]

TrackConfig* = object
slug*: string
files*: FilePatterns
exercises*: Exercises
concepts*: Concepts

Expand Down Expand Up @@ -655,6 +664,32 @@ proc checkExerciseSlugsAndForegone(exercises: Exercises; b: var bool;
"but there is an implemented exercise with that slug"
b.setFalseAndPrint(msg, path)

proc checkFilePatternsOverlap(filePatterns: FilePatterns; trackSlug: string,
b: var bool; path: Path) =
const uniqueFilePatternCombinations = [
("solution", "test"),
("solution", "example"),
("solution", "exemplar"),
("solution", "editor"),
("test", "example"),
("test", "exemplar"),
("test", "editor"),
("editor", "example"),
("editor", "exemplar"),
]

var seenFilePatterns = initTable[string, HashSet[string]](250)
for key, patterns in filePatterns.fieldPairs:
seenFilePatterns[key] = patterns.toHashSet

for (key1, key2) in uniqueFilePatternCombinations:
let duplicatePatterns = seenFilePatterns[key1] * seenFilePatterns[key2]
for duplicatePattern in duplicatePatterns:
let msg =
&"The values in the `files.{key1}` and `files.{key2}` keys must not overlap, " &
&"but the {q duplicatePattern} value appears in both"
b.setFalseAndPrint(msg, path)

proc satisfiesSecondPass(trackConfigContents: string; path: Path): bool =
## Returns `true` if `trackConfigContents` satisfies some checks.
##
Expand Down Expand Up @@ -684,6 +719,7 @@ proc satisfiesSecondPass(trackConfigContents: string; path: Path): bool =
checkExercisesPCP(conceptExercises, result, path)
checkExercisesPCP(practiceExercises, result, path)
checkExerciseSlugsAndForegone(exercises, result, path)
checkFilePatternsOverlap(trackConfig.files, trackConfig.slug, result, path)

proc getExerciseSlugs(data: JsonNode; k: string): HashSet[string] =
result = initHashSet[string]()
Expand Down
6 changes: 0 additions & 6 deletions src/lint/validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ func list(a: SomeSet[string]; prefix = ""; suffix = ""): string =
result.add ", and "

var seenUuids = initHashSet[string](250)
var seenFilePatterns = initHashSet[string](250)

proc isString*(data: JsonNode; key: string; path: Path; context: string;
isRequired = true; allowed = emptySetOfStrings;
Expand Down Expand Up @@ -282,11 +281,6 @@ proc isString*(data: JsonNode; key: string; path: Path; context: string;
"lowercased version 4 UUID"
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
elif checkIsFilesPattern:
if seenFilePatterns.containsOrIncl(s):
let msg =
&"A {format(context, key)} value is {q s}, which is not a unique " &
"`files` entry"
result.setFalseAndPrint(msg, path, annotation = errorAnnotation)
if isFilesPattern(s):
if "%{" in s and "}" notin s:
let msg =
Expand Down

0 comments on commit fea7bcc

Please sign in to comment.