Skip to content

Commit

Permalink
working-ish implementation of mutator enable/disable comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bttmly committed Aug 5, 2016
1 parent ba2c459 commit c423243
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 54 deletions.
11 changes: 3 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
test: lint
test: compile
NODE_ENV=testing ./node_modules/.bin/_mocha ./test --recursive

test-bail: lint
test-bail: compile
NODE_ENV=testing ./node_modules/.bin/_mocha ./test --bail --recursive

lint:
./node_modules/.bin/eslint ./src/**/*.js

compile:
rm -rf ./built
./node_modules/.bin/tsc
Expand All @@ -15,10 +12,8 @@ example-events: compile
rm -rf ./.perturb
node ./script/run.js events

dogfood:
dogfood: compile
rm -rf ./.perturb
rm -rf ./built
./node_modules/.bin/tsc
node ./script/run.js dogfood $(RUNNER)

build:
Expand Down
45 changes: 19 additions & 26 deletions src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,53 @@ interface CommentedNode extends ESTree.Node {
}

function applyNodeComments (_node: ESTree.Node, disabledSet: Set<string>) {
const node = <CommentedNode>_node;
R.pipe(
_getComments,
R.chain(_extractOperators),
R.forEach(_applyOperator(disabledSet))
)(node);
getComments,
R.chain(extractOperators),
R.forEach(applyOperator(disabledSet))
)(<CommentedNode>_node);
}

const _applyOperator = R.curry(function (set: Set<string>, op: Operator) {
const applyOperator = R.curry(function (set: Set<string>, op: Operator) {
// perturb-disable: drop-return
switch (op.type) {
case "enable": {
console.log("ENABLE", op.name)
// console.log("ENABLE", op.name)
return set.add(op.name);
}
case "disable": {
console.log("DISABLE", op.name);
// console.log("DISABLE", op.name);
return set.delete(op.name);
}
}
// perturb-enable: drop-return
});

function _getComments (node: CommentedNode): Comment[] {
return [].concat(
function getComments (node: CommentedNode): Comment[] {
const out: Comment[] = [];
return out.concat(
node.leadingComments || [],
node.trailingComments || []
);
}

function _extractOperators (c: Comment): Operator[] {
function extractOperators (c: Comment): Operator[] {
const value = c.value.trim();

console.log("RAW VALUE", value);
const type = value.startsWith(PERTURB_ENABLE) ?
"enable" : value.startsWith(PERTURB_DISABLE) ?
"disable" : null;

let type;
if (value.startsWith(PERTURB_ENABLE)) {
type = "enable";
} else if (value.startsWith(PERTURB_DISABLE)) {
type = "disable";
}
if (type == null) return null;
if (type == null) return [];

return R.pipe(
R.split(":"),
R.prop("1"),
R.split(","),
R.filter(Boolean),
R.map(R.trim),
R.filter(Boolean),
R.map(name => ({ type, name: String(name) }))
)(value);
}

export = {
_getComments,
_extractOperators,
_applyOperator,
applyNodeComments,
};
export = applyNodeComments;
6 changes: 5 additions & 1 deletion src/file-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ function setupPerturbDirectory (config: PerturbConfig): void {
.filter(f => f !== config.perturbDir)
.filter(f => shouldSymlink.has(f))
.map(f => [path.join(projectRoot, f), path.join(pAbs, f)])
.forEach(R.apply(fs.symlinkSync))
// .forEach(R.apply(fs.symlinkSync))
.forEach(function (paths: string[]) {
const [src, dest] = paths;
fs.symlinkSync(src, dest);
})
}

function teardownPerturbDirectory (config): void {
Expand Down
25 changes: 21 additions & 4 deletions src/make-mutants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import esprima = require("esprima");
import escodegen = require("escodegen");
import estraverse = require("estraverse");

import applyNodeComments = require("./comments");
import mutators = require("./mutators");
import shouldSkip = require("./skippers");
import updateIn = require("./util/update-in");
const { getMutatorsForNode, hasAvailableMutations } = require("./mutators");

const PERTURB_ENABLE = "perturb-enable:";
const PERTURB_DISABLE = "perturb-disable:";
Expand All @@ -22,6 +23,9 @@ const FS_SETTINGS = {
encoding: "utf8",
};

// TODO: make this take the mutator plugins (index? finding func?) as an argument
// TODO: this function should not do the file reads, perhaps Match type needs the
// source code.
function makeMutants (match: Match): Mutant[] {
const { source, tests } = match;
const { ast, code } = parse(source);
Expand Down Expand Up @@ -58,23 +62,36 @@ function makeMutants (match: Match): Mutant[] {
};
});
})
)(getMutatorsForNode(node));
)(mutators.getMutatorsForNode(node));
}
}

type Path = string[];


// TODO: break this into it's own module
// and have it take the plugin index as an argument
function getMutationPaths (ast: ESTree.Node) {
const mutationPaths: Path[] = [];
const disabledMutations = new Set<string>();

estraverse.traverse(ast, {
enter: function (node: ESTree.Node) {
const path = <Path>this.path();
if (shouldSkip(node, path)) {
return this.skip();
}

if (hasAvailableMutations(node)) {
applyNodeComments(node, disabledMutations);

const plugins = mutators.getMutatorsForNode(node)
const active = plugins.filter(m => !disabledMutations.has(m.name))

// if (plugins.length !== active.length) {
// console.log("some are disabled, what we have is", [...active]);
// console.log(escodegen.generate(node));
// }

if (active.length) {
mutationPaths.push(path)
}
},
Expand Down
14 changes: 8 additions & 6 deletions src/mutators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,20 @@ function locateMutatorPlugins (names: string[]): MutatorPlugin[] {
// index = locateMutatorPlugins(names);
// }

exports.hasAvailableMutations = function (node: ESTree.Node): boolean {
if (node == null || node.type == null) return false;
return R.has(node.type, index);
}
// exports.hasAvailableMutations = function (node: ESTree.Node): boolean {
// if (node == null || node.type == null) return false;
// return R.has(node.type, index);
// }

exports.getMutatorsForNode = function (node: ESTree.Node): MutatorPlugin[] {
function getMutatorsForNode (node: ESTree.Node): MutatorPlugin[] {
if (node == null || node.type == null) return [];
return <MutatorPlugin[]>R.propOr([], node.type, index);
}

exports.getMutatorByName = function (name: string): MutatorPlugin | undefined {
function getMutatorByName (name: string): MutatorPlugin | undefined {
return activeMutators.find(m => m.name === name);
}

index = makeMutatorIndex([]);

export = { getMutatorsForNode, getMutatorByName };
9 changes: 6 additions & 3 deletions src/runners/fork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ async function childRunner (name) {
const runner: RunnerPlugin = require("./")(name);
const result = await runMutant(runner, mutant);

process.send(JSON.stringify({
error: runnerUtils.makeErrorSerializable(result.error)
}));
const message = result.error == null ?
{} : { error: runnerUtils.makeErrorSerializable(result.error) }

if (process.send != null) {
process.send(JSON.stringify(message));
}

return null;
}
Expand Down
13 changes: 7 additions & 6 deletions test/comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const expect = require("expect");
const esprima = require("esprima");
const comments = require("../built/comments");
const applyNodeComments = require("../built/comments");

const helpers = require("./helpers");

Expand All @@ -15,15 +15,14 @@ function createTest (obj) {
const node = helpers.nodeFromCode(obj.code);
const set = obj.set || new Set();
const expected = obj.expected;
comments.applyNodeComments(node, set);
applyNodeComments(node, set);
expect([...set]).toEqual(expected);
});
}


describe("comments", function () {

describe("comments", function () {
describe("applyNodeComments", function () {

createTest({
title: "enable: works for leading line comments",
Expand Down Expand Up @@ -54,10 +53,12 @@ describe("comments", function () {
});

createTest({
title: "disable: works for leading and trailing comments",
title: "interleaved enable/disable works",
expected: ["b"],
code: `
// perturb-enable: a,b
// perturb-enable: a,b,,c
// perturb-disable: c,b
// perturb-enable: b
const x = 1;
// perturb-disable: a
`,
Expand Down

0 comments on commit c423243

Please sign in to comment.