Skip to content

Commit

Permalink
Use strong types for parsed stories.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <[email protected]>
  • Loading branch information
dblock committed Dec 11, 2024
1 parent 5024aec commit 7551636
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 52 deletions.
39 changes: 39 additions & 0 deletions tools/src/tester/StoryParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 _ from "lodash";
import { ChapterRequest, Story } from "./types/story.types";
import { ParsedChapterRequest, ParsedStory } from "./types/parsed_story.types";

export default class StoryParser {
static parse(story: Story): ParsedStory {
return {
...story,
chapters: this.#expand_chapters(story.chapters),
prologues: this.#expand_chapters(story.prologues),
epilogues: this.#expand_chapters(story.epilogues)
}
}

static #chapter_methods(methods: string[] | string): string[] {
return [...(Array.isArray(methods) ? methods : [methods])]
}

static #expand_chapters(chapters: ChapterRequest[] | undefined): ParsedChapterRequest[] {
if (chapters === undefined) return []
return _.flatMap(_.map(chapters, (chapter) => {
return _.map(this.#chapter_methods(chapter.method), (method) => {
return {
...chapter,
method
}
})
})) as ParsedChapterRequest[]
}
}
42 changes: 0 additions & 42 deletions tools/src/tester/StoryReader.ts

This file was deleted.

10 changes: 4 additions & 6 deletions tools/src/tester/TestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import type StoryEvaluator from './StoryEvaluator'
import { StoryEvaluations, type StoryFile } from './types/eval.types'
import fs from 'fs'
import { type Story } from './types/story.types'
import { read_yaml } from '../helpers'
import { Result } from './types/eval.types'
import { type ResultLogger } from './ResultLogger'
Expand All @@ -20,7 +19,7 @@ import { OpenSearchHttpClient } from 'OpenSearchHttpClient'
import * as ansi from './Ansi'
import _ from 'lodash'
import { Logger } from 'Logger'
import StoryReader from './StoryReader'
import StoryParser from './StoryParser'

export default class TestRunner {
private readonly _http_client: OpenSearchHttpClient
Expand Down Expand Up @@ -56,9 +55,8 @@ export default class TestRunner {
}

for (const story_file of story_files) {
var story_reader = new StoryReader(story_file)
this._logger.info(`Evaluating ${story_reader.display_path} ...`)
const evaluation = this._story_validator.validate(story_reader.story_file) ?? await this._story_evaluator.evaluate(story_reader.story_file, version, distribution, dry_run)
this._logger.info(`Evaluating ${story_file.display_path} ...`)
const evaluation = this._story_validator.validate(story_file) ?? await this._story_evaluator.evaluate(story_file, version, distribution, dry_run)
results.evaluations.push(evaluation)
this._result_logger.log(evaluation)
if ([Result.ERROR, Result.FAILED].includes(evaluation.result)) failed = true
Expand All @@ -79,7 +77,7 @@ export default class TestRunner {
if (file.startsWith('.') || file == 'docker-compose.yml' || file == 'Dockerfile' || file.endsWith('.py')) {
return []
} else if (fs.statSync(path).isFile()) {
const story: Story = read_yaml(path)
const story = StoryParser.parse(read_yaml(path))
return [{
display_path: next_prefix === '' ? basename(path) : next_prefix,
full_path: path,
Expand Down
4 changes: 2 additions & 2 deletions tools/src/tester/types/eval.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

import { type ChapterOutput } from '../ChapterOutput'
import { StoryOutputs } from '../StoryOutputs'
import type { Story } from "./story.types";
import { ParsedStory } from './parsed_story.types';

export interface StoryFile {
display_path: string
full_path: string
story: Story
story: ParsedStory
}

export interface Operation {
Expand Down
23 changes: 23 additions & 0 deletions tools/src/tester/types/parsed_story.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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 { Chapter, HttpMethod, Story, SupplementalChapter } from "./story.types"

export interface ParsedChapterRequest extends Chapter {
method: HttpMethod
}

export type ParsedChapter = ParsedChapterRequest & Chapter
export type ParsedSupplementalChapter = ParsedChapterRequest & SupplementalChapter

export interface ParsedStory extends Story {
chapters: ParsedChapter[]
prologues?: ParsedSupplementalChapter[]
epilogues?: ParsedSupplementalChapter[]
}
31 changes: 31 additions & 0 deletions tools/tests/tester/StoryReader.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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 { read_yaml } from "helpers";
import StoryParser from "../../src/tester/StoryParser";
import _ from "lodash";

describe('StoryReader', () => {
const story = StoryParser.parse(read_yaml('tools/tests/tester/fixtures/stories/passed/multiple_methods.yaml'))

test('expands prologues', () => {
expect(story.prologues?.length).toEqual(2)
expect(_.map(story.prologues, (prologue) => prologue.method)).toEqual(['HEAD', 'DELETE'])
})

test('expands epilogues', () => {
expect(story.epilogues?.length).toEqual(2)
expect(_.map(story.epilogues, (epilogue) => epilogue.method)).toEqual(['HEAD', 'DELETE'])
})

test('expands chapters', () => {
expect(story.chapters?.length).toEqual(2)
expect(_.map(story.chapters, (chapter) => chapter.method)).toEqual(['PUT', 'HEAD'])
})
})
5 changes: 3 additions & 2 deletions tools/tests/tester/StoryValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import StoryValidator from "../../src/tester/StoryValidator";
import { StoryEvaluation } from "../../src/tester/types/eval.types";
import { read_yaml } from "../../src/helpers";
import { Story } from "../../src/tester/types/story.types";
import { ParsedStory } from "tester/types/parsed_story.types";
import StoryParser from "../../src/tester/StoryParser";

const validator = new StoryValidator()

function validate(path: string): StoryEvaluation | undefined {
const story: Story = read_yaml(path)
const story: ParsedStory = StoryParser.parse(read_yaml(path))
return validator.validate({ story, display_path: path, full_path: path })
}

Expand Down

0 comments on commit 7551636

Please sign in to comment.