Skip to content

Commit

Permalink
fix(pwt): compatibility in CWD with wrong casing
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Aug 18, 2022
1 parent d7ba592 commit bb062f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
17 changes: 17 additions & 0 deletions packages/playwright-test/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export class Loader {
}

private async _requireOrImport(file: string) {
file = fixFilepathCapitalization(file);
const revertBabelRequire = installTransform();
const isModule = fileIsModule(file);
try {
Expand Down Expand Up @@ -678,3 +679,19 @@ export function folderIsModule(folder: string): boolean {
// Rely on `require` internal caching logic.
return require(packageJsonPath).type === 'module';
}

function fixFilepathCapitalization(file: string): string {
/**
* On Windows with PowerShell <= 6 it is possible to have a CWD with different
* casing than what the actual directory on the filesystem is. This can causes
* that we require the file multiple times with different casing. To mitigate
* this we get the actual underlying filesystem path and use that.
* https://github.com/microsoft/playwright/issues/9193#issuecomment-1219362150
*/
const realFile = fs.realpathSync.native(file);
// We do not want to resolve them (e.g. 8.3 filenames), so we do a primitive
// approach by only using it if the actual lowercase characters are the same:
if (realFile.toLowerCase() === file.toLowerCase())
return realFile;
return file;
}
4 changes: 2 additions & 2 deletions tests/installation/npmTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import debugLogger from 'debug';
import { Registry } from './registry';
import { spawnAsync } from './spawnAsync';


export const TMP_WORKSPACES = path.join(os.platform() === 'darwin' ? '/tmp' : os.tmpdir(), 'pwt', 'workspaces');
// os.tmpdir() on Windows returns a 8.3 filename, so we resolve it.
export const TMP_WORKSPACES = path.join(os.platform() === 'darwin' ? '/tmp' : fs.realpathSync.native(os.tmpdir()), 'pwt', 'workspaces');

const debug = debugLogger('itest');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs';
import path from 'path';
import { test, expect } from './npmTest';

test('@playwright/test should handle incorrect cwd casing', async ({ exec, tmpWorkspace }) => {
const cwd = path.join(tmpWorkspace, 'expectedcasing');
fs.mkdirSync(cwd);
fs.writeFileSync(path.join(cwd, 'sample.spec.ts'), `
import { test, expect } from '@playwright/test';
test('should pass', async () => {
expect(1 + 1).toBe(2);
})
`);
fs.writeFileSync(path.join(cwd, 'sample.spec.js'), `
const { test, expect } = require('@playwright/test');
test('should pass', async () => {
expect(1 + 1).toBe(2);
})
`);
await exec('npm init -y', { cwd });
await exec('npm i --foreground-scripts @playwright/test', { cwd });

const output = await exec('npx playwright test --reporter=list', { cwd: path.join(tmpWorkspace, 'eXpEcTeDcAsInG') });
expect(output).toContain('2 passed');
});
2 changes: 1 addition & 1 deletion tests/installation/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const config: PlaywrightTestConfig = {
timeout: 5 * 60 * 1000,
retries: 0,
reporter: process.env.CI ? [
['dot'],
['list'],
['json', { outputFile: path.join(outputDir, 'report.json') }],
] : [['list'], ['html', { open: 'on-failure' }]],
forbidOnly: !!process.env.CI,
Expand Down

0 comments on commit bb062f7

Please sign in to comment.