From ad1c3afba0d7e73953a251de206a8549f68f6605 Mon Sep 17 00:00:00 2001 From: dblock Date: Tue, 14 Jan 2025 15:50:18 -0500 Subject: [PATCH] Warn if file path is invalid. Signed-off-by: dblock --- json_schemas/test_story.schema.yaml | 4 ++++ tools/src/tester/StoryEvaluator.ts | 27 ++++++++++++++++++++++++--- tools/src/tester/types/story.types.ts | 4 ++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/json_schemas/test_story.schema.yaml b/json_schemas/test_story.schema.yaml index 01b742b51..a0bc7ba23 100644 --- a/json_schemas/test_story.schema.yaml +++ b/json_schemas/test_story.schema.yaml @@ -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 diff --git a/tools/src/tester/StoryEvaluator.ts b/tools/src/tester/StoryEvaluator.ts index a032f9369..22bd52b64 100644 --- a/tools/src/tester/StoryEvaluator.ts +++ b/tools/src/tester/StoryEvaluator.ts @@ -79,7 +79,7 @@ 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 } @@ -87,9 +87,10 @@ export default class StoryEvaluator { 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 } @@ -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 { const evaluations: ChapterEvaluation[] = [] for (const chapter of chapters) { diff --git a/tools/src/tester/types/story.types.ts b/tools/src/tester/types/story.types.ts index 2e48e38af..c0c40cbbc 100644 --- a/tools/src/tester/types/story.types.ts +++ b/tools/src/tester/types/story.types.ts @@ -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