Skip to content

Commit

Permalink
fix: local file path update in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mrednic-1A committed Aug 20, 2024
1 parent 33707b9 commit 40b92ac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { cleanVirtualFileSystem, useVirtualFileSystem } from '@o3r/test-helpers'
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';

describe('Copy Referenced Files', () => {
describe('Specs processing', () => {
const virtualFileSystem = useVirtualFileSystem();
const copyReferencedFiles = require('./copy-referenced-files').copyReferencedFiles;
const {copyReferencedFiles, updateLocalRelativeRefs} = require('./copy-referenced-files');

const migrationScriptMocksPath = join(__dirname, '../../../../testing/mocks');
const specsMocksPath = join(__dirname, '../../../../testing/mocks');
const specFilePath = '../models/split-spec/split-spec.yaml';
const outputDirectory = './local-references';

const copyMockFile = async (virtualPath: string, realPath: string) => {
if (!virtualFileSystem.existsSync(dirname(virtualPath))) {
await virtualFileSystem.promises.mkdir(dirname(virtualPath), {recursive: true});
}
await virtualFileSystem.promises.writeFile(virtualPath, await readFile(join(migrationScriptMocksPath, realPath), {encoding: 'utf8'}));
await virtualFileSystem.promises.writeFile(virtualPath, await readFile(join(specsMocksPath, realPath), {encoding: 'utf8'}));
};

beforeAll(async () => {
Expand All @@ -39,4 +39,15 @@ describe('Copy Referenced Files', () => {
expect(virtualFileSystem.existsSync(join(outputDirectory, 'spec-chunk3/spec-chunk3.yaml'))).toBe(true);
expect(virtualFileSystem.existsSync(join(outputDirectory, 'spec-chunk4/spec-chunk4.yaml'))).toBe(true);
});

it('should update with new local basepath', async () => {
const specWitheRelativesFilePath = 'split-spec/split-spec.yaml';
const expectedSpecWitheRelativesFilePath = 'split-spec/spec-with-updated-paths.yaml';
const expectedContent = await readFile(join(specsMocksPath, expectedSpecWitheRelativesFilePath), {encoding: 'utf8'});
const specContent = await readFile(join(specsMocksPath, specWitheRelativesFilePath), {encoding: 'utf8'});

const baseRelativePath = await copyReferencedFiles(specFilePath, './output-local-directory');
const newSpecContent = await updateLocalRelativeRefs(specContent, baseRelativePath);
expect(newSpecContent).toBe(expectedContent);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs';
import { copyFile, mkdir, readFile, rm } from 'node:fs/promises';
import { dirname, join, normalize, posix, relative, resolve } from 'node:path';
import { dirname, join, normalize, posix, relative, resolve, sep } from 'node:path';

const refMatcher = /\B['"]?[$]ref['"]?\s*:\s*([^#\n]+)/g;

Expand Down Expand Up @@ -53,7 +53,7 @@ export function updateLocalRelativeRefs(specContent: string, newBaseRelativePath
return specContent.replace(refMatcher, (match, ref: string) => {
const refPath = ref.replace(/['"]/g, '');
return refPath.startsWith('.') ?
match.replace(refPath, formatPath(normalize(posix.join(newBaseRelativePath, refPath))))
match.replace(refPath, formatPath(normalize(posix.join(newBaseRelativePath.replaceAll(sep, posix.sep), refPath))))
: match;
});
}
Expand All @@ -64,12 +64,9 @@ export function updateLocalRelativeRefs(specContent: string, newBaseRelativePath
* @param outputDirectory
*/
export async function copyReferencedFiles(specFilePath: string, outputDirectory: string) {
const dedupe = (paths: string[]) =>
paths.filter((refPath, index) => {
const actualPath = join(dirname(specFilePath), refPath);
return paths.findIndex((otherRefPath) => join(dirname(specFilePath), otherRefPath) === actualPath) === index;
});
const refPaths = dedupe(await extractRefPathRecursive(specFilePath, specFilePath, new Set()));
const dedupe = (paths: string[]) => ([...new Set(paths)]);
const allRefPaths = await extractRefPathRecursive(specFilePath, specFilePath, new Set());
const refPaths = dedupe(allRefPaths);
if (refPaths.length) {
if (existsSync(outputDirectory)) {
await rm(outputDirectory, { recursive: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ properties:
example: 10
category:
$ref: './split-spec.yaml#/components/schemas/Category'
category2:
$ref: '../spec-chunk4/spec-chunk4.yaml#/components/schemas/Category'
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
openapi: 3.0.2
info:
description: test
title: test
version: 0.0.0
paths:
/test:
get:
responses:
'200':
description: test
content:
application/json:
schema:
$ref: './output-local-directory/split-spec/spec-chunk1.yaml'
/test2:
get:
responses:
'200':
description: test
content:
application/json:
schema:
$ref: './output-local-directory/spec-chunk2.yaml'
/test3:
get:
responses:
'200':
description: test
content:
application/json:
schema:
$ref: './output-local-directory/spec-chunk3/spec-chunk3.yaml'
components:
schemas:
Category:
type: object
properties:
id:
type: integer
format: int64
example: 10
name:
type: string
example: "test"

0 comments on commit 40b92ac

Please sign in to comment.