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 136009f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 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
35 changes: 30 additions & 5 deletions tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,18 +80,19 @@ 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 {
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
}

Expand All @@ -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<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 136009f

Please sign in to comment.