Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
fix: Workspace path match (#33)
Browse files Browse the repository at this point in the history
* fix: Project path compare

* chore: Create @garment/utils package, add isSubPath win32 test

* chore: Change path import
  • Loading branch information
Aleksei Dmitriev authored Jan 6, 2021
1 parent a16a853 commit a1a5c06
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 7 deletions.
7 changes: 4 additions & 3 deletions core/garment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
"@garment/runner": "^0.14.0",
"@garment/scheduler": "^0.14.0",
"@garment/schema-validator": "^0.14.0",
"@garment/utils": "^0.0.14",
"@garment/workspace": "^0.14.0",
"@parcel/watcher": "^2.0.0-alpha.9",
"fs-extra": "8.1.0",
"globby": "10.0.1",
"is-valid-path": "^0.1.1",
"matcher": "^2.1.0",
"memfs": "^3.1.2",
"multimatch": "^4.0.0",
"normalize-path": "^3.0.0",
"tempy": "0.3.0",
"unionfs": "^4.4.0",
"globby": "10.0.1"
"unionfs": "^4.4.0"
},
"files": [
"lib",
Expand All @@ -30,4 +31,4 @@
"@types/is-valid-path": "^0.1.0",
"@types/tempy": "^0.2.0"
}
}
}
3 changes: 2 additions & 1 deletion core/garment/src/garment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { FileCache } from './FileCache';
import { getProjectsByName } from './getProjectsByName';
import globby = require('globby');
import normalizePath = require('normalize-path');
import { isSubPath } from '@garment/utils';

export type Cache =
| {
Expand Down Expand Up @@ -954,7 +955,7 @@ async function garmentFromWorkspace(

if (
subscription.type === 'glob' &&
normalizePath(event.path).startsWith(subscription.input.rootDir)
isSubPath(subscription.input.rootDir, normalizePath(event.path))
) {
const matched = multimatch(
event.path,
Expand Down
10 changes: 10 additions & 0 deletions core/utils/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@garment/utils",
"version": "0.0.14",
"main": "lib/index.js",
"license": "MIT",
"dependencies": {},
"files": [
"lib"
]
}
1 change: 1 addition & 0 deletions core/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './isSubPath';
50 changes: 50 additions & 0 deletions core/utils/src/isSubPath/__tests__/isSubPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { isSubPath } from '..';
import * as Path from 'path';

describe('utils | isSubPath', () => {
it('should return true if path is a subpath', () => {
expect(isSubPath('/root', '/root/child')).toBeTruthy();
});

it('should return false if path is not a subpath', () => {
expect(isSubPath('/root/foo', '/root/bar/baz')).toBeFalsy();
});

it('should return false if path has a similar substring', () => {
expect(isSubPath('/root/foo', '/root/foo bar/baz')).toBeFalsy();
});

it("should return true if path has special path .. symbols and it's a subpath", () => {
expect(isSubPath('/root/foo', '/root/../root/foo/bar')).toBeTruthy();
});

it("should return true if path has special path . symbol and it's a subpath", () => {
expect(isSubPath('/root/foo', '/root/./foo/bar')).toBeTruthy();
});
});

describe('utils | isSubPath [win32]', () => {
let relativeSpy: jest.SpyInstance;
let isAbsoluteSpy: jest.SpyInstance;

beforeEach(() => {
relativeSpy = jest
.spyOn(Path, 'relative')
.mockImplementationOnce(Path.win32.relative);
isAbsoluteSpy = jest
.spyOn(Path, 'isAbsolute')
.mockImplementationOnce(Path.win32.isAbsolute);
});

afterEach(() => {
relativeSpy.mockRestore();
isAbsoluteSpy.mockRestore();
});

it('should return true if path is a subpath', () => {
expect(isSubPath('C:\\Foo', 'C:\\Foo\\Bar')).toBeTruthy();
});
it('should return false if path is not a subpath', () => {
expect(isSubPath('C:\\Foo\\Bar', 'D:\\Foo\\Bar')).toBeFalsy();
});
});
7 changes: 7 additions & 0 deletions core/utils/src/isSubPath/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as Path from 'path';

export const isSubPath = (parent: string, child: string) => {
const relative = Path.relative(parent, child);

return relative && !relative.startsWith('..') && !Path.isAbsolute(relative);
};
3 changes: 2 additions & 1 deletion core/workspace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
],
"dependencies": {
"@garment/schema-validator": "^0.14.0",
"@garment/utils": "^0.0.14",
"dependency-graph": "^0.8.0",
"fs-extra": "8.1.0",
"mustache": "^3.0.1",
Expand All @@ -21,4 +22,4 @@
"@types/normalize-path": "^3.0.0",
"@types/object-hash": "^1.3.0"
}
}
}
5 changes: 3 additions & 2 deletions core/workspace/src/ProjectRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Project } from './Project';

import normalizePath = require('normalize-path');
import { isSubPath } from '@garment/utils';

export class ProjectRegistry {
[Symbol.iterator]() {
Expand Down Expand Up @@ -32,13 +33,13 @@ export class ProjectRegistry {
getByPaths(...paths: string[]) {
paths = paths.map(path => normalizePath(path));
return this.list().filter(project =>
paths.some(path => path.indexOf(project.fullPath) === 0)
paths.some(path => isSubPath(project.fullPath, path))
);
}

getByPath(path: string) {
path = normalizePath(path);
return this.list().find(project => path.indexOf(project.fullPath) === 0);
return this.list().find(project => isSubPath(project.fullPath, path));
}

getByPathExact(path: string) {
Expand Down
4 changes: 4 additions & 0 deletions garment.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@
"visualize-graph": {
"path": "utils/visualize-graph",
"extends": ["tspackage"]
},
"utils": {
"path": "core/utils",
"extends": ["tspackage"]
}
},
"schematics": ["@garment/schematics", "@garment/schematics-typescript"],
Expand Down

0 comments on commit a1a5c06

Please sign in to comment.