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

Cut back on more 'caught exceptions' from vfs #24076

Merged
merged 1 commit into from
May 14, 2018
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
4 changes: 2 additions & 2 deletions src/harness/fakes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ namespace fakes {

private _getStats(path: string) {
try {
return this.vfs.statSync(path);
return this.vfs.existsSync(path) ? this.vfs.statSync(path) : undefined;
}
catch {
return undefined;
Expand Down Expand Up @@ -332,7 +332,7 @@ namespace fakes {
let fs = this.vfs;
while (fs.shadowRoot) {
try {
const shadowRootStats = fs.shadowRoot.statSync(canonicalFileName);
const shadowRootStats = fs.shadowRoot.existsSync(canonicalFileName) && fs.shadowRoot.statSync(canonicalFileName);
if (shadowRootStats.dev !== stats.dev ||
shadowRootStats.ino !== stats.ino ||
shadowRootStats.mtimeMs !== stats.mtimeMs) {
Expand Down
12 changes: 10 additions & 2 deletions src/harness/vfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ namespace vfs {

// POSIX API (aligns with NodeJS "fs" module API)

/**
* Determines whether a path exists.
*/
public existsSync(path: string) {
const result = this._walk(this._resolve(path), /*noFollow*/ true, () => "stop");
return result !== undefined && result.node !== undefined;
}

/**
* Get file status. If `path` is a symbolic link, it is dereferenced.
*
Expand Down Expand Up @@ -861,8 +869,8 @@ namespace vfs {
*/
private _resolve(path: string) {
return this._cwd
? vpath.resolve(this._cwd, vpath.validate(path, vpath.ValidationFlags.RelativeOrAbsolute))
: vpath.validate(path, vpath.ValidationFlags.Absolute);
? vpath.resolve(this._cwd, vpath.validate(path, vpath.ValidationFlags.RelativeOrAbsolute | vpath.ValidationFlags.AllowWildcard))
: vpath.validate(path, vpath.ValidationFlags.Absolute | vpath.ValidationFlags.AllowWildcard);
}

private _applyFiles(files: FileSet, dirname: string) {
Expand Down
7 changes: 6 additions & 1 deletion src/harness/vpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ namespace vpath {

const invalidRootComponentRegExp = /^(?!(\/|\/\/\w+\/|[a-zA-Z]:\/?|)$)/;
const invalidNavigableComponentRegExp = /[:*?"<>|]/;
const invalidNavigableComponentWithWildcardsRegExp = /[:"<>|]/;
const invalidNonNavigableComponentRegExp = /^\.{1,2}$|[:*?"<>|]/;
const invalidNonNavigableComponentWithWildcardsRegExp = /^\.{1,2}$|[:"<>|]/;
const extRegExp = /\.\w+$/;

export const enum ValidationFlags {
Expand All @@ -44,6 +46,7 @@ namespace vpath {
AllowExtname = 1 << 8,
AllowTrailingSeparator = 1 << 9,
AllowNavigation = 1 << 10,
AllowWildcard = 1 << 11,

/** Path must be a valid directory root */
Root = RequireRoot | AllowRoot | AllowTrailingSeparator,
Expand All @@ -63,7 +66,9 @@ namespace vpath {
const hasDirname = components.length > 2;
const hasBasename = components.length > 1;
const hasExtname = hasBasename && extRegExp.test(components[components.length - 1]);
const invalidComponentRegExp = flags & ValidationFlags.AllowNavigation ? invalidNavigableComponentRegExp : invalidNonNavigableComponentRegExp;
const invalidComponentRegExp = flags & ValidationFlags.AllowNavigation
? flags & ValidationFlags.AllowWildcard ? invalidNavigableComponentWithWildcardsRegExp : invalidNavigableComponentRegExp
: flags & ValidationFlags.AllowWildcard ? invalidNonNavigableComponentWithWildcardsRegExp : invalidNonNavigableComponentRegExp;

// Validate required components
if (flags & ValidationFlags.RequireRoot && !hasRoot) return false;
Expand Down