diff --git a/.vscode/launch.json b/.vscode/launch.json index c77b2adf2..148f45d8a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,7 @@ "type": "extensionHost", "request": "launch", "runtimeExecutable": "${execPath}", - "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ], + "args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test"], "stopOnEntry": false, "sourceMaps": true, "outDir": "${workspaceRoot}/out/test", diff --git a/package.json b/package.json index 312c0e564..5b67baa0e 100644 --- a/package.json +++ b/package.json @@ -40,13 +40,15 @@ "postinstall": "node ./node_modules/vscode/bin/install" }, "devDependencies": { - "typescript": "^2.0.3", - "vscode": "^1.0.0", - "mocha": "^2.3.3", + "@types/babylon": "^6.7.14", + "@types/mocha": "^2.2.32", "@types/node": "^6.0.40", - "@types/mocha": "^2.2.32" + "mocha": "^2.3.3", + "typescript": "^2.0.3", + "vscode": "^1.0.0" }, "dependencies": { + "babylon": "^6.13.0", "tmp": "0.0.29" } } diff --git a/src/jest_runner.ts b/src/jest_runner.ts index 4c645c560..23c30326c 100644 --- a/src/jest_runner.ts +++ b/src/jest_runner.ts @@ -12,7 +12,7 @@ export class JestRunner extends EventEmitter { constructor() { super(); - var runtimeArgs = ['.', '--json', '--useStderr', '--watch', '--coverage', 'true ', '--colors', 'false', "--verbose"]; + var runtimeArgs = ['.', '--json', '--useStderr', '--watch', '--colors', 'false', "--verbose"]; var runtimeExecutable: string; runtimeExecutable = "node_modules/.bin/jest" @@ -49,20 +49,12 @@ export class JestRunner extends EventEmitter { this.emit('terminalError', "Process failed: " + error.message); }); - this.debugprocess.on('disconnect', () => { - console.log("DDD") - }); - - this.debugprocess.on('message', () => { - console.log("MMM") - }); - this.debugprocess.on('close', () => { console.log("Jest Closed") }); } - // This doens't work... + // This doesn't work yet... public triggerFullTestSuite() { this.debugprocess.stdin.write("o") } diff --git a/src/test_file_parser.ts b/src/test_file_parser.ts new file mode 100644 index 000000000..96419f2e8 --- /dev/null +++ b/src/test_file_parser.ts @@ -0,0 +1,91 @@ +'use strict' + +import fs = require('fs'); + +import {basename, dirname} from 'path'; +import * as path from 'path'; + +// var esprima = require('esprima'); +import * as babylon from 'babylon' + +interface Location { + line: number + column: number +} + +export class ItBlock { + name: string + file: string + start: Location + end: Location + + updateWithNode(node: any){ + this.start = node.loc.start + this.end = node.loc.end + this.name = node.expression.arguments[0].value + } +} + +export default class TestFileParser { + + itBlocks: ItBlock[] + + async run(file: string): Promise { + let data = await this.generateAST(file) + this.itBlocks = [] + this.findItBlocksInBody(data["program"]) + return data + } + + foundItNode(node){ + let it = new ItBlock() + it.updateWithNode(node) + this.itBlocks.push(it) + } + + isAnIt(node) { + return ( + node.type === "ExpressionStatement" && + node.expression.type === "CallExpression" + ) + && + ( + node.expression.callee.name === "it" || + node.expression.callee.name === "test" + ) + } + + isADescribe(node) { + return node.type === "ExpressionStatement" && + node.expression.type === "CallExpression" && + node.expression.callee.name === "describe" + } + + findItBlocksInBody(root) { + for (var node in root.body) { + if (root.body.hasOwnProperty(node)) { + var element = root.body[node]; + if (this.isADescribe(element)){ + if (element.expression.arguments.length == 2) { + let newBody = element.expression.arguments[1].body + this.findItBlocksInBody(newBody); + } + } + if (this.isAnIt(element)) { + this.foundItNode(element) + } + } + } + } + + generateAST(file: string): Promise { + return new Promise((resolve, reject) =>{ + var parentDir = path.resolve(process.cwd(), '..'); + + fs.readFile(file, "utf8", (err, data) => { + if (err) { return reject(err.message) } + resolve(babylon.parse(data, { sourceType:"module", plugins: ["jsx", "flow"] })) + }) + }) + } +} \ No newline at end of file diff --git a/test/fixtures/dangerjs/travis-ci.jstest.js b/test/fixtures/dangerjs/travis-ci.jstest.js new file mode 100644 index 000000000..5ff3f9b23 --- /dev/null +++ b/test/fixtures/dangerjs/travis-ci.jstest.js @@ -0,0 +1,70 @@ +import Travis from "../travis.js" + +let correctEnv = { + "HAS_JOSH_K_SEAL_OF_APPROVAL": "true", + "TRAVIS_PULL_REQUEST": "800", + "TRAVIS_REPO_SLUG": "artsy/eigen" +} + +describe(".isCI", () => { + test("validates when all Travis environment vars are set and Josh K says so", () => { + let travis = new Travis(correctEnv) + expect(travis.isCI).toBeTruthy() + }) + + test("does not validate without josh", () => { + let travis = new Travis({}) + expect(travis.isCI).toBeFalsy() + }) +}) + +describe(".isPR", () => { + test("validates when all Travis environment vars are set and Josh K says so", () => { + let travis = new Travis(correctEnv) + expect(travis.isPR).toBeTruthy() + }) + + test("does not validate without josh", () => { + let travis = new Travis({}) + expect(travis.isPR).toBeFalsy() + }) + + let envs = ["TRAVIS_PULL_REQUEST", "TRAVIS_REPO_SLUG"] + envs.forEach((key: string) => { + var env = { + "HAS_JOSH_K_SEAL_OF_APPROVAL": "true", + "TRAVIS_PULL_REQUEST": "800", + "TRAVIS_REPO_SLUG": "artsy/eigen" + } + env[key] = null + + test(`does not validate when ${key} is missing`, () => { + let travis = new Travis({}) + expect(travis.isPR).toBeFalsy() + }) + }) + + it("needs to have a PR number", () => { + var env = { + "HAS_JOSH_K_SEAL_OF_APPROVAL": "true", + "TRAVIS_PULL_REQUEST": "asdasd", + "TRAVIS_REPO_SLUG": "artsy/eigen" + } + let travis = new Travis(env) + expect(travis.isPR).toBeFalsy() + }) +}) + +describe(".pullReuestID", () => { + it("pulls it out of the env", () => { + let travis = new Travis(correctEnv) + expect(travis.pullRequestID).toEqual("800") + }) +}) + +describe(".repoSlug", () => { + it("pulls it out of the env", () => { + let travis = new Travis(correctEnv) + expect(travis.repoSlug).toEqual("artsy/eigen") + }) +}) diff --git a/test/fixtures/global_its.js b/test/fixtures/global_its.js new file mode 100644 index 000000000..541373dc8 --- /dev/null +++ b/test/fixtures/global_its.js @@ -0,0 +1,7 @@ +it("works with old functions", function() { + +}) + +it("works with new functions", () => { + +}) \ No newline at end of file diff --git a/test/fixtures/nested_its.js b/test/fixtures/nested_its.js new file mode 100644 index 000000000..538331345 --- /dev/null +++ b/test/fixtures/nested_its.js @@ -0,0 +1,11 @@ +describe("some context", () => { + it("1", function() { + }) + it("2", function() { + }) +}) + +describe("some other context", function() { + it("3", () => { + }) +}) \ No newline at end of file diff --git a/test/test_file_parser.test.ts b/test/test_file_parser.test.ts new file mode 100644 index 000000000..5f1f888bd --- /dev/null +++ b/test/test_file_parser.test.ts @@ -0,0 +1,55 @@ +// The module 'assert' provides assertion methods from node +import * as assert from 'assert'; + +// You can import and use all API from the 'vscode' module +// as well as import your extension to test it +import * as vscode from 'vscode'; +import * as myExtension from '../src/extension'; + +import Parser from '../src/test_file_parser' + +suite("File Parsing", () => { + + test("For the simplest global case", async () => { + let parser = new Parser() + await parser.run(__dirname + "/../../test/fixtures/global_its.js") + assert.equal(parser.itBlocks.length, 2) + + let firstIt = parser.itBlocks[0] + assert.equal(firstIt.name, "works with old functions") + assert.notStrictEqual(firstIt.start, { line: 1, column: 0 }) + assert.notStrictEqual(firstIt.end, { line: 3, column: 0 }) + + let secondIt = parser.itBlocks[1] + assert.equal(secondIt.name, "works with new functions") + assert.notStrictEqual(secondIt.start, { line: 5, column: 0 }) + assert.notStrictEqual(secondIt.end, { line: 7, column: 0 }) + }); + + test("For its inside describes", async () => { + let parser = new Parser() + await parser.run(__dirname + "/../../test/fixtures/nested_its.js") + assert.equal(parser.itBlocks.length, 3) + + let firstIt = parser.itBlocks[0] + assert.equal(firstIt.name, "1") + assert.deepEqual(firstIt.start, { line: 2, column: 4 }) + assert.deepEqual(firstIt.end, { line: 3, column: 6 }) + + let secondIt = parser.itBlocks[1] + assert.equal(secondIt.name, "2") + assert.deepEqual(secondIt.start, { line: 4, column: 4 }) + assert.deepEqual(secondIt.end, { line: 5, column: 6 }) + + let thirdIt = parser.itBlocks[2] + assert.equal(thirdIt.name, "3") + assert.deepEqual(thirdIt.start, { line: 9, column: 4 }) + assert.deepEqual(thirdIt.end, { line: 10, column: 6 }) + }); + + test("For a danger test file (which has flow annotations)", async () => { + let parser = new Parser() + await parser.run(__dirname + "/../../test/fixtures/dangerjs/travis-ci.jstest.js") + assert.equal(parser.itBlocks.length, 7) + }) +}); \ No newline at end of file