accessibility-checker
Version:
An automated testing tools for accessibility testing using Puppeteer, Selenium, or Zombie
406 lines (403 loc) • 22.5 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ACConfigManager = void 0;
/******************************************************************************
Copyright:: 2020- IBM, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*****************************************************************************/
// Load all the modules that are needed
var pathLib = require("path");
var fs = require("fs");
var YAML = require("js-yaml");
var ACConstants_1 = require("./ACConstants");
var uuid_1 = require("uuid");
var axios_1 = require("axios");
/**
* This function is responsible converting policies into an Array based on string or Array.
*
* i.e. convert array into string version of array
* "CI162_5_2_DCP070116,CI162_5_2_DCP070116"
*
* converts to:
*
* [
* "CI162_5_2_DCP070116",
* "CI162_5_2_DCP070116"
* ]
*
* @param {Array or String} policies - Provide list of policies, single policy or comma seperated policies
*
* @return {Array} policies - return the policy converted into Array version
*
* @memberOf this
*/
function convertPolicies(policies) {
ACConstants_1.ACConstants.DEBUG && console.log("START 'convertPolicies' function");
ACConstants_1.ACConstants.DEBUG && console.log("Converting policy provided to Array: ");
ACConstants_1.ACConstants.DEBUG && console.log(policies);
ACConstants_1.ACConstants.DEBUG && console.log("END 'convertPolicies' function");
// In the case policies is an Array return it as engine expects list
if (policies instanceof Array) {
return policies;
}
// If the policies is string, we need to convert it to an array. Which includes comma seperated string support also
else if (typeof policies === "string") {
return policies.split(',');
}
return policies;
}
/**
* This function is responsible processing the achecker config which was initialized to make sure it contains,
* information which matches what the engine reads.
*
* i.e.
* Need to change reportLevels and failLevels to match with level declerations in the engine.
* replace violation with level.violation
* Need to change array of policies into a string
* ["CI162_5_2_DCP070116","CI162_5_2_DCP070116"] to "CI162_5_2_DCP070116,CI162_5_2_DCP070116"
*
* @param {Object} ACConfig - Provide the config object in which needs to be processed.
*
* @return {Object} ACConfig - return the config object which has been made engine readable
*
* @memberOf this
*/
function processACConfig(ACConfig) {
return __awaiter(this, void 0, void 0, function () {
var ruleServer, ruleArchiveFile, ruleArchiveParse, response, err_1, ruleArchivePath, ruleArchiveVersion, ruleArchive, i, errStr, errStr;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ACConstants_1.ACConstants.DEBUG && console.log("START 'processACConfig' function");
// Convert the reportLevels and failLevels to match with what the engine provides
// Don't need to convert the levels from the input as we are going to compare with out the level.
// by using contains algo, so that in the reports we can add it without level, until the engine is
// updated to pass back with out level.
//ACConfig.reportLevels = mapLevelsToEngineReadableLevels(ACConfig.reportLevels);
//ACConfig.failLevels = mapLevelsToEngineReadableLevels(ACConfig.failLevels);
// Convert the policies into a comma seperated string
ACConfig.policies = convertPolicies(ACConfig.policies);
ruleServer = ACConfig.ruleServer ? ACConfig.ruleServer : ACConstants_1.ACConstants.ruleServer;
ruleArchiveFile = "".concat(ruleServer).concat(ruleServer.includes("jsdelivr.net") ? "@next" : "", "/archives.json");
_a.label = 1;
case 1:
_a.trys.push([1, 4, , 5]);
if (ACConfig.ignoreHTTPSErrors) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
}
return [4 /*yield*/, axios_1.default.get(ruleArchiveFile)];
case 2:
response = _a.sent();
return [4 /*yield*/, response.data];
case 3:
ruleArchiveParse = _a.sent();
return [3 /*break*/, 5];
case 4:
err_1 = _a.sent();
console.log(err_1);
throw new Error(err_1);
case 5:
ruleArchivePath = null;
ruleArchiveVersion = null;
if (ruleArchiveParse && ruleArchiveParse.length > 0) {
ACConstants_1.ACConstants.DEBUG && console.log("Found archiveFile: " + ruleArchiveFile);
ACConfig.ruleArchiveSet = ruleArchiveParse;
ruleArchive = ACConfig.ruleArchive;
ACConfig.ruleArchiveLabel = ACConfig.ruleArchive;
for (i = 0; i < ACConfig.ruleArchiveSet.length; i++) {
if (ruleArchive == ACConfig.ruleArchiveSet[i].id && !ACConfig.ruleArchiveSet[i].sunset) {
ruleArchivePath = ACConfig.ruleArchiveSet[i].path;
ruleArchiveVersion = ACConfig.ruleArchiveSet[i].version;
ACConfig.ruleArchiveLabel = ruleArchiveParse[i].name + " (" + ruleArchiveParse[i].id + ")";
break;
}
}
if (!ruleArchivePath || ruleArchiveVersion === null) {
errStr = "[ERROR] RuleArchiveInvalid: Make Sure correct rule archive is provided in the configuration file. More information is available in the README.md";
console.error(errStr);
throw new Error(errStr);
}
//}
}
else {
errStr = "[ERROR] UnableToParseArchive: Archives are unable to be parse. Contact support team.";
console.error(errStr);
throw new Error(errStr);
}
// Build the new rulePack based of the baseA11yServerURL
if (ruleServer.includes("jsdelivr.net")) {
ACConfig.rulePack = "".concat(ruleServer, "@").concat(ruleArchiveVersion);
}
else {
ACConfig.rulePack = "".concat(ruleServer).concat(ruleArchivePath, "/js");
}
ACConfig.ruleServer = ruleServer;
ACConstants_1.ACConstants.DEBUG && console.log("Built new rulePack: " + ACConfig.rulePack);
ACConstants_1.ACConstants.DEBUG && console.log("END 'processACConfig' function");
// Return the updated ACConfig object
return [2 /*return*/, ACConfig];
}
});
});
}
/**
* This function is responsible initializing all the default values for the configurations, in the case any
* of the config options are missing.
*
* @param {Object} config - Provide the config object in which we need to initialize the default values.
*
* @return {Object} config - return the config object which has all the default values, in the case
* some of the options are null or undefined.
*
* @memberOf this
*/
function initializeDefaults(config) {
ACConstants_1.ACConstants.DEBUG && console.log("START 'initializeDefaults' function");
ACConstants_1.ACConstants.DEBUG && console.log("Config before initialization: ");
ACConstants_1.ACConstants.DEBUG && console.log(config);
// Make sure all the following options are defined, otherwise reset them to default values.
config.ruleArchiveLabel = config.ruleArchiveLabel || ACConstants_1.ACConstants.ruleArchiveLabel || config.ruleArchive;
// For capture screenshots need to check for null or undefined and then set default otherwise it will evaluate the
// boolean which causes it to always comply with the default value and not user provided option
if (config.captureScreenshots === null || config.captureScreenshots === undefined || typeof config.captureScreenshots === "undefined") {
config.captureScreenshots = ACConstants_1.ACConstants.captureScreenshots;
}
// Load in the package.json file so that we can extract the module name and the version to build
// a toolID which needs to be used when results are build for the purpose of keeping track of
// which tool is uploading the results.
var packageObject = require('../package.json');
// Build the toolID based on name and version
config.toolID = packageObject.name + "-v" + packageObject.version;
// Using the uuid module generate a uuid number which is used to assoiciate to the scans that
// are done for a single run of karma.
config.scanID = (0, uuid_1.v4)();
for (var key in ACConstants_1.ACConstants) {
config[key] = config[key] || ACConstants_1.ACConstants[key];
}
ACConstants_1.ACConstants.DEBUG && console.log("Config after initialization: ");
ACConstants_1.ACConstants.DEBUG && console.log(config);
ACConstants_1.ACConstants.DEBUG && console.log("END 'initializeDefaults' function");
}
/**
* This function is responsible reading in the .yaml or .yml or .json and set the config options based on this.
*
* @return {Object} config - return the config object that was read in, refer to function initializeDefaults
* to view how the object is to be constructed.
*
* @memberOf this
*/
function loadConfigFromYAMLorJSONFile() {
ACConstants_1.ACConstants.DEBUG && console.log("START 'loadConfigFromYAMLorJSONFile' function");
// Variable Decleration
var config = {};
// Get the current working directory, where we will look for the yaml, yml or json file
var workingDir = process.cwd();
ACConstants_1.ACConstants.DEBUG && console.log("Working directory set to: " + workingDir);
// List of files to look for in that order, in the case even one is found we stop and load that as the config.
// Theses files will be checked for in the working directory:
// ./.achecker.yml
// ./.achecker.yaml
// ./achecker.js
// ./achecker.json
// ./.config/.achecker.yml
// ./.config/.achecker.yaml
// ./.config/achecker.js
// ./.config/achecker.json
// The node module, require will load js or json depending on which one is present, in the case
// both json and js are present it loads js first.
// Refer to ACConstants.js for more details
var configFiles = ACConstants_1.ACConstants.configFiles;
// Loop over all the possible location where the config file can reside, if one is found load it and break out.
for (var i = 0; i < configFiles.length; i++) {
// Get the full path to the config file we are going to check
var fileToCheck = pathLib.join(workingDir, configFiles[i]);
ACConstants_1.ACConstants.DEBUG && console.log("Checking file: " + fileToCheck);
// Get the extension of the file we are about to scan
var fileExtension = fileToCheck.substr(fileToCheck.lastIndexOf('.') + 1);
// If this is a yml or yaml file verify that the file exists and then load as such.
if (fileExtension === "yml" || fileExtension === "yaml") {
// Start checking which files exists, if it exists then load it as the config that was read in
// We only allow specifying a file, and the order it checks is based on what is specified in the array.
// i.e. So in the case that it finds yml, it will load and not check the rest, etc...
if (fs.existsSync(fileToCheck)) {
ACConstants_1.ACConstants.DEBUG && console.log("File: " + fileToCheck + " exists loading it.");
ACConstants_1.ACConstants.DEBUG && console.log("Loading as YAML file.");
// Load in as yml or yaml file and return this object
return YAML.load(fs.readFileSync(fileToCheck), 'utf8');
}
}
else {
ACConstants_1.ACConstants.DEBUG && console.log("Trying to load as json or js.");
// Need to use try/catch mech so that in the case the require throws an exception, we can
// catch this and discatd the error, as in the case there is no config file provided then
// we load in default values.
try {
// call the resolve to check if the file exists or not, and get the actualy path if it was js or json
var jsOrJSONFile = require.resolve(fileToCheck);
// Only try to load the achecker js or json file if it exists.
if (jsOrJSONFile) {
ACConstants_1.ACConstants.DEBUG && console.log("Loading: " + jsOrJSONFile);
// Load in as json or js and return this object
return require(fileToCheck);
}
}
catch (e) {
ACConstants_1.ACConstants.DEBUG && console.log("JSON or JS file does not exists, will load default config.");
}
}
}
ACConstants_1.ACConstants.DEBUG && console.log("END 'loadConfigFromYAMLorJSONFile' function");
return config;
}
/**
* This function is responsible for processing the karma configuration for accessibility-checker.
* The ACConfig provided in the Karma configuration will be processed by this
* function and then the config variables will be assoiciated to the global space so that
* they can be accessed from window.__karma__.config
*
* @param {Object} config - All the Karma configuration, we will extract what we need from this over
* all object, we need the entire object so that we can reasign some config
* variables to global scope so that all karma testscases/scripts can access
* them.
*
* @return - N/A - Object will be processed and all the params that are needed for this module will
* be extracted and then the entire object will be added to global space.
*
* @memberOf this
*/
function processConfiguration(config) {
return __awaiter(this, void 0, void 0, function () {
var ACConfig, configFromFile;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
ACConstants_1.ACConstants.DEBUG && console.log("START 'processConfiguration' function");
ACConfig = null;
configFromFile = null;
// Read in the .yaml (.yml) or .json file to load in the configuration
configFromFile = loadConfigFromYAMLorJSONFile();
ACConstants_1.ACConstants.DEBUG && console.log("Loaded config from file: ");
ACConstants_1.ACConstants.DEBUG && console.log(configFromFile);
// In the case configuration was provided in a yaml, yml or json file, then set this as the configuration
// otherwise load them from the Karma configuration.
if (configFromFile !== null && typeof (configFromFile) !== "undefined" && Object.keys(configFromFile).length !== 0) {
ACConstants_1.ACConstants.DEBUG && console.log("Using config which was loaded from config file.");
ACConfig = configFromFile;
}
else if (config !== null && typeof (config) !== "undefined" && Object.keys(config).length !== 0) {
// Extract the ACConfig from the overall karma configuration, in the case config file
// was not provided.
ACConfig = config;
}
else {
ACConfig = JSON.parse(JSON.stringify(ACConstants_1.ACConstants));
}
// In the case the ACConfig object is not defined, then define it with default config options so
// it can be set in window.__karma__.config.ACConfig, so that we know even in the testcases, other
// wrapper scripts that there was nothing defined at all, and at the same time to make sure that this
// code was actually executed.
initializeDefaults(ACConfig);
// Now we process the final accessibility-checker config object that is build to make sure it is valid, also need to perform some
// mapping for provided paremeters to actualy read by the engine.
return [4 /*yield*/, processACConfig(ACConfig)];
case 1:
// Now we process the final accessibility-checker config object that is build to make sure it is valid, also need to perform some
// mapping for provided paremeters to actualy read by the engine.
_a.sent();
// In the case the Karma config is set to config.LOG_DEBUG then also enable the accessibility-checker debuging
ACConfig.DEBUG = ACConstants_1.ACConstants.DEBUG;
ACConstants_1.ACConstants.DEBUG && console.log("END 'processConfiguration' function");
return [2 /*return*/, ACConfig];
}
});
});
}
var config = null;
var ACConfigManager = /** @class */ (function () {
function ACConfigManager() {
}
ACConfigManager.setConfig = function (inConfig) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, processConfiguration(inConfig)];
case 1:
config = _a.sent();
return [2 /*return*/];
}
});
});
};
ACConfigManager.getConfig = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getConfigUnsupported()];
});
});
};
ACConfigManager.getConfigUnsupported = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!!config) return [3 /*break*/, 2];
return [4 /*yield*/, ACConfigManager.setConfig()];
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/, config];
}
});
});
};
ACConfigManager.getConfigNow = function () {
return config;
};
return ACConfigManager;
}());
exports.ACConfigManager = ACConfigManager;
//# sourceMappingURL=ACConfigManager.js.map
;