Skip to content

Commit

Permalink
fix(windows): handle $0 better on Windows platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
elas7 authored and bcoe committed Apr 5, 2016
1 parent a2b5a2a commit eb6e03f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
40 changes: 40 additions & 0 deletions test/usage.js
Expand Up @@ -2165,6 +2165,46 @@ describe('usage tests', function () {
yargs([]).$0.should.equal('/code/iojs/script.js')
})
})

it('is detected correctly when argv contains "node.exe"', function () {
mockProcessArgv(['node.exe', 'script.js'], function () {
yargs([]).$0.should.equal('script.js')
})
})

it('is detected correctly when argv contains "iojs.exe"', function () {
mockProcessArgv(['iojs.exe', 'script.js'], function () {
yargs([]).$0.should.equal('script.js')
})
})

if (process.platform !== 'win32') {
it('is resolved to the relative path if it is shorter', function () {
mockProcessArgv(['node', '/code/node/script.js'], function () {
yargs([], '/code/python/').$0.should.equal('../node/script.js')
})
})

it('is not resolved to the relative path if it is larger', function () {
mockProcessArgv(['node', '/script.js'], function () {
yargs([], '/very/long/current/directory/').$0.should.equal('/script.js')
})
})
}

if (process.platform === 'win32') {
it('is resolved to the relative path if it is shorter, using Windows paths', function () {
mockProcessArgv(['node.exe', 'C:\\code\\node\\script.js'], function () {
yargs([], 'C:\\code\\python\\').$0.should.equal('..\\node\\script.js')
})
})

it('is not resolved to the relative path if it is larger, using Windows paths', function () {
mockProcessArgv(['node', 'C:\\script.js'], function () {
yargs([], 'C:\\very\\long\\current\\directory\\').$0.should.equal('C:\\script.js')
})
})
}
})

describe('choices', function () {
Expand Down
4 changes: 2 additions & 2 deletions yargs.js
Expand Up @@ -35,9 +35,9 @@ function Yargs (processArgs, cwd, parentRequire) {
.map(function (x, i) {
// ignore the node bin, specify this in your
// bin file with #!/usr/bin/env node
if (i === 0 && /\b(node|iojs)$/.test(x)) return
if (i === 0 && /\b(node|iojs)(\.exe)?$/.test(x)) return
var b = rebase(cwd, x)
return x.match(/^\//) && b.length < x.length ? b : x
return x.match(/^(\/|([a-zA-Z]:)?\\)/) && b.length < x.length ? b : x
})
.join(' ').trim()

Expand Down

0 comments on commit eb6e03f

Please sign in to comment.