Skip to content

Commit

Permalink
fix: take realpath of cwd, whether or not set in env (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
motiz88 authored and bcoe committed Sep 8, 2016
1 parent 0bd16cf commit 6274d83
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 11 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"find-up": "^1.1.2",
"istanbul-lib-instrument": "^1.1.1",
"lodash.assign": "^4.2.0",
"test-exclude": "^2.1.1"
},
"devDependencies": {
Expand All @@ -21,7 +22,9 @@
"coveralls": "^2.11.9",
"cross-env": "^2.0.0",
"mocha": "^3.0.2",
"mock-fs": "^3.11.0",
"nyc": "^8.1.0",
"pmock": "^0.2.3",
"standard": "^8.0.0",
"standard-version": "^2.3.1"
},
Expand Down
28 changes: 18 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {realpathSync} from 'fs'
import {dirname} from 'path'
import {programVisitor} from 'istanbul-lib-instrument'
import assign from 'lodash.assign'

const testExclude = require('test-exclude')
const findUp = require('find-up')

Expand All @@ -12,20 +14,26 @@ function getRealpath (n) {
}
}

let exclude
function shouldSkip (file, opts) {
if (!exclude) {
exclude = testExclude(Object.keys(opts).length > 0 ? opts : {
cwd: process.env.NYC_CWD || getRealpath(process.cwd()),
configKey: 'nyc',
configPath: dirname(findUp.sync('package.json'))
})
}
function makeShouldSkip () {
let exclude
return function shouldSkip (file, opts) {
if (!exclude) {
const cwd = getRealpath(process.env.NYC_CWD || process.cwd())
exclude = testExclude(assign(
{ cwd },
Object.keys(opts).length > 0 ? opts : {
configKey: 'nyc',
configPath: dirname(findUp.sync('package.json', { cwd }))
}
))
}

return !exclude.shouldInstrument(file)
return !exclude.shouldInstrument(file)
}
}

function makeVisitor ({types: t}) {
const shouldSkip = makeShouldSkip()
return {
visitor: {
Program: {
Expand Down
79 changes: 78 additions & 1 deletion test/babel-plugin-istanbul.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* global context, describe, it */
/* eslint-env mocha */

const babel = require('babel-core')
import mockFs from 'mock-fs'
import mockProcess from 'pmock'
import fs from 'fs'
import makeVisitor from '../src'

require('chai').should()
Expand Down Expand Up @@ -49,4 +52,78 @@ describe('babel-plugin-istanbul', function () {
result.code.should.not.match(/statementMap/)
})
})

// Regression tests for https://github.com/istanbuljs/babel-plugin-istanbul/issues/7
context('when current path uses a symlink', function () {
beforeEach(function () {
mockFs({
'/project': {
'fixtures': {
'should-cover.js': fs.readFileSync('./fixtures/should-cover.js')
},
'package.json': fs.readFileSync('./package.json')
},
'/symlink': mockFs.symlink({path: '/project'})
})
})

const shouldInstrument = file => {
var result = babel.transformFileSync(file, {
plugins: [
makeVisitor({types: babel.types})
]
})
result.code.should.match(/statementMap/)
}

context('and NYC_CWD is set', function () {
var env

beforeEach(function () {
env = mockProcess.env({
NYC_CWD: '/symlink'
})
})

it('should instrument file accessed via link', function () {
shouldInstrument('/symlink/fixtures/should-cover.js')
})

it('should instrument file accessed via real path', function () {
shouldInstrument('/project/fixtures/should-cover.js')
})

afterEach(function () {
env.reset()
})
})

context('and only process.cwd() is set', function () {
var env, cwd

beforeEach(function () {
env = mockProcess.env({
NYC_CWD: ''
})
cwd = mockProcess.cwd(fs.realpathSync('/symlink')) // realpath because of mock-fs Windows quirk re: process.cwd
})

it('should instrument file accessed via link', function () {
shouldInstrument('/symlink/fixtures/should-cover.js')
})

it('should instrument file accessed via real path', function () {
shouldInstrument('/project/fixtures/should-cover.js')
})

afterEach(function () {
env.reset()
cwd.reset()
})
})

afterEach(function () {
mockFs.restore()
})
})
})
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require mock-fs

0 comments on commit 6274d83

Please sign in to comment.