Skip to content

Commit

Permalink
Warn if file path is invalid.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Jan 14, 2025
1 parent 806b25f commit ad1c3af
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
4 changes: 4 additions & 0 deletions json_schemas/test_story.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,8 @@ definitions:
type: boolean
default: true
description: Enable/disable warnings about multiple paths being tested in the same story.
invalid-path-detected:
type: boolean
default: true
description: Enable/disable warnings about file paths that do not match paths tested in the story.
additionalProperties: false
27 changes: 24 additions & 3 deletions tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,18 @@ export default class StoryEvaluator {
result: overall_result(prologues.concat(chapters).concat(epilogues).concat(prologues).map(e => e.overall)),
}

const warnings = this.#chapter_warnings(story)
const warnings = this.#chapter_warnings(story, full_path)
if (warnings !== undefined) {
result.warnings = warnings
}

return result
}

#chapter_warnings(story: ParsedStory): string[] | undefined {
#chapter_warnings(story: ParsedStory, full_path: string): string[] | undefined {
const result = _.compact([
this.#warning_if_mismatched_chapter_paths(story)
this.#warning_if_mismatched_chapter_paths(story),
this.#warning_if_invalid_path(story, full_path)
])
return result.length > 0 ? result : undefined
}
Expand All @@ -107,6 +108,26 @@ export default class StoryEvaluator {
}
}

#warning_if_invalid_path(story: ParsedStory, full_path: string): string | undefined {
if (story.warnings?.['invalid-path-detected'] === false) return
const paths = _.compact(_.map(story.chapters, (chapter) => {
return chapter.path
}))
const normalized_paths = _.uniq(_.map(paths, (path) =>
path.replaceAll(/\/\{[^}]+}/g, '')
.replaceAll('//', '/')
.replaceAll('_', '')
+ '.yaml'
))

normalized_paths.forEach((path) => {
if (!full_path.endsWith(path)) {
const filename = full_path.split('/').pop()
return `Invalid path detected, please move ${filename} to ${path}.\n`
}
})
}

async #evaluate_chapters(chapters: ParsedChapter[], has_errors: boolean, dry_run: boolean, story_outputs: StoryOutputs, version?: string, distribution?: string): Promise<ChapterEvaluation[]> {
const evaluations: ChapterEvaluation[] = []
for (const chapter of chapters) {
Expand Down
4 changes: 4 additions & 0 deletions tools/src/tester/types/story.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export interface Warnings {
* Enable/disable warnings about multiple paths being tested in the same story.
*/
'multiple-paths-detected'?: boolean;
/**
* Enable/disable warnings about file paths that do not match paths tested in the story.
*/
'invalid-path-detected'?: boolean;
}
/**
* This interface was referenced by `Story`'s JSON-Schema
Expand Down

0 comments on commit ad1c3af

Please sign in to comment.