-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
133 lines (116 loc) · 4.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* @license Copyright (c) 2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
'use strict';
const fs = require( 'fs' );
const path = require( 'path' );
const meta = require( './src/meta' );
const tests = require( './src/tests' );
/**
* Returns relative path starting from a given folder based on absolute file path:
*
* filePath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.js'
* start: 'tests'
* returns: 'tests/core/ckeditor/ckeditor.js'
*
* @param {String} filePath Absolute path to a file.
* @param {String} start Start folder from which relative path should start.
* @returns {String|null} Relative path or null.
*/
function getRelativePath( filePath, start ) {
start = `${ path.sep }${ start }${ path.sep }`;
if ( filePath.indexOf( start ) !== -1 ) {
return path.join( start, filePath.split( start ).pop() ).substr( 1 );
}
return null;
}
/**
* Returns test name based on the absolute test file path. By default CKEditor tests are placed in `tests` directory.
* This directory is treated as a start of the test name:
*
* filePath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.js'
* returns: 'tests/core/ckeditor/ckeditor'
*
* @param {String} filePath Absolute path to a test file.
* @returns {String|null} Test name or null.
*/
function getTestName( filePath ) {
const testRelativePath = getRelativePath( filePath, 'tests' );
return testRelativePath ? testRelativePath.replace( /\.js$/, '' ) : null;
}
/**
* Returns test file info containing absolute and relative file paths. The relative path starts from default
* CKEditor tests folder which is `tests`:
*
* filePath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.js'
* returns: {
* path: 'tests/core/ckeditor/ckeditor.js'
* fullpath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.js'
* }
*
* @param {String} filePath Absolute path to a test file.
* @returns {Object} Object containing relative and absolute file paths.
* @returns {String} return.path Relative path to a test file.
* @returns {String} return.fullpath Absolute path to a test file.
*/
function getTestFileInfo( filePath ) {
return {
path: getRelativePath( filePath, 'tests' ),
fullpath: filePath
};
}
/**
* Returns test fixture file info containing absolute and relative file paths. The relative path starts from default
* CKEditor tests folder which is `tests`:
*
* filePath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.js'
* returns: {
* path: 'tests/core/ckeditor/ckeditor.html'
* fullpath: '/Workspace/CKSource/CKEditor4/ckeditor/ckeditor-dev/tests/core/ckeditor/ckeditor.html'
* }
*
* @param {String} filePath Absolute path to a test file.
* @returns {Object} Object containing relative and absolute file paths.
* @returns {String} return.path Relative path to a fixture file.
* @returns {String} return.fullpath Absolute path to a fixture file.
*/
function getFixtureFileInfo( filePath ) {
const fixturePath = filePath.replace( /\.js$/, '.html' );
if ( filePath && fs.existsSync( fixturePath ) ) {
return {
path: getRelativePath( fixturePath, 'tests' ),
fullpath: filePath
};
}
return null;
}
/**
* Creates preprocessor which converts bender tags (e.g. `/* bender-tags: editor *\/`) into JavaScript object which then
* can be accessed by tests. It also contains test name, test and fixture file paths.
*
* @returns {Function} Preprocessor function.
*/
function createCKEditor4Preprocessor() {
return ( content, file, done ) => {
const testFileInfo = getTestFileInfo( file.path );
content = tests.replaceTags( content, testFileInfo );
if ( tests.isTestFile( testFileInfo.path ) ) {
const tags = meta.parse( content ),
htmlFixture = getFixtureFileInfo( file.path );
tags.test = {
name: getTestName( file.path ),
file: getTestFileInfo( file.path )
};
if ( htmlFixture ) {
tags.test.fixture = htmlFixture;
}
done( tests.wrap( meta.remove( content ), tags ) );
} else {
done( content );
}
};
}
module.exports = {
'preprocessor:ckeditor4': [ 'factory', createCKEditor4Preprocessor ]
};