From e0596a22a0a0525507235759da27909f7484c643 Mon Sep 17 00:00:00 2001 From: Cajetan Grill <99743209+krokettenkoal@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:08:46 +0200 Subject: [PATCH 1/2] Updated code format tests to be OS-agnostic - Test results for formatCode.ts are now designed to match irrespective of the system's line separators: The previously used multi-line template strings created UNIX-style line separators, which do not match on Windows systems. Therefore, line endings are now explicitly written into the expected output string, unfortunately affecting readability. - Inside `formatCode`, input strings are now split into lines by any valid line separator. Previously, only the system's line separators have been considered, which may produce some unexpected results when receiving line endings from other systems. --- src/utils/formatCode.spec.ts | 11 +++-------- src/utils/formatCode.ts | 2 +- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/utils/formatCode.spec.ts b/src/utils/formatCode.spec.ts index df46263b1..49a4665f1 100644 --- a/src/utils/formatCode.spec.ts +++ b/src/utils/formatCode.spec.ts @@ -1,3 +1,4 @@ +import {EOL} from 'os'; import { formatCode } from './formatCode'; const input1 = `{ foo: true }`; @@ -13,20 +14,14 @@ foo: true, bar: 123 }`; -const output3 = `{ -\tfoo: true, -\tbar: 123 -}`; +const output3 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`; const input4 = `{ \t\t\t\tfoo: true, \t\t\t\tbar: 123 }`; -const output4 = `{ -\tfoo: true, -\tbar: 123 -}`; +const output4 = `{${EOL}\tfoo: true,${EOL}\tbar: 123${EOL}}`; describe('format', () => { it('should produce correct result', () => { diff --git a/src/utils/formatCode.ts b/src/utils/formatCode.ts index 42cfd5cf4..9d0e8e69b 100644 --- a/src/utils/formatCode.ts +++ b/src/utils/formatCode.ts @@ -2,7 +2,7 @@ import { EOL } from 'os'; export const formatCode = (s: string): string => { let indent: number = 0; - let lines = s.split(EOL); + let lines = s.split(/[\r\n]+/); lines = lines.map(line => { line = line.trim().replace(/^\*/g, ' *'); let i = indent; From d97edce97995f69b839265550580403cf8de0c7b Mon Sep 17 00:00:00 2001 From: Cajetan Grill <99743209+krokettenkoal@users.noreply.github.com> Date: Fri, 28 Jul 2023 11:22:56 +0200 Subject: [PATCH 2/2] Updated file system tests to be OS-agnostic - Test results for write operations are now designed to match irrespective of the system's path encoding: The previously used UNIX-style path strings do not match on Windows systems. Therefore, expected paths are now being resolved as they are in their corresponding write function. - Unit tests should now pass on all systems. --- src/utils/writeClientCore.spec.ts | 13 +++++++------ src/utils/writeClientIndex.spec.ts | 4 +++- src/utils/writeClientModels.spec.ts | 3 ++- src/utils/writeClientSchemas.spec.ts | 3 ++- src/utils/writeClientServices.spec.ts | 3 ++- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/utils/writeClientCore.spec.ts b/src/utils/writeClientCore.spec.ts index 36990054e..7db71f59b 100644 --- a/src/utils/writeClientCore.spec.ts +++ b/src/utils/writeClientCore.spec.ts @@ -1,4 +1,5 @@ import { EOL } from 'os'; +import { resolve } from 'path'; import type { Client } from '../client/interfaces/Client'; import { HttpClient } from '../HttpClient'; @@ -40,11 +41,11 @@ describe('writeClientCore', () => { await writeClientCore(client, templates, '/', HttpClient.FETCH, Indent.SPACE_4); - expect(writeFile).toBeCalledWith('/OpenAPI.ts', `settings${EOL}`); - expect(writeFile).toBeCalledWith('/ApiError.ts', `apiError${EOL}`); - expect(writeFile).toBeCalledWith('/ApiRequestOptions.ts', `apiRequestOptions${EOL}`); - expect(writeFile).toBeCalledWith('/ApiResult.ts', `apiResult${EOL}`); - expect(writeFile).toBeCalledWith('/CancelablePromise.ts', `cancelablePromise${EOL}`); - expect(writeFile).toBeCalledWith('/request.ts', `request${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/OpenAPI.ts'), `settings${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/ApiError.ts'), `apiError${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/ApiRequestOptions.ts'), `apiRequestOptions${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/ApiResult.ts'), `apiResult${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/CancelablePromise.ts'), `cancelablePromise${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/request.ts'), `request${EOL}`); }); }); diff --git a/src/utils/writeClientIndex.spec.ts b/src/utils/writeClientIndex.spec.ts index a74421115..a7d5b610a 100644 --- a/src/utils/writeClientIndex.spec.ts +++ b/src/utils/writeClientIndex.spec.ts @@ -1,3 +1,5 @@ +import { resolve } from 'path'; + import type { Client } from '../client/interfaces/Client'; import { writeFile } from './fileSystem'; import type { Templates } from './registerHandlebarTemplates'; @@ -36,6 +38,6 @@ describe('writeClientIndex', () => { await writeClientIndex(client, templates, '/', true, true, true, true, true, 'Service', ''); - expect(writeFile).toBeCalledWith('/index.ts', 'index'); + expect(writeFile).toBeCalledWith(resolve('/', '/index.ts'), 'index'); }); }); diff --git a/src/utils/writeClientModels.spec.ts b/src/utils/writeClientModels.spec.ts index e147c8e73..ee0f2b4f6 100644 --- a/src/utils/writeClientModels.spec.ts +++ b/src/utils/writeClientModels.spec.ts @@ -1,4 +1,5 @@ import { EOL } from 'os'; +import { resolve } from 'path'; import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; @@ -53,6 +54,6 @@ describe('writeClientModels', () => { await writeClientModels(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4); - expect(writeFile).toBeCalledWith('/User.ts', `model${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/User.ts'), `model${EOL}`); }); }); diff --git a/src/utils/writeClientSchemas.spec.ts b/src/utils/writeClientSchemas.spec.ts index f71286232..e75928b8c 100644 --- a/src/utils/writeClientSchemas.spec.ts +++ b/src/utils/writeClientSchemas.spec.ts @@ -1,4 +1,5 @@ import { EOL } from 'os'; +import { resolve } from 'path'; import type { Model } from '../client/interfaces/Model'; import { HttpClient } from '../HttpClient'; @@ -53,6 +54,6 @@ describe('writeClientSchemas', () => { await writeClientSchemas(models, templates, '/', HttpClient.FETCH, false, Indent.SPACE_4); - expect(writeFile).toBeCalledWith('/$User.ts', `schema${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/$User.ts'), `schema${EOL}`); }); }); diff --git a/src/utils/writeClientServices.spec.ts b/src/utils/writeClientServices.spec.ts index b7ebbfe6c..f936d6609 100644 --- a/src/utils/writeClientServices.spec.ts +++ b/src/utils/writeClientServices.spec.ts @@ -1,4 +1,5 @@ import { EOL } from 'os'; +import { resolve } from 'path'; import type { Service } from '../client/interfaces/Service'; import { HttpClient } from '../HttpClient'; @@ -41,6 +42,6 @@ describe('writeClientServices', () => { await writeClientServices(services, templates, '/', HttpClient.FETCH, false, false, Indent.SPACE_4, 'Service'); - expect(writeFile).toBeCalledWith('/UserService.ts', `service${EOL}`); + expect(writeFile).toBeCalledWith(resolve('/', '/UserService.ts'), `service${EOL}`); }); });