From c386b95cc84135e025fcf723c3b84b8843fbc41b Mon Sep 17 00:00:00 2001 From: splincode Date: Wed, 25 Dec 2024 13:14:52 +0300 Subject: [PATCH] chore(schematics): replace template line endings with platform specific --- .github/workflows/schematic.yml | 13 ++- projects/cdk/schematics/ng-update/v4/index.ts | 2 + .../v4/steps/replace-template-line-endings.ts | 14 +++ .../ng-update/v4/tests/replace-crlf.spec.ts | 95 +++++++++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 projects/cdk/schematics/ng-update/v4/steps/replace-template-line-endings.ts create mode 100644 projects/cdk/schematics/ng-update/v4/tests/replace-crlf.spec.ts diff --git a/.github/workflows/schematic.yml b/.github/workflows/schematic.yml index 803b2e4f96547..9b558df4d596d 100644 --- a/.github/workflows/schematic.yml +++ b/.github/workflows/schematic.yml @@ -3,7 +3,7 @@ on: pull_request: jobs: - schematics: + migration: runs-on: ubuntu-latest name: Run the latest migration steps: @@ -13,6 +13,17 @@ jobs: id: info - run: npx nx run cdk:schematics --v=${{ steps.info.outputs.root-package-major-version }} + unit-test: + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4.2.2 + - uses: taiga-family/ci/actions/setup/node@v1.105.0 + - uses: taiga-family/ci/actions/run/node-info@v1.105.0 + - run: npx jest projects/cdk/schematics/**/*.spec.ts --coverage=false + concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true diff --git a/projects/cdk/schematics/ng-update/v4/index.ts b/projects/cdk/schematics/ng-update/v4/index.ts index 32e7663a26551..f6af6876cb8ce 100644 --- a/projects/cdk/schematics/ng-update/v4/index.ts +++ b/projects/cdk/schematics/ng-update/v4/index.ts @@ -48,6 +48,7 @@ import {migrateEditor} from './steps/migrate-editor'; import {migrateImportProvidersFrom} from './steps/migrate-providers-from'; import {migrateRoot} from './steps/migrate-root'; import {replaceFunctions} from './steps/replace-functions'; +import {replaceTemplateLineEndings} from './steps/replace-template-line-endings'; import {replaceModulesWithProviders} from './steps/utils/replace-modules-with-providers'; function main(options: TuiSchema): Rule { @@ -104,6 +105,7 @@ export function updateToV4(options: TuiSchema): Rule { return chain([ main(options), + () => replaceTemplateLineEndings(), () => { const executionTime = getExecutionTime(t0, performance.now()); diff --git a/projects/cdk/schematics/ng-update/v4/steps/replace-template-line-endings.ts b/projects/cdk/schematics/ng-update/v4/steps/replace-template-line-endings.ts new file mode 100644 index 0000000000000..8c3b7f259cd18 --- /dev/null +++ b/projects/cdk/schematics/ng-update/v4/steps/replace-template-line-endings.ts @@ -0,0 +1,14 @@ +/// +import {EOL} from 'node:os'; + +import {getSourceFiles, saveActiveProject} from 'ng-morph'; + +import {ALL_FILES} from '../../../constants'; + +export function replaceTemplateLineEndings(pattern = ALL_FILES): void { + getSourceFiles(pattern).forEach((file) => + file.replaceWithText(file.getFullText().replaceAll(/\r?\n/g, EOL)), + ); + + saveActiveProject(); +} diff --git a/projects/cdk/schematics/ng-update/v4/tests/replace-crlf.spec.ts b/projects/cdk/schematics/ng-update/v4/tests/replace-crlf.spec.ts new file mode 100644 index 0000000000000..51cf227460d00 --- /dev/null +++ b/projects/cdk/schematics/ng-update/v4/tests/replace-crlf.spec.ts @@ -0,0 +1,95 @@ +import {type} from 'node:os'; +import {join} from 'node:path'; + +import {HostTree} from '@angular-devkit/schematics'; +import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; +import type {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema'; +import { + createProject, + createSourceFile, + resetActiveProject, + saveActiveProject, + setActiveProject, +} from 'ng-morph'; + +import {createAngularJson} from '../../../utils/create-angular-json'; + +const collectionPath = join(__dirname, '../../../migration.json'); + +const COMPONENT_BEFORE = `\r\n +@Component({ + standalone: true, + templateUrl: './test.template.html', +}) +export class Test {}\r\n`; + +const COMPONENT_AFTER = type().startsWith('Windows') + ? `\r\n +@Component({ + standalone: true, + templateUrl: './test.template.html', +}) +export class Test {}\r\n` + : `\n +@Component({ + standalone: true, + templateUrl: './test.template.html', +}) +export class Test {}\n`; + +const TEMPLATE_BEFORE = '\r\n

Hello

\r\n'; + +const TEMPLATE_AFTER = type().startsWith('Windows') + ? '\r\n

Hello

\r\n' + : '\n

Hello

\n'; + +describe('ng-update', () => { + let host: UnitTestTree; + let runner: SchematicTestRunner; + + beforeEach(() => { + host = new UnitTestTree(new HostTree()); + runner = new SchematicTestRunner('schematics', collectionPath); + + setActiveProject(createProject(host)); + + createMainFiles(); + + saveActiveProject(); + }); + + it('should migrate badge in template', async () => { + const tree = await runner.runSchematic( + 'updateToV4', + {'skip-logs': process.env['TUI_CI'] === 'true'} as Partial, + host, + ); + + expect(tree.readContent('test/app/test.template.html')).toEqual(TEMPLATE_AFTER); + }); + + it('should migrate badge references in ts files', async () => { + const tree = await runner.runSchematic( + 'updateToV4', + {'skip-logs': process.env['TUI_CI'] === 'true'} as Partial, + host, + ); + + expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER); + }); + + afterEach(() => { + resetActiveProject(); + }); +}); + +function createMainFiles(): void { + createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE); + createSourceFile('test/app/test.template.html', TEMPLATE_BEFORE); + + createAngularJson(); + createSourceFile( + 'package.json', + '{\r\n"dependencies": {\r\n"@angular/core": "~13.0.0",\r\n "@taiga-ui/addon-commerce": "~3.42.0"\r\n}\r\n}', + ); +}