Skip to content

Commit

Permalink
Merge pull request #1062 from ckeditor/ck/17797-fixed-automated-tests…
Browse files Browse the repository at this point in the history
…-entry

Internal: Fixed automated tests entry point on Windows.
  • Loading branch information
martnpaneq authored Jan 31, 2025
2 parents 875f7b3 + a8f4de5 commit b70aa02
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
26 changes: 13 additions & 13 deletions packages/ckeditor5-dev-tests/lib/tasks/runautomatedtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/

import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { logger } from '@ckeditor/ckeditor5-dev-utils';
import getKarmaConfig from '../utils/automated-tests/getkarmaconfig.js';
Expand All @@ -15,21 +14,22 @@ import { mkdirp } from 'mkdirp';
import karmaLogger from 'karma/lib/logger.js';
import karma from 'karma';
import transformFileOptionToTestGlob from '../utils/transformfileoptiontotestglob.js';
import upath from 'upath';

const __filename = fileURLToPath( import.meta.url );
const __dirname = path.dirname( __filename );
const __dirname = upath.dirname( __filename );

// Glob patterns that should be ignored. It means if a specified test file is located under path
// that matches to these patterns, the file will be skipped.
const IGNORE_GLOBS = [
// Ignore files which are saved in `manual/` directory. There are manual tests.
path.join( '**', 'tests', '**', 'manual', '**', '*.{js,ts}' ),
upath.join( '**', 'tests', '**', 'manual', '**', '*.{js,ts}' ),
// Ignore `_utils` directory as well because there are saved utils for tests.
path.join( '**', 'tests', '**', '_utils', '**', '*.{js,ts}' )
upath.join( '**', 'tests', '**', '_utils', '**', '*.{js,ts}' )
];

// An absolute path to the entry file that will be passed to Karma.
const ENTRY_FILE_PATH = path.posix.join( process.cwd(), 'build', '.automated-tests', 'entry-point.js' );
const ENTRY_FILE_PATH = upath.join( process.cwd(), 'build', '.automated-tests', 'entry-point.js' );

export default function runAutomatedTests( options ) {
return Promise.resolve().then( () => {
Expand Down Expand Up @@ -68,7 +68,7 @@ function transformFilesToTestGlob( files ) {
}

function createEntryFile( globPatterns, production ) {
mkdirp.sync( path.dirname( ENTRY_FILE_PATH ) );
mkdirp.sync( upath.dirname( ENTRY_FILE_PATH ) );
karmaLogger.setupFromConfig( { logLevel: 'INFO' } );

const log = karmaLogger.create( 'config' );
Expand All @@ -78,7 +78,7 @@ function createEntryFile( globPatterns, production ) {
let hasFiles = false;

for ( const resolvedPattern of globPatterns[ singlePattern ] ) {
const files = globSync( resolvedPattern ).map( file => file.replace( /\\/g, '/' ) );
const files = globSync( resolvedPattern ).map( filePath => upath.normalize( filePath ) );

if ( files.length ) {
hasFiles = true;
Expand All @@ -99,21 +99,21 @@ function createEntryFile( globPatterns, production ) {
}

// Set global license key in the `before` hook.
allFiles.unshift( path.join( __dirname, '..', 'utils', 'automated-tests', 'licensekeybefore.js' ).replace( /\\/g, '/' ) );
allFiles.unshift( upath.join( __dirname, '..', 'utils', 'automated-tests', 'licensekeybefore.js' ) );

// Inject the leak detector root hooks. Need to be split into two parts due to #598.
allFiles.splice( 0, 0, path.join( __dirname, '..', 'utils', 'automated-tests', 'leaksdetectorbefore.js' ).replace( /\\/g, '/' ) );
allFiles.push( path.join( __dirname, '..', 'utils', 'automated-tests', 'leaksdetectorafter.js' ).replace( /\\/g, '/' ) );
allFiles.splice( 0, 0, upath.join( __dirname, '..', 'utils', 'automated-tests', 'leaksdetectorbefore.js' ) );
allFiles.push( upath.join( __dirname, '..', 'utils', 'automated-tests', 'leaksdetectorafter.js' ) );

const entryFileContent = allFiles
.map( file => 'import "' + file + '";' );

// Inject the custom chai assertions. See ckeditor/ckeditor5#9668.
const assertionsDir = path.join( __dirname, '..', 'utils', 'automated-tests', 'assertions' ).replace( /\\/g, '/' );
const assertionsDir = upath.join( __dirname, '..', 'utils', 'automated-tests', 'assertions' );
const customAssertions = fs.readdirSync( assertionsDir ).map( assertionFileName => {
return [
assertionFileName,
path.parse( assertionFileName ).name.replace( /-([a-z])/g, value => value[ 1 ].toUpperCase() )
upath.parse( assertionFileName ).name.replace( /-([a-z])/g, value => value[ 1 ].toUpperCase() )
];
} );

Expand Down Expand Up @@ -209,7 +209,7 @@ function runKarma( options ) {
} );

if ( options.coverage ) {
const coveragePath = path.join( process.cwd(), 'coverage' );
const coveragePath = upath.join( process.cwd(), 'coverage' );

server.on( 'run_complete', () => {
// Use timeout to not write to the console in the middle of Karma's status.
Expand Down
1 change: 1 addition & 0 deletions packages/ckeditor5-dev-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"sinon-chai": "^3.5.0",
"socket.io": "^4.0.0",
"typescript": "5.0.4",
"upath": "^2.0.1",
"webpack": "^5.94.0"
},
"devDependencies": {
Expand Down
51 changes: 48 additions & 3 deletions packages/ckeditor5-dev-tests/tests/tasks/runautomatedtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* For licensing, see LICENSE.md.
*/

import path from 'path';
import fs from 'fs';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { globSync } from 'glob';
Expand All @@ -12,6 +11,7 @@ import chalk from 'chalk';
import karma from 'karma';
import karmaLogger from 'karma/lib/logger.js';
import transformFileOptionToTestGlob from '../../lib/utils/transformfileoptiontotestglob.js';
import upath from 'upath';

const stubs = vi.hoisted( () => ( {
log: {
Expand Down Expand Up @@ -349,7 +349,7 @@ describe( 'runAutomatedTests()', () => {
'/workspace/packages/ckeditor5-basic-styles/tests/italic.js'
] );

const assertionsDir = path.join( __dirname, '..', '..', 'lib', 'utils', 'automated-tests', 'assertions' ).replace( /\\/g, '/' );
const assertionsDir = upath.join( __dirname, '..', '..', 'lib', 'utils', 'automated-tests', 'assertions' );

const expectedEntryPointContent = [
`import assertionAFactory from "${ assertionsDir }/assertionA.js";`,
Expand Down Expand Up @@ -400,7 +400,7 @@ describe( 'runAutomatedTests()', () => {
'/workspace/packages/ckeditor5-basic-styles/tests/italic.js'
] );

const assertionsDir = path.join( __dirname, '..', '..', 'lib', 'utils', 'automated-tests', 'assertions' ).replace( /\\/g, '/' );
const assertionsDir = upath.join( __dirname, '..', '..', 'lib', 'utils', 'automated-tests', 'assertions' );

const expectedEntryPointContent = [
`import assertionAFactory from "${ assertionsDir }/assertion-a.js";`,
Expand Down Expand Up @@ -429,4 +429,49 @@ describe( 'runAutomatedTests()', () => {
expect.stringContaining( expectedEntryPointContent )
);
} );

it( 'should load custom assertions automatically (Windows paths)', async () => {
const options = {
files: [
'basic-styles'
],
production: true
};

vi.mocked( fs ).readdirSync.mockReturnValue( [ 'assertion-a.js', 'assertion-b.js' ] );

vi.mocked( transformFileOptionToTestGlob ).mockReturnValue( [
'\\workspace\\packages\\ckeditor5-basic-styles\\tests\\**\\*.js',
'\\workspace\\packages\\ckeditor-basic-styles\\tests\\**\\*.js'
] );

vi.mocked( globSync )
.mockReturnValue( [] )
.mockReturnValueOnce( [
'\\workspace\\packages\\ckeditor5-basic-styles\\tests\\bold.js',
'\\workspace\\packages\\ckeditor5-basic-styles\\tests\\italic.js'
] );

const promise = runAutomatedTests( options );

setTimeout( () => {
expect( stubs.karma.server.constructor ).toHaveBeenCalledOnce();

const [ firstCall ] = stubs.karma.server.constructor.mock.calls;
const [ , exitCallback ] = firstCall;

exitCallback( 0 );
} );

await promise;

expect( vi.mocked( mkdirp ).sync ).toHaveBeenCalledExactlyOnceWith( '/workspace/build/.automated-tests' );
expect( vi.mocked( fs ).writeFileSync ).toHaveBeenCalledExactlyOnceWith(
'/workspace/build/.automated-tests/entry-point.js',
expect.stringContaining( [
'import "/workspace/packages/ckeditor5-basic-styles/tests/bold.js";',
'import "/workspace/packages/ckeditor5-basic-styles/tests/italic.js";'
].join( '\n' ) )
);
} );
} );

0 comments on commit b70aa02

Please sign in to comment.