Skip to content

Commit

Permalink
Replaced require with import (readline) for Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Sep 19, 2024
1 parent 5f7fddb commit 0aa7ab0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -30,15 +31,12 @@ 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;

readline.on( 'SIGINT', () => {
readlineInterface.on( 'SIGINT', () => {
process.emit( 'SIGINT' );
} );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,13 +35,6 @@ describe( 'createManualTestServer()', () => {

afterEach( () => {
server.close();

// To avoid false positives and encourage better testing practices, Mocha will no longer automatically
// kill itself via `process.exit()` when it thinks it should be done running. Hence, we must close the stream
// before leaving the test. See: https://stackoverflow.com/a/52143003.
if ( server._readline ) {
server._readline.close();
}
} );

it( 'should start http server', () => {
Expand Down Expand Up @@ -77,4 +72,18 @@ describe( 'createManualTestServer()', () => {

expect( spy ).toHaveBeenCalledExactlyOnceWith( server );
} );

it( 'should use "readline" to listen to the SIGINT event on Windows', () => {
const readlineInterface = {
on: 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 ) );
} );
} );

0 comments on commit 0aa7ab0

Please sign in to comment.