Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
acb9100
commit 6083a38
Showing
14 changed files
with
502 additions
and
268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import hammerhead from 'testcafe-hammerhead'; | ||
import { Compiler as LegacyTestFileCompiler } from 'testcafe-legacy-api'; | ||
import EsNextTestFileCompiler from './test-file/formats/es-next/compiler'; | ||
import TypeScriptTestFileCompiler from './test-file/formats/typescript/compiler'; | ||
import CoffeeScriptTestFileCompiler from './test-file/formats/coffeescript/compiler'; | ||
import RawTestFileCompiler from './test-file/formats/raw'; | ||
|
||
function createTestFileCompilers (options) { | ||
return [ | ||
new LegacyTestFileCompiler(hammerhead.processScript), | ||
new EsNextTestFileCompiler(), | ||
new TypeScriptTestFileCompiler(options), | ||
new CoffeeScriptTestFileCompiler(), | ||
new RawTestFileCompiler() | ||
]; | ||
} | ||
|
||
let testFileCompilers = []; | ||
|
||
export function getTestFileCompilers () { | ||
if (!testFileCompilers.length) | ||
initTestFileCompilers(); | ||
|
||
return testFileCompilers; | ||
} | ||
|
||
export function initTestFileCompilers (options = {}) { | ||
testFileCompilers = createTestFileCompilers(options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import debug from 'debug'; | ||
import { stat, readFile } from '../utils/promisified-functions'; | ||
import Option from './option'; | ||
import optionSource from './option-source'; | ||
import { cloneDeep, castArray } from 'lodash'; | ||
import resolvePathRelativelyCwd from '../utils/resolve-path-relatively-cwd'; | ||
import JSON5 from 'json5'; | ||
import renderTemplate from '../utils/render-template'; | ||
import WARNING_MESSAGES from '../notifications/warning-message'; | ||
import log from '../cli/log'; | ||
|
||
const DEBUG_LOGGER = debug('testcafe:configuration'); | ||
|
||
export default class Configuration { | ||
constructor (configurationFileName) { | ||
this._options = {}; | ||
this._filePath = resolvePathRelativelyCwd(configurationFileName); | ||
this._overridenOptions = []; | ||
} | ||
|
||
static _fromObj (obj) { | ||
const result = Object.create(null); | ||
|
||
Object.entries(obj).forEach(([key, value]) => { | ||
const option = new Option(key, value); | ||
|
||
result[key] = option; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
static async _isConfigurationFileExists (path) { | ||
try { | ||
await stat(path); | ||
|
||
return true; | ||
} | ||
catch (error) { | ||
DEBUG_LOGGER(renderTemplate(WARNING_MESSAGES.cannotFindConfigurationFile, path, error.stack)); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
static _showConsoleWarning (message) { | ||
log.write(message); | ||
} | ||
|
||
static _showWarningForError (error, warningTemplate, ...args) { | ||
const message = renderTemplate(warningTemplate, ...args); | ||
|
||
Configuration._showConsoleWarning(message); | ||
|
||
DEBUG_LOGGER(message); | ||
DEBUG_LOGGER(error); | ||
} | ||
|
||
async init () { | ||
} | ||
|
||
mergeOptions (options) { | ||
Object.entries(options).map(([key, value]) => { | ||
const option = this._ensureOption(key, value, optionSource.input); | ||
|
||
if (value === void 0) | ||
return; | ||
|
||
if (option.value !== value && | ||
option.source === optionSource.configuration) | ||
this._overridenOptions.push(key); | ||
|
||
this._setOptionValue(option, value); | ||
}); | ||
} | ||
|
||
getOption (key) { | ||
if (!key) | ||
return void 0; | ||
|
||
const option = this._options[key]; | ||
|
||
if (!option) | ||
return void 0; | ||
|
||
return option.value; | ||
} | ||
|
||
getOptions () { | ||
const result = Object.create(null); | ||
|
||
Object.entries(this._options).forEach(([name, option]) => { | ||
result[name] = option.value; | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
clone () { | ||
return cloneDeep(this); | ||
} | ||
|
||
get filePath () { | ||
return this._filePath; | ||
} | ||
|
||
async _load () { | ||
const configurationFileExists = await Configuration._isConfigurationFileExists(this.filePath); | ||
|
||
if (configurationFileExists) { | ||
const configurationFileContent = await this._readConfigurationFileContent(); | ||
|
||
if (configurationFileContent) | ||
return this._parseConfigurationFileContent(configurationFileContent); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async _readConfigurationFileContent () { | ||
try { | ||
return await readFile(this.filePath); | ||
} | ||
catch (error) { | ||
Configuration._showWarningForError(error, WARNING_MESSAGES.cannotReadConfigFile); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
_parseConfigurationFileContent (configurationFileContent) { | ||
try { | ||
return JSON5.parse(configurationFileContent); | ||
} | ||
catch (error) { | ||
Configuration._showWarningForError(error, WARNING_MESSAGES.cannotParseConfigFile); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
_ensureArrayOption (name) { | ||
const options = this._options[name]; | ||
|
||
if (!options) | ||
return; | ||
|
||
options.value = castArray(options.value); | ||
} | ||
|
||
_ensureOption (name, value, source) { | ||
let option = null; | ||
|
||
if (name in this._options) | ||
option = this._options[name]; | ||
else { | ||
option = new Option(name, value, source); | ||
|
||
this._options[name] = option; | ||
} | ||
|
||
return option; | ||
} | ||
|
||
_ensureOptionWithValue (name, defaultValue, source) { | ||
const option = this._ensureOption(name, defaultValue, source); | ||
|
||
if (option.value !== void 0) | ||
return; | ||
|
||
option.value = defaultValue; | ||
option.source = source; | ||
} | ||
|
||
_setOptionValue (option, value) { | ||
option.value = value; | ||
option.source = optionSource.input; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.