Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST Parse for it/test blocks #1

Merged
merged 2 commits into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 2 additions & 10 deletions src/jest_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
91 changes: 91 additions & 0 deletions src/test_file_parser.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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<babylon.Node> {
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"] }))
})
})
}
}
70 changes: 70 additions & 0 deletions test/fixtures/dangerjs/travis-ci.jstest.js
Original file line number Diff line number Diff line change
@@ -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")
})
})
7 changes: 7 additions & 0 deletions test/fixtures/global_its.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
it("works with old functions", function() {

})

it("works with new functions", () => {

})
11 changes: 11 additions & 0 deletions test/fixtures/nested_its.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe("some context", () => {
it("1", function() {
})
it("2", function() {
})
})

describe("some other context", function() {
it("3", () => {
})
})
55 changes: 55 additions & 0 deletions test/test_file_parser.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
});