diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts index b9f91fe7a8..4aaf1a2bd2 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.spec.ts @@ -2,11 +2,11 @@ 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'; @@ -14,7 +14,7 @@ describe('Copy Referenced Files', () => { 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 () => { @@ -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); + }); }); diff --git a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts index ac73243b3d..814f65271c 100644 --- a/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts +++ b/packages/@ama-sdk/schematics/schematics/typescript/core/helpers/copy-referenced-files.ts @@ -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; @@ -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; }); } diff --git a/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml new file mode 100644 index 0000000000..4f5fc58aed --- /dev/null +++ b/packages/@ama-sdk/schematics/testing/mocks/split-spec/spec-with-updated-paths.yaml @@ -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"