-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thomas Farr <[email protected]>
- Loading branch information
Showing
10 changed files
with
292 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import { type ChapterEvaluation, type Evaluation, Result, type StoryEvaluation } from './types/eval.types' | ||
import { overall_result } from './helpers' | ||
import * as ansi from './Ansi' | ||
|
||
export interface ResultLogger { | ||
log: (evaluation: StoryEvaluation) => void | ||
} | ||
|
||
export class NoOpResultLogger implements ResultLogger { | ||
log (_: StoryEvaluation): void { } | ||
} | ||
|
||
export class ConsoleResultLogger implements ResultLogger { | ||
private readonly _tab_width: number | ||
private readonly _verbose: boolean | ||
|
||
constructor (tab_width: number = 4, verbose: boolean = false) { | ||
this._tab_width = tab_width | ||
this._verbose = verbose | ||
} | ||
|
||
log (evaluation: StoryEvaluation): void { | ||
this.#log_story(evaluation) | ||
this.#log_chapters(evaluation.prologues ?? [], 'PROLOGUES') | ||
this.#log_chapters(evaluation.chapters ?? [], 'CHAPTERS') | ||
this.#log_chapters(evaluation.epilogues ?? [], 'EPILOGUES') | ||
console.log('\n') | ||
} | ||
|
||
#log_story ({ result, full_path, description, display_path }: StoryEvaluation): void { | ||
this.#log_evaluation({ result, message: full_path }, ansi.cyan(ansi.b(description ?? display_path))) | ||
} | ||
|
||
#log_chapters (evaluations: ChapterEvaluation[], title: string): void { | ||
if (evaluations.length === 0) return | ||
const result = overall_result(evaluations.map(e => e.overall)) | ||
if (!this._verbose && (result === Result.SKIPPED || result === Result.PASSED)) return | ||
this.#log_evaluation({ result }, title, this._tab_width) | ||
for (const evaluation of evaluations) this.#log_chapter(evaluation) | ||
} | ||
|
||
#log_chapter (chapter: ChapterEvaluation): void { | ||
this.#log_evaluation(chapter.overall, ansi.i(chapter.title), this._tab_width * 2) | ||
this.#log_parameters(chapter.request?.parameters ?? {}) | ||
this.#log_request_body(chapter.request?.request_body) | ||
this.#log_status(chapter.response?.status) | ||
this.#log_payload(chapter.response?.payload) | ||
} | ||
|
||
#log_parameters (parameters: Record<string, Evaluation>): void { | ||
if (Object.keys(parameters).length === 0) return | ||
const result = overall_result(Object.values(parameters)) | ||
this.#log_evaluation({ result }, 'PARAMETERS', this._tab_width * 3) | ||
for (const [name, evaluation] of Object.entries(parameters)) { | ||
this.#log_evaluation(evaluation, name, this._tab_width * 4) | ||
} | ||
} | ||
|
||
#log_request_body (evaluation: Evaluation | undefined): void { | ||
if (evaluation == null) return | ||
this.#log_evaluation(evaluation, 'REQUEST BODY', this._tab_width * 3) | ||
} | ||
|
||
#log_status (evaluation: Evaluation | undefined): void { | ||
if (evaluation == null) return | ||
this.#log_evaluation(evaluation, 'RESPONSE STATUS', this._tab_width * 3) | ||
} | ||
|
||
#log_payload (evaluation: Evaluation | undefined): void { | ||
if (evaluation == null) return | ||
this.#log_evaluation(evaluation, 'RESPONSE PAYLOAD', this._tab_width * 3) | ||
} | ||
|
||
#log_evaluation (evaluation: Evaluation, title: string, prefix: number = 0): void { | ||
const result = ansi.padding(this.#result(evaluation.result), 0, prefix) | ||
const message = evaluation.message != null ? `${ansi.gray('(' + evaluation.message + ')')}` : '' | ||
console.log(`${result} ${title} ${message}`) | ||
if (evaluation.error && this._verbose) { | ||
console.log('-'.repeat(100)) | ||
console.error(evaluation.error) | ||
console.log('-'.repeat(100)) | ||
} | ||
} | ||
|
||
#result (r: Result): string { | ||
const text = ansi.padding(r, 7) | ||
switch (r) { | ||
case Result.PASSED: return ansi.green(text) | ||
case Result.SKIPPED: return ansi.yellow(text) | ||
case Result.FAILED: return ansi.magenta(text) | ||
case Result.ERROR: return ansi.red(text) | ||
default: return ansi.gray(text) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
import type StoryEvaluator from './StoryEvaluator' | ||
import { type StoryFile } from './StoryEvaluator' | ||
import fs from 'fs' | ||
import { type Story } from './types/story.types' | ||
import { read_yaml } from '../../helpers' | ||
import { Result, type StoryEvaluation } from './types/eval.types' | ||
import { type ResultLogger } from './ResultLogger' | ||
import { basename, resolve } from 'path' | ||
|
||
export default class TestRunner { | ||
private readonly _story_evaluator: StoryEvaluator | ||
private readonly _result_logger: ResultLogger | ||
|
||
constructor (story_evaluator: StoryEvaluator, result_logger: ResultLogger) { | ||
this._story_evaluator = story_evaluator | ||
this._result_logger = result_logger | ||
} | ||
|
||
async run (story_path: string, dry_run: boolean = false): Promise<{ evaluations: StoryEvaluation[], failed: boolean }> { | ||
let failed = false | ||
const story_files = this.#sort_story_files(this.#collect_story_files(resolve(story_path), '', '')) | ||
const evaluations: StoryEvaluation[] = [] | ||
for (const story_file of story_files) { | ||
const evaluation = await this._story_evaluator.evaluate(story_file, dry_run) | ||
evaluations.push(evaluation) | ||
this._result_logger.log(evaluation) | ||
if ([Result.ERROR, Result.FAILED].includes(evaluation.result)) failed = true | ||
} | ||
return { evaluations, failed } | ||
} | ||
|
||
#collect_story_files (folder: string, file: string, prefix: string): StoryFile[] { | ||
const path = file === '' ? folder : `${folder}/${file}` | ||
const next_prefix = prefix === '' ? file : `${prefix}/${file}` | ||
if (fs.statSync(path).isFile()) { | ||
const story: Story = read_yaml(path) | ||
return [{ | ||
display_path: next_prefix === '' ? basename(path) : next_prefix, | ||
full_path: path, | ||
story | ||
}] | ||
} else { | ||
return fs.readdirSync(path).flatMap(next_file => { | ||
return this.#collect_story_files(path, next_file, next_prefix) | ||
}) | ||
} | ||
} | ||
|
||
#sort_story_files (story_files: StoryFile[]): StoryFile[] { | ||
return story_files.sort(({ display_path: a }, { display_path: b }) => { | ||
const a_depth = a.split('/').length | ||
const b_depth = b.split('/').length | ||
if (a_depth !== b_depth) return a_depth - b_depth | ||
return a.localeCompare(b) | ||
}) | ||
} | ||
} |
Oops, something went wrong.