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 1e8d079
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 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 Down
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 1e8d079

Please sign in to comment.