more
This commit is contained in:
dopeuni444
2025-07-31 12:23:33 +04:00
parent 20b46678b7
commit b5a22951ae
3401 changed files with 331100 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
Copyright 2012-2015 Yahoo! Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yahoo! Inc. nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL YAHOO! INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

View File

@@ -0,0 +1,132 @@
'use strict';
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
const fs = require('fs');
const FileWriter = require('./file-writer');
const XMLWriter = require('./xml-writer');
const tree = require('./tree');
const watermarks = require('./watermarks');
const SummarizerFactory = require('./summarizer-factory');
function defaultSourceLookup(path) {
try {
return fs.readFileSync(path, 'utf8');
} catch (ex) {
throw new Error(`Unable to lookup source: ${path} (${ex.message})`);
}
}
function normalizeWatermarks(specified = {}) {
Object.entries(watermarks.getDefault()).forEach(([k, value]) => {
const specValue = specified[k];
if (!Array.isArray(specValue) || specValue.length !== 2) {
specified[k] = value;
}
});
return specified;
}
/**
* A reporting context that is passed to report implementations
* @param {Object} [opts=null] opts options
* @param {String} [opts.dir='coverage'] opts.dir the reporting directory
* @param {Object} [opts.watermarks=null] opts.watermarks watermarks for
* statements, lines, branches and functions
* @param {Function} [opts.sourceFinder=fsLookup] opts.sourceFinder a
* function that returns source code given a file path. Defaults to
* filesystem lookups based on path.
* @constructor
*/
class Context {
constructor(opts) {
this.dir = opts.dir || 'coverage';
this.watermarks = normalizeWatermarks(opts.watermarks);
this.sourceFinder = opts.sourceFinder || defaultSourceLookup;
this._summarizerFactory = new SummarizerFactory(
opts.coverageMap,
opts.defaultSummarizer
);
this.data = {};
}
/**
* returns a FileWriter implementation for reporting use. Also available
* as the `writer` property on the context.
* @returns {Writer}
*/
getWriter() {
return this.writer;
}
/**
* returns the source code for the specified file path or throws if
* the source could not be found.
* @param {String} filePath the file path as found in a file coverage object
* @returns {String} the source code
*/
getSource(filePath) {
return this.sourceFinder(filePath);
}
/**
* returns the coverage class given a coverage
* types and a percentage value.
* @param {String} type - the coverage type, one of `statements`, `functions`,
* `branches`, or `lines`
* @param {Number} value - the percentage value
* @returns {String} one of `high`, `medium` or `low`
*/
classForPercent(type, value) {
const watermarks = this.watermarks[type];
if (!watermarks) {
return 'unknown';
}
if (value < watermarks[0]) {
return 'low';
}
if (value >= watermarks[1]) {
return 'high';
}
return 'medium';
}
/**
* returns an XML writer for the supplied content writer
* @param {ContentWriter} contentWriter the content writer to which the returned XML writer
* writes data
* @returns {XMLWriter}
*/
getXMLWriter(contentWriter) {
return new XMLWriter(contentWriter);
}
/**
* returns a full visitor given a partial one.
* @param {Object} partialVisitor a partial visitor only having the functions of
* interest to the caller. These functions are called with a scope that is the
* supplied object.
* @returns {Visitor}
*/
getVisitor(partialVisitor) {
return new tree.Visitor(partialVisitor);
}
getTree(name = 'defaultSummarizer') {
return this._summarizerFactory[name];
}
}
Object.defineProperty(Context.prototype, 'writer', {
enumerable: true,
get() {
if (!this.data.writer) {
this.data.writer = new FileWriter(this.dir);
}
return this.data.writer;
}
});
module.exports = Context;

View File

@@ -0,0 +1,189 @@
'use strict';
/*
Copyright 2012-2015, Yahoo Inc.
Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
const path = require('path');
const fs = require('fs');
const mkdirp = require('make-dir');
const supportsColor = require('supports-color');
/**
* Base class for writing content
* @class ContentWriter
* @constructor
*/
class ContentWriter {
/**
* returns the colorized version of a string. Typically,
* content writers that write to files will return the
* same string and ones writing to a tty will wrap it in
* appropriate escape sequences.
* @param {String} str the string to colorize
* @param {String} clazz one of `high`, `medium` or `low`
* @returns {String} the colorized form of the string
*/
colorize(str /*, clazz*/) {
return str;
}
/**
* writes a string appended with a newline to the destination
* @param {String} str the string to write
*/
println(str) {
this.write(`${str}\n`);
}
/**
* closes this content writer. Should be called after all writes are complete.
*/
close() {}
}
/**
* a content writer that writes to a file
* @param {Number} fd - the file descriptor
* @extends ContentWriter
* @constructor
*/
class FileContentWriter extends ContentWriter {
constructor(fd) {
super();
this.fd = fd;
}
write(str) {
fs.writeSync(this.fd, str);
}
close() {
fs.closeSync(this.fd);
}
}
// allow stdout to be captured for tests.
let capture = false;
let output = '';
/**
* a content writer that writes to the console
* @extends ContentWriter
* @constructor
*/
class ConsoleWriter extends ContentWriter {
write(str) {
if (capture) {
output += str;
} else {
process.stdout.write(str);
}
}
colorize(str, clazz) {
const colors = {
low: '31;1',
medium: '33;1',
high: '32;1'
};
/* istanbul ignore next: different modes for CI and local */
if (supportsColor.stdout && colors[clazz]) {
return `\u001b[${colors[clazz]}m${str}\u001b[0m`;
}
return str;
}
}
/**
* utility for writing files under a specific directory
* @class FileWriter
* @param {String} baseDir the base directory under which files should be written
* @constructor
*/
class FileWriter {
constructor(baseDir) {
if (!baseDir) {
throw new Error('baseDir must be specified');
}
this.baseDir = baseDir;
}
/**
* static helpers for capturing stdout report output;
* super useful for tests!
*/
static startCapture() {
capture = true;
}
static stopCapture() {
capture = false;
}
static getOutput() {
return output;
}
static resetOutput() {
output = '';
}
/**
* returns a FileWriter that is rooted at the supplied subdirectory
* @param {String} subdir the subdirectory under which to root the
* returned FileWriter
* @returns {FileWriter}
*/
writerForDir(subdir) {
if (path.isAbsolute(subdir)) {
throw new Error(
`Cannot create subdir writer for absolute path: ${subdir}`
);
}
return new FileWriter(`${this.baseDir}/${subdir}`);
}
/**
* copies a file from a source directory to a destination name
* @param {String} source path to source file
* @param {String} dest relative path to destination file
* @param {String} [header=undefined] optional text to prepend to destination
* (e.g., an "this file is autogenerated" comment, copyright notice, etc.)
*/
copyFile(source, dest, header) {
if (path.isAbsolute(dest)) {
throw new Error(`Cannot write to absolute path: ${dest}`);
}
dest = path.resolve(this.baseDir, dest);
mkdirp.sync(path.dirname(dest));
let contents;
if (header) {
contents = header + fs.readFileSync(source, 'utf8');
} else {
contents = fs.readFileSync(source);
}
fs.writeFileSync(dest, contents);
}
/**
* returns a content writer for writing content to the supplied file.
* @param {String|null} file the relative path to the file or the special
* values `"-"` or `null` for writing to the console
* @returns {ContentWriter}
*/
writeFile(file) {
if (file === null || file === '-') {
return new ConsoleWriter();
}
if (path.isAbsolute(file)) {
throw new Error(`Cannot write to absolute path: ${file}`);
}
file = path.resolve(this.baseDir, file);
mkdirp.sync(path.dirname(file));
return new FileContentWriter(fs.openSync(file, 'w'));
}
}
module.exports = FileWriter;