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

Enhance loop condition in URI.allLocations to prevent endless loops #6378

Merged
merged 1 commit into from
Oct 16, 2019
Merged
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
9 changes: 9 additions & 0 deletions packages/core/src/common/path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/');
assert.deepEqual(path.dir.toString(), '/foo/bar');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, 'file.txt');
assert.deepEqual(path.name, 'file');
assert.deepEqual(path.ext, '.txt');
Expand All @@ -36,6 +37,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, false);
assert.deepEqual(path.root, undefined);
assert.deepEqual(path.dir.toString(), 'foo/bar');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, 'file.txt');
assert.deepEqual(path.name, 'file');
assert.deepEqual(path.ext, '.txt');
Expand All @@ -47,6 +49,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/');
assert.deepEqual(path.dir.toString(), '/');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, 'foo');
assert.deepEqual(path.name, 'foo');
assert.deepEqual(path.ext, '');
Expand All @@ -58,6 +61,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, false);
assert.deepEqual(path.root, undefined);
assert.deepEqual(path.dir.toString(), 'foo');
assert.deepEqual(path.hasDir, false);
assert.deepEqual(path.base, 'foo');
assert.deepEqual(path.name, 'foo');
assert.deepEqual(path.ext, '');
Expand All @@ -69,6 +73,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/');
assert.deepEqual(path.dir.toString(), '/');
assert.deepEqual(path.hasDir, false);
assert.deepEqual(path.base, '');
assert.deepEqual(path.name, '');
assert.deepEqual(path.ext, '');
Expand All @@ -80,6 +85,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/c:');
assert.deepEqual(path.dir.toString(), '/c:/foo/bar');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, 'file.txt');
assert.deepEqual(path.name, 'file');
assert.deepEqual(path.ext, '.txt');
Expand All @@ -91,6 +97,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/c:');
assert.deepEqual(path.dir.toString(), '/c:');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, 'foo');
assert.deepEqual(path.name, 'foo');
assert.deepEqual(path.ext, '');
Expand All @@ -102,6 +109,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/c:');
assert.deepEqual(path.dir.toString(), '/c:');
assert.deepEqual(path.hasDir, true);
assert.deepEqual(path.base, '');
assert.deepEqual(path.name, '');
assert.deepEqual(path.ext, '');
Expand All @@ -113,6 +121,7 @@ describe('Path', () => {
assert.deepEqual(path.isAbsolute, true);
assert.deepEqual(path.root!.toString(), '/c:');
assert.deepEqual(path.dir.toString(), '/c:');
assert.deepEqual(path.hasDir, false);
assert.deepEqual(path.base, 'c:');
assert.deepEqual(path.name, 'c:');
assert.deepEqual(path.ext, '');
Expand Down
18 changes: 14 additions & 4 deletions packages/core/src/common/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,31 @@ export class Path {
return new Path(this.raw.substr(0, index)).root;
}

/**
* Returns the parent directory if it exists (`hasDir === true`) or `this` otherwise.
*/
get dir(): Path {
if (this._dir === undefined) {
this._dir = this.computeDir();
}
return this._dir;
}

/**
* Returns `true` if this has a parent directory, `false` otherwise.
*
* _This implementation returns `true` if and only if this is not the root dir and
* there is a path separator in the raw path._
*/
get hasDir(): boolean {
return !this.isRoot && this.raw.lastIndexOf(Path.separator) !== -1;
}

protected computeDir(): Path {
if (this.isRoot) {
if (!this.hasDir) {
return this;
}
const lastIndex = this.raw.lastIndexOf(Path.separator);
if (lastIndex === -1) {
return this;
}
if (this.isAbsolute) {
const firstIndex = this.raw.indexOf(Path.separator);
if (firstIndex === lastIndex) {
Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/common/uri.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,42 @@ const expect = chai.expect;

describe('uri', () => {

describe('#getAllLocations', () => {

it('of /foo/bar/file.txt', () => {
expect(new URI('/foo/bar/file.txt').allLocations.map(x => x.toString()))
.deep.equals([
new URI('/foo/bar/file.txt').toString(),
new URI('/foo/bar').toString(),
new URI('/foo').toString(),
new URI('/').toString()
]);
});

it('of foo', () => {
expect(new URI('foo').allLocations.map(x => x.toString()))
.deep.equals([
new URI('foo').toString(),
new URI('/').toString()
]);
});

it('of foo:bar.txt', () => {
expect(new URI().withScheme('foo').withPath('bar.txt').allLocations.map(x => x.toString()))
.deep.equals([
'foo:bar.txt'
]);
});

it('of foo:bar/foobar.txt', () => {
expect(new URI().withScheme('foo').withPath('bar/foobar.txt').allLocations.map(x => x.toString()))
.deep.equals([
new URI().withScheme('foo').withPath('bar/foobar.txt').toString(),
new URI().withScheme('foo').withPath('bar').toString()
]);
});
});

describe('#getParent', () => {

it('of file:///foo/bar.txt', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/common/uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export default class URI {
get allLocations(): URI[] {
const locations = [];
let location: URI = this;
while (!location.path.isRoot) {
while (!location.path.isRoot && location.path.hasDir) {
Copy link
Member

@akosyakov akosyakov Oct 15, 2019

Choose a reason for hiding this comment

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

I think location.path.hasDir should not be necessary.

Since we cannot return undefined for path.dir, can we return an empty path instead? It would be aligned when with Node.js behaviour as it should be, i.e.

const path = require('path');
console.log(path.parse('foo'));

prints:

gitpod /workspace/theia $ node test.js 
{ root: '', dir: '', base: 'foo', ext: '', name: 'foo' }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean that Path#dir should return an empty string instead of this when there is no dir? If you mean that, then I would totally agree with you. In my opinion, that would be much more intuitive. I just wonder what the reason for returning this was in the first place … and if changing this would break something elsewhere.

Copy link
Member

Choose a reason for hiding this comment

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

I meant an empty path, i.e. something like new Path('').

My concern is that a loop like here should always terminate for any URI just by checking path.isRoot.

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the confusion, just spotted that i typed remove instead of return in the original comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I meant empty path instead of empty string. :-D

I totally agree with you that an empty path instead of this makes more sense as return value. The same goes in my opinion for URI#parent, by the way. But this does not terminates the loop above. A path that is not absolute does not have a root at all and therefore asking for isRoot is not right here. Or am I wrong?

But the loop condition above can be abbreviated by just checking for hasDir because root never has a dir.

Copy link
Member

Choose a reason for hiding this comment

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

yes, I think you are right, let's go with this solution it does not seem to be breaking

locations.push(location);
location = location.parent;
}
Expand Down