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

os: refine tmpdir() trailing slash stripping #1673

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ exports.platform = function() {
return process.platform;
};

const trailingSlashRe = isWindows ? /[^:]\\$/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like something that should live in path.js, it'd be nice if we could confine a lot of this platform path logic to one place and export it to wherever it's needed

: /.\/$/;

exports.tmpdir = function() {
var path;
if (isWindows) {
Expand All @@ -34,7 +37,7 @@ exports.tmpdir = function() {
process.env.TEMP ||
'/tmp';
}
if (/[\\\/]$/.test(path))
if (trailingSlashRe.test(path))
path = path.slice(0, -1);
return path;
};
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-os.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), expected);
process.env.TEMP = '\\temp\\';
assert.equal(os.tmpdir(), '\\temp');
process.env.TEMP = '\\tmpdir/';
assert.equal(os.tmpdir(), '\\tmpdir/');
process.env.TEMP = '\\';
assert.equal(os.tmpdir(), '\\');
process.env.TEMP = 'C:\\';
assert.equal(os.tmpdir(), 'C:\\');
} else {
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '';
Expand All @@ -25,6 +31,10 @@ if (process.platform === 'win32') {
assert.equal(os.tmpdir(), '/tmp');
process.env.TMPDIR = '/tmpdir/';
assert.equal(os.tmpdir(), '/tmpdir');
process.env.TMPDIR = '/tmpdir\\';
assert.equal(os.tmpdir(), '/tmpdir\\');
process.env.TMPDIR = '/';
assert.equal(os.tmpdir(), '/');
}

var endianness = os.endianness();
Expand Down