test(ivy): normalize rooted paths to include a drive letter in Windows (#31996)

The Angular compiler has an emulation system for various kinds of
filesystems and runs its testcases for all those filesystems. This
allows to verify that the compiler behaves correctly in all of the
supported platforms, without needing to run the tests on the actual
platforms.

Previously, the emulated Windows mode would normalize rooted paths to
always include a drive letter, whereas the native mode did not perform
this normalization. The consequence of this discrepancy was that running
the tests in native Windows was behaving differently compared to how
emulated Windows mode behaves, potentially resulting in test failures
in native Windows that would succeed for emulated Windows.

This commit adds logic to ensure that paths are normalized equally for
emulated Windows and native Windows mode, therefore resolving the
discrepancy.

PR Close #31996
This commit is contained in:
JoostK 2019-08-11 15:46:57 +02:00 committed by Miško Hevery
parent 375897801b
commit 4161d19374
1 changed files with 13 additions and 0 deletions

View File

@ -5,11 +5,15 @@
* Use of this source code is governed by an MIT-style license that can be * Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
/// <reference types="node" />
import * as os from 'os';
import {NodeJSFileSystem} from '../../src/node_js_file_system'; import {NodeJSFileSystem} from '../../src/node_js_file_system';
import {AbsoluteFsPath, PathSegment, PathString} from '../../src/types'; import {AbsoluteFsPath, PathSegment, PathString} from '../../src/types';
import {MockFileSystem} from './mock_file_system'; import {MockFileSystem} from './mock_file_system';
const isWindows = os.platform() === 'win32';
export class MockFileSystemNative extends MockFileSystem { export class MockFileSystemNative extends MockFileSystem {
constructor(cwd: AbsoluteFsPath = '/' as AbsoluteFsPath) { super(undefined, cwd); } constructor(cwd: AbsoluteFsPath = '/' as AbsoluteFsPath) { super(undefined, cwd); }
@ -41,6 +45,15 @@ export class MockFileSystemNative extends MockFileSystem {
} }
normalize<T extends PathString>(path: T): T { normalize<T extends PathString>(path: T): T {
// When running in Windows, absolute paths are normalized to always include a drive letter. This
// ensures that rooted posix paths used in tests will be normalized to real Windows paths, i.e.
// including a drive letter. Note that the same normalization is done in emulated Windows mode
// (see `MockFileSystemWindows`) so that the behavior is identical between native Windows and
// emulated Windows mode.
if (isWindows) {
path = path.replace(/^[\/\\]/i, 'C:/') as T;
}
return NodeJSFileSystem.prototype.normalize.call(this, path) as T; return NodeJSFileSystem.prototype.normalize.call(this, path) as T;
} }