From ac9939a41ddc5a53d783e162879190c814320016 Mon Sep 17 00:00:00 2001 From: dblock Date: Fri, 7 Jun 2024 16:37:18 -0400 Subject: [PATCH] Moved helpers into src. Signed-off-by: dblock --- tools/src/OpenSearchHttpClient.ts | 2 +- tools/src/coverage/CoverageCalculator.ts | 2 +- .../dump-cluster-spec/dump-cluster-spec.ts | 2 +- tools/{ => src}/helpers.ts | 0 tools/src/linter/components/NamespaceFile.ts | 2 +- .../components/SupersededOperationsFile.ts | 2 +- .../linter/components/base/FileValidator.ts | 2 +- tools/src/merger/GlobalParamsGenerator.ts | 2 +- tools/src/merger/OpenApiMerger.ts | 2 +- tools/src/merger/OpenDistro.ts | 2 +- tools/src/merger/SupersededOpsGenerator.ts | 2 +- tools/src/tester/OperationLocator.ts | 2 +- tools/src/tester/TestRunner.ts | 2 +- tools/src/tester/_generate_story_types.ts | 2 +- tools/tests/helpers.test.ts | 28 +++++++++++++++++++ tools/tests/tester/helpers.ts | 2 +- 16 files changed, 42 insertions(+), 14 deletions(-) rename tools/{ => src}/helpers.ts (100%) create mode 100644 tools/tests/helpers.test.ts diff --git a/tools/src/OpenSearchHttpClient.ts b/tools/src/OpenSearchHttpClient.ts index 76cb8672d..cd5ce9cb2 100644 --- a/tools/src/OpenSearchHttpClient.ts +++ b/tools/src/OpenSearchHttpClient.ts @@ -10,7 +10,7 @@ import { Option } from '@commander-js/extra-typings' import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios' import * as https from 'node:https' -import { sleep } from '../helpers' +import { sleep } from './helpers' const DEFAULT_URL = 'https://localhost:9200' const DEFAULT_USER = 'admin' diff --git a/tools/src/coverage/CoverageCalculator.ts b/tools/src/coverage/CoverageCalculator.ts index 090e0ecb1..0bc33a837 100644 --- a/tools/src/coverage/CoverageCalculator.ts +++ b/tools/src/coverage/CoverageCalculator.ts @@ -8,7 +8,7 @@ */ import { type OpenAPIV3 } from 'openapi-types' -import { HTTP_METHODS, read_yaml, write_json } from '../../helpers' +import { HTTP_METHODS, read_yaml, write_json } from '../helpers' export default class CoverageCalculator { private readonly _cluster_spec: OpenAPIV3.Document diff --git a/tools/src/dump-cluster-spec/dump-cluster-spec.ts b/tools/src/dump-cluster-spec/dump-cluster-spec.ts index 16038396c..cedc7094a 100644 --- a/tools/src/dump-cluster-spec/dump-cluster-spec.ts +++ b/tools/src/dump-cluster-spec/dump-cluster-spec.ts @@ -10,7 +10,7 @@ import { Command, Option } from '@commander-js/extra-typings' import { resolve } from 'path' import * as process from 'node:process' -import { write_yaml } from '../../helpers' +import { write_yaml } from '../helpers' import { get_opensearch_opts_from_cli, OPENSEARCH_INSECURE_OPTION, diff --git a/tools/helpers.ts b/tools/src/helpers.ts similarity index 100% rename from tools/helpers.ts rename to tools/src/helpers.ts diff --git a/tools/src/linter/components/NamespaceFile.ts b/tools/src/linter/components/NamespaceFile.ts index 2808b6986..fb6ad52f7 100644 --- a/tools/src/linter/components/NamespaceFile.ts +++ b/tools/src/linter/components/NamespaceFile.ts @@ -12,7 +12,7 @@ import { type OperationSpec, type ValidationError } from 'types' import OperationGroup from './OperationGroup' import _ from 'lodash' import Operation from './Operation' -import { resolve_ref, sort_by_keys } from '../../../helpers' +import { resolve_ref, sort_by_keys } from '../../helpers' import FileValidator from './base/FileValidator' const HTTP_METHODS = ['get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'trace'] diff --git a/tools/src/linter/components/SupersededOperationsFile.ts b/tools/src/linter/components/SupersededOperationsFile.ts index 7c0f5d3b0..65e3ec825 100644 --- a/tools/src/linter/components/SupersededOperationsFile.ts +++ b/tools/src/linter/components/SupersededOperationsFile.ts @@ -8,7 +8,7 @@ */ import _ from 'lodash' -import { sort_array_by_keys } from '../../../helpers' +import { sort_array_by_keys } from '../../helpers' import FileValidator from './base/FileValidator' import { type ValidationError } from 'types' import { type OpenAPIV3 } from 'openapi-types' diff --git a/tools/src/linter/components/base/FileValidator.ts b/tools/src/linter/components/base/FileValidator.ts index 2ed0aa128..53a887014 100644 --- a/tools/src/linter/components/base/FileValidator.ts +++ b/tools/src/linter/components/base/FileValidator.ts @@ -10,7 +10,7 @@ import ValidatorBase from './ValidatorBase' import { type ValidationError } from 'types' import { type OpenAPIV3 } from 'openapi-types' -import { read_yaml } from '../../../../helpers' +import { read_yaml } from '../../../helpers' import AJV from 'ajv' import addFormats from 'ajv-formats' diff --git a/tools/src/merger/GlobalParamsGenerator.ts b/tools/src/merger/GlobalParamsGenerator.ts index 1e1af59fb..192e711b9 100644 --- a/tools/src/merger/GlobalParamsGenerator.ts +++ b/tools/src/merger/GlobalParamsGenerator.ts @@ -9,7 +9,7 @@ import { type OpenAPIV3 } from 'openapi-types' import _ from 'lodash' -import { read_yaml } from '../../helpers' +import { read_yaml } from '../helpers' export default class GlobalParamsGenerator { global_params: Record diff --git a/tools/src/merger/OpenApiMerger.ts b/tools/src/merger/OpenApiMerger.ts index 47decaeaa..5d38ddeca 100644 --- a/tools/src/merger/OpenApiMerger.ts +++ b/tools/src/merger/OpenApiMerger.ts @@ -10,7 +10,7 @@ import { type OpenAPIV3 } from 'openapi-types' import fs from 'fs' import _ from 'lodash' -import { read_yaml, write_yaml } from '../../helpers' +import { read_yaml, write_yaml } from '../helpers' import SupersededOpsGenerator from './SupersededOpsGenerator' import GlobalParamsGenerator from './GlobalParamsGenerator' import { Logger } from '../Logger' diff --git a/tools/src/merger/OpenDistro.ts b/tools/src/merger/OpenDistro.ts index f7081ce06..021e58b70 100644 --- a/tools/src/merger/OpenDistro.ts +++ b/tools/src/merger/OpenDistro.ts @@ -8,7 +8,7 @@ */ import { type HttpVerb, type OperationPath, type SupersededOperationMap } from 'types' -import { read_yaml, write_yaml } from '../../helpers' +import { read_yaml, write_yaml } from '../helpers' // One-time script to generate _superseded_operations.yaml file for OpenDistro // Keeping this for now in case we need to update the file in the near future. Can be removed after a few months. diff --git a/tools/src/merger/SupersededOpsGenerator.ts b/tools/src/merger/SupersededOpsGenerator.ts index 0fd34eb54..9228af55d 100644 --- a/tools/src/merger/SupersededOpsGenerator.ts +++ b/tools/src/merger/SupersededOpsGenerator.ts @@ -9,7 +9,7 @@ import { type OperationSpec, type SupersededOperationMap } from 'types' import _ from 'lodash' -import { read_yaml } from '../../helpers' +import { read_yaml } from '../helpers' import { type Logger } from '../Logger' export default class SupersededOpsGenerator { diff --git a/tools/src/tester/OperationLocator.ts b/tools/src/tester/OperationLocator.ts index 2d5df08ee..df0f2dc91 100644 --- a/tools/src/tester/OperationLocator.ts +++ b/tools/src/tester/OperationLocator.ts @@ -8,7 +8,7 @@ */ import { type OpenAPIV3 } from 'openapi-types' -import { resolve_ref } from '../../helpers' +import { resolve_ref } from '../helpers' import { type Chapter } from './types/story.types' import { type ParsedOperation } from './types/spec.types' import _ from 'lodash' diff --git a/tools/src/tester/TestRunner.ts b/tools/src/tester/TestRunner.ts index fa88306ab..779d314de 100644 --- a/tools/src/tester/TestRunner.ts +++ b/tools/src/tester/TestRunner.ts @@ -11,7 +11,7 @@ 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 { read_yaml } from '../helpers' import { Result, type StoryEvaluation } from './types/eval.types' import { type ResultLogger } from './ResultLogger' import { basename, resolve } from 'path' diff --git a/tools/src/tester/_generate_story_types.ts b/tools/src/tester/_generate_story_types.ts index 98e9ed1f6..6113e7948 100644 --- a/tools/src/tester/_generate_story_types.ts +++ b/tools/src/tester/_generate_story_types.ts @@ -9,7 +9,7 @@ import * as js2ts from 'json-schema-to-typescript' import fs from 'fs' -import { read_yaml } from '../../helpers' +import { read_yaml } from '../helpers' const schema = read_yaml('json_schemas/test_story.schema.yaml') void js2ts.compile(schema, 'Story', diff --git a/tools/tests/helpers.test.ts b/tools/tests/helpers.test.ts new file mode 100644 index 000000000..de8db04c3 --- /dev/null +++ b/tools/tests/helpers.test.ts @@ -0,0 +1,28 @@ +/* +* 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 { sort_array_by_keys } from '../src/helpers' + +describe('helpers', () => { + describe('sort_array_by_keys', () => { + test('sorts arrays of string', () => { + expect(sort_array_by_keys([])).toEqual([]) + expect(sort_array_by_keys(['GET', 'POST'], ['GET', 'POST'])).toEqual(['GET', 'POST']) + expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET'])).toEqual(['POST', 'GET']) + expect(sort_array_by_keys(['GET', 'POST'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET']) + expect(sort_array_by_keys(['DELETE', 'POST', 'GET'], ['POST', 'GET', 'DELETE'])).toEqual(['POST', 'GET', 'DELETE']) + }) + + test('does not modify the original object', () => { + const arr = ['GET', 'POST'] + expect(sort_array_by_keys(arr, ['POST', 'GET'])).toEqual(['POST', 'GET']) + expect(arr).toEqual(['GET', 'POST'] ) + }) + }) +}) diff --git a/tools/tests/tester/helpers.ts b/tools/tests/tester/helpers.ts index e10accce6..05bf981eb 100644 --- a/tools/tests/tester/helpers.ts +++ b/tools/tests/tester/helpers.ts @@ -9,7 +9,7 @@ import YAML from 'yaml' import type { ChapterEvaluation, Evaluation, StoryEvaluation } from '../../src/tester/types/eval.types' -import { read_yaml } from '../../helpers' +import { read_yaml } from 'helpers' import StoryEvaluator from '../../src/tester/StoryEvaluator' import OperationLocator from '../../src/tester/OperationLocator' import ChapterReader from '../../src/tester/ChapterReader'