From 136009fad4fd63937858bd1c62c0d3c90a742b0a 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 | 35 +++++++++++++++++++++++---- tools/src/tester/types/story.types.ts | 4 +++ 3 files changed, 38 insertions(+), 5 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..280d090ee 100644 --- a/tools/src/tester/StoryEvaluator.ts +++ b/tools/src/tester/StoryEvaluator.ts @@ -18,6 +18,7 @@ import * as semver from '../_utils/semver' import _ from 'lodash' import { ParsedChapter, ParsedStory } from './types/parsed_story.types' import { OutputReference } from './OutputReference' +import * as path from 'path' export default class StoryEvaluator { private readonly _chapter_evaluator: ChapterEvaluator @@ -79,7 +80,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,10 +88,11 @@ export default class StoryEvaluator { return result } - #chapter_warnings(story: ParsedStory): string[] | undefined { - const result = _.compact([ - this.#warning_if_mismatched_chapter_paths(story) - ]) + #chapter_warnings(story: ParsedStory, full_path: string): string[] | undefined { + const result = _.compact(_.flattenDeep([ + [this.#warning_if_mismatched_chapter_paths(story)], + this.#warning_if_invalid_path(story, full_path) + ])) return result.length > 0 ? result : undefined } @@ -107,6 +109,29 @@ 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) => { + if (chapter.warnings?.['multiple-paths-detected'] === false) return // not the path being tested + return chapter.path + })) + const normalized_paths = _.uniq(_.map(paths, (path) => + path + .replaceAll('/_plugins/', '') + .replaceAll(/\/\{[^}]+}/g, '') + .replaceAll('//', '/') + .replaceAll('_', '') + + '.yaml' + )) + + return _.compact(_.map(normalized_paths, (normalized_path) => { + if (!full_path.endsWith(normalized_path)) { + const relative_path = path.relative('.', full_path) + return `Invalid path detected, please move /${relative_path} to ${normalized_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