From 66f41a6b34bab384d4a61b0912e0c4a7a4887d7a Mon Sep 17 00:00:00 2001 From: Kamil Piechaczek Date: Thu, 19 Sep 2024 08:50:00 +0200 Subject: [PATCH] Replaced require with import (readline) for Windows. --- .../lib/utils/manual-tests/createserver.js | 11 ++++++----- .../tests/utils/manual-tests/createserver.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/ckeditor5-dev-tests/lib/utils/manual-tests/createserver.js b/packages/ckeditor5-dev-tests/lib/utils/manual-tests/createserver.js index bafe7dfc4..2db3da25a 100644 --- a/packages/ckeditor5-dev-tests/lib/utils/manual-tests/createserver.js +++ b/packages/ckeditor5-dev-tests/lib/utils/manual-tests/createserver.js @@ -4,12 +4,13 @@ */ import http from 'http'; +import fs from 'fs'; import path from 'path'; +import readline from 'readline'; +import { fileURLToPath } from 'url'; import { globSync } from 'glob'; -import fs from 'fs'; import combine from 'dom-combiner'; import { logger } from '@ckeditor/ckeditor5-dev-utils'; -import { fileURLToPath } from 'url'; const __filename = fileURLToPath( import.meta.url ); const __dirname = path.dirname( __filename ); @@ -30,15 +31,15 @@ export default function createManualTestServer( sourcePath, port = 8125, onCreat // SIGINT isn't caught on Windows in process. However, `CTRL+C` can be caught // by `readline` module. After that we can emit SIGINT to the process manually. if ( process.platform === 'win32' ) { - const readline = require( 'readline' ).createInterface( { + const readlineInterface = readline.createInterface( { input: process.stdin, output: process.stdout } ); // Save the reference of the stream to be able to close it in tests. - server._readline = readline; + server._readline = readlineInterface; - readline.on( 'SIGINT', () => { + readlineInterface.on( 'SIGINT', () => { process.emit( 'SIGINT' ); } ); } diff --git a/packages/ckeditor5-dev-tests/tests/utils/manual-tests/createserver.js b/packages/ckeditor5-dev-tests/tests/utils/manual-tests/createserver.js index 5192181ed..f35577449 100644 --- a/packages/ckeditor5-dev-tests/tests/utils/manual-tests/createserver.js +++ b/packages/ckeditor5-dev-tests/tests/utils/manual-tests/createserver.js @@ -4,11 +4,13 @@ */ import http from 'http'; +import readline from 'readline'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { logger } from '@ckeditor/ckeditor5-dev-utils'; import createManualTestServer from '../../../lib/utils/manual-tests/createserver.js'; vi.mock( '@ckeditor/ckeditor5-dev-utils' ); +vi.mock( 'readline' ); describe( 'createManualTestServer()', () => { let loggerStub, server; @@ -77,4 +79,19 @@ describe( 'createManualTestServer()', () => { expect( spy ).toHaveBeenCalledExactlyOnceWith( server ); } ); + + it( 'should use "readline" to listen to the SIGINT event on Windows', () => { + const readlineInterface = { + on: vi.fn(), + close: vi.fn() + }; + + vi.mocked( readline ).createInterface.mockReturnValue( readlineInterface ); + vi.spyOn( process, 'platform', 'get' ).mockReturnValue( 'win32' ); + + createManualTestServer( 'workspace/build/.manual-tests' ); + + expect( vi.mocked( readline ).createInterface ).toHaveBeenCalledOnce(); + expect( readlineInterface.on ).toHaveBeenCalledExactlyOnceWith( 'SIGINT', expect.any( Function ) ); + } ); } );