Skip to content

Commit

Permalink
fix(utils): dirname and basename should handle Windows paths (#8737)
Browse files Browse the repository at this point in the history
The Electron SDK uses `basename` to get the file name without path but
this returned the full path on Windows.

This PR adds Windows support to the regex and adds some tests.
  • Loading branch information
timfish authored Aug 8, 2023
1 parent fac2be8 commit a4ae291
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/utils/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ function normalizeArray(parts: string[], allowAboveRoot?: boolean): string[] {

// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
const splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^/]+?|)(\.[^./]*|))(?:[/]*)$/;
const splitPathRe = /^(\S+:\\|\/?)([\s\S]*?)((?:\.{1,2}|[^/\\]+?|)(\.[^./\\]*|))(?:[/\\]*)$/;
/** JSDoc */
function splitPath(filename: string): string[] {
const parts = splitPathRe.exec(filename);
// Truncate files names greater than 1024 characters to avoid regex dos
// https://github.com/getsentry/sentry-javascript/pull/8737#discussion_r1285719172
const truncated = filename.length > 1024 ? `<truncated>${filename.slice(-1024)}` : filename;
const parts = splitPathRe.exec(truncated);
return parts ? parts.slice(1) : [];
}

Expand Down
33 changes: 33 additions & 0 deletions packages/utils/test/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { basename, dirname } from '../src/path';

describe('path', () => {
describe('basename', () => {
test('unix', () => {
expect(basename('/foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('foo/bar/baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('../baz/asdf/quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
test('windows', () => {
expect(basename('c:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('..\\bar\\baz\\asdf\\quux.html')).toEqual('quux.html');
expect(basename('quux.html')).toEqual('quux.html');
});
});

describe('dirname', () => {
test('unix', () => {
expect(dirname('/foo/bar/baz/asdf/quux.html')).toEqual('/foo/bar/baz/asdf');
expect(dirname('foo/bar/baz/asdf/quux.html')).toEqual('foo/bar/baz/asdf');
expect(dirname('../baz/asdf/quux.html')).toEqual('../baz/asdf');
expect(dirname('/quux.html')).toEqual('/');
});
test('windows', () => {
expect(dirname('C:\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('C:\\foo\\bar\\baz\\asdf');
expect(dirname('\\foo\\bar\\baz\\asdf\\quux.html')).toEqual('\\foo\\bar\\baz\\asdf');
expect(dirname('..\\bar\\baz\\asdf\\quux.html')).toEqual('..\\bar\\baz\\asdf');
expect(dirname('quux.html')).toEqual('.');
});
});
});

0 comments on commit a4ae291

Please sign in to comment.