-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛 Fixes #700 replace back slashes in fie paths with forward slashes #707
Changes from all commits
ecc1ca9
7c5778c
9d1bf82
ffba179
905c713
acc2109
d470523
d392e8b
92f775f
32a6e53
4b30f2c
e396752
eff4792
4553c28
3c6520a
966e516
63d2d65
f6d469e
029e055
e8c71c0
51cf9d2
7aadc43
f0f5c59
b2b9da9
30a4091
b16d2f9
c8db345
0df7f16
3ccc881
bb0709e
2c19004
8f224ab
41b7080
dab38dc
ae22dd4
d2340d2
52bb7ae
b6b2531
8d8d2fc
c425a55
3963217
a696f2a
a31e659
2663cd5
7c85e0b
beb82c2
01e722a
d84da8e
78da3e1
685b683
43364fd
8701636
5c8addf
588c2f9
66b1382
e6b4b48
83ad842
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,11 @@ interface String { | |
* E.g. if an argument contains a space, then it will be enclosed within double quotes. | ||
*/ | ||
toCommandArgument(): string; | ||
/** | ||
* Appropriately formats a a file path so it can be used as an argument for a command in a shell. | ||
* E.g. if an argument contains a space, then it will be enclosed within double quotes. | ||
*/ | ||
fileToCommandArgument(): string; | ||
} | ||
|
||
/** | ||
|
@@ -47,5 +52,16 @@ String.prototype.toCommandArgument = function (this: string): string { | |
if (!this) { | ||
return this; | ||
} | ||
return (this.indexOf(' ') > 0 && !this.startsWith('"') && !this.endsWith('"')) ? `"${this}"` : this.toString(); | ||
return (this.indexOf(' ') >= 0 && !this.startsWith('"') && !this.endsWith('"')) ? `"${this}"` : this.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it is single-quoted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use single quotes in the code. its only double quotes used. So this should be ok. The check is there to ensure this doesn't fail if we call this twice. |
||
}; | ||
|
||
/** | ||
* Appropriately formats a a file path so it can be used as an argument for a command in a shell. | ||
* E.g. if an argument contains a space, then it will be enclosed within double quotes. | ||
*/ | ||
String.prototype.fileToCommandArgument = function (this: string): string { | ||
if (!this) { | ||
return this; | ||
} | ||
return this.toCommandArgument().replace(/\\/g, '/'); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { expect } from 'chai'; | ||
import '../../client/common/extensions'; | ||
|
||
// Defines a Mocha test suite to group tests of similar kind together | ||
suite('String Extensions', () => { | ||
test('Should return empty string for empty arg', () => { | ||
const argTotest = ''; | ||
expect(argTotest.toCommandArgument()).to.be.equal(''); | ||
}); | ||
test('Should quote an empty space', () => { | ||
const argTotest = ' '; | ||
expect(argTotest.toCommandArgument()).to.be.equal('" "'); | ||
}); | ||
test('Should not quote command arguments without spaces', () => { | ||
const argTotest = 'one.two.three'; | ||
expect(argTotest.toCommandArgument()).to.be.equal(argTotest); | ||
}); | ||
test('Should quote command arguments with spaces', () => { | ||
const argTotest = 'one two three'; | ||
expect(argTotest.toCommandArgument()).to.be.equal(`"${argTotest}"`); | ||
}); | ||
test('Should return empty string for empty path', () => { | ||
const fileToTest = ''; | ||
expect(fileToTest.fileToCommandArgument()).to.be.equal(''); | ||
}); | ||
test('Should not quote file argument without spaces', () => { | ||
const fileToTest = 'users/test/one'; | ||
expect(fileToTest.fileToCommandArgument()).to.be.equal(fileToTest); | ||
}); | ||
test('Should quote file argument with spaces', () => { | ||
const fileToTest = 'one two three'; | ||
expect(fileToTest.fileToCommandArgument()).to.be.equal(`"${fileToTest}"`); | ||
}); | ||
test('Should replace all back slashes with forward slashes (irrespective of OS)', () => { | ||
const fileToTest = 'c:\\users\\user\\conda\\scripts\\python.exe'; | ||
expect(fileToTest.fileToCommandArgument()).to.be.equal(fileToTest.replace(/\\/g, '/')); | ||
}); | ||
test('Should replace all back slashes with forward slashes (irrespective of OS) and quoted when file has spaces', () => { | ||
const fileToTest = 'c:\\users\\user namne\\conda path\\scripts\\python.exe'; | ||
expect(fileToTest.fileToCommandArgument()).to.be.equal(`"${fileToTest.replace(/\\/g, '/')}"`); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it need to be
this.toCommandArgument(this.ToString())
or we don't care about spaces after thetoString()
conversion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't care about spaces. The change was added to ensure unit test passed, in case we have an empty string with spaces " " (I don't see how that would ever be a valid argument, but I'm supporting that just in case there are some tools that do require it).