Skip to content
This repository has been archived by the owner on Jan 12, 2021. It is now read-only.

Commit

Permalink
Enable stripProjectRoot by default (& tests)
Browse files Browse the repository at this point in the history
- Added Windows path support on macOS/Linux
- Wrote some tests for stripping the project root
  • Loading branch information
jmshal committed Jul 18, 2017
1 parent 40b6661 commit 77fd4d6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
41 changes: 35 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DEFAULT_OPTIONS = {
endpoint: 'https://upload.bugsnag.com',
uploadSources: false,
projectRoot: '.',
stripProjectRoot: false,
stripProjectRoot: true,
addWildcardPrefix: false,
};

Expand Down Expand Up @@ -57,15 +57,26 @@ function validateOptions(options) {
}

/**
* Returns a relative path from a project root.
* Strips the project root from the file path.
*
* @param {string} projectRoot The project root path
* @param {string} filePath The file path
* @returns {string}
*/
function getRelativePath(projectRoot, filePath) {
if (typeof filePath === 'string' && path.isAbsolute(filePath)) {
return path.relative(projectRoot, filePath);
function stripProjectRoot(projectRoot, filePath) {
if (typeof filePath === 'string') {
// Check whether the path is an posix absolute file, otherwise check whether it's a
// win32 valid absolute file. The order is important here because win32 treats posix
// absolute paths as absolute, but posix doesn't do the same for win32.
const p = path.posix.isAbsolute(filePath) ? path.posix
: path.win32.isAbsolute(filePath) ? path.win32
: null;
if (p) {
const relative = p.relative(projectRoot, filePath);
if (relative.indexOf('.') !== 0) {
return relative;
}
}
}
return filePath;
}
Expand Down Expand Up @@ -153,7 +164,7 @@ function transformSourcesMap(options) {
readFileJSON(options.sourceMap)
.then(sourceMap => (
mapSources(sourceMap, path => {
const relativePath = getRelativePath(options.projectRoot, path);
const relativePath = stripProjectRoot(options.projectRoot, path);
return doesFileExist(path).then(exists => {
if (exists && options.uploadSources) {
options.sources[relativePath] = path;
Expand Down Expand Up @@ -356,3 +367,21 @@ function upload(options, callback) {
}

exports.upload = upload;

if (process.env.NODE_ENV === 'test') {
Object.assign(exports, {
applyDefaults,
validateOptions,
stripProjectRoot,
readFileJSON,
doesFileExist,
mapSources,
transformSourcesMap,
transformOptions,
createReadStream,
prepareRequest,
sendRequest,
cleanupTempFiles,
upload,
});
}
52 changes: 52 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,59 @@
const {
stripProjectRoot,
upload,
} = require('./index');

test('upload function exists', () => {
expect(typeof upload).toBe('function');
});

describe('stripProjectRoot', () => {
test('strips project root', () => {
expect(stripProjectRoot(
'/Users/test/git/my-app/',
'/Users/test/git/my-app/src/index.js'
)).toBe('src/index.js');
});

test('strips project root with no trailing slash', () => {
expect(stripProjectRoot(
'/Users/test/git/my-app',
'/Users/test/git/my-app/src/index.js'
)).toBe('src/index.js');
});

test(`wont strip project root when path is not within project root`, () => {
expect(stripProjectRoot(
'/Users/test/git/another-app',
'/Users/test/git/my-app/src/index.js'
)).toBe('/Users/test/git/my-app/src/index.js');
});

test('strips project root from node_moodules', () => {
expect(stripProjectRoot(
'/Users/test/git/my-app',
'/Users/test/git/my-app/node_modules/some-module/lib/something.js'
)).toBe('node_modules/some-module/lib/something.js');
});

test(`won't strip project root when path is absolute`, () => {
expect(stripProjectRoot(
'/Users/test/git/my-app',
'src/index.js'
)).toBe('src/index.js');
});

test('strips project root for Windows paths', () => {
expect(stripProjectRoot(
'C:/Users/test/git/my-app',
'C:/Users/test/git/my-app/src/index.js'
)).toBe('src\\index.js');
});

test('strips project root for Windows paths', () => {
expect(stripProjectRoot(
'C:\\Users\\test\\git\\my-app',
'C:\\Users\\test\\git\\my-app\\src\\index.js'
)).toBe('src\\index.js');
});
});

0 comments on commit 77fd4d6

Please sign in to comment.