chore: prefer use of fs/promise to utils/promisify (#9848)

Co-authored-by: Alex Rudenko <OrKoN@users.noreply.github.com>
This commit is contained in:
Nikolay Vitkov 2023-03-14 18:30:41 +01:00 committed by GitHub
parent 3e7a514e55
commit 17c650b6d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 47 deletions

View File

@ -17,13 +17,12 @@
import {exec as execChildProcess} from 'child_process';
import {createReadStream, createWriteStream, existsSync, readdirSync} from 'fs';
import {chmod, mkdir, readdir, unlink} from 'fs/promises';
import * as http from 'http';
import * as https from 'https';
import * as os from 'os';
import * as path from 'path';
import * as URL from 'url';
import * as util from 'util';
import {promisify} from 'util';
import http from 'http';
import https from 'https';
import os from 'os';
import path from 'path';
import URL from 'url';
import {promisify, format} from 'util';
import extractZip from 'extract-zip';
import createHttpsProxyAgent, {
@ -106,7 +105,7 @@ function downloadURL(
host: string,
revision: string
): string {
const url = util.format(
const url = format(
downloadURLs[product][platform],
host,
revision,

View File

@ -14,11 +14,11 @@
* limitations under the License.
*/
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
import {promisify} from 'util';
import childProcess from 'child_process';
import fs from 'fs';
import {rename, unlink} from 'fs/promises';
import path from 'path';
import readline from 'readline';
import rimraf from 'rimraf';
@ -40,9 +40,6 @@ import {isErrnoException, isErrorLike} from '../util/ErrorLike.js';
import {LaunchOptions} from './LaunchOptions.js';
import {PipeTransport} from './PipeTransport.js';
const renameAsync = promisify(fs.rename);
const unlinkAsync = promisify(fs.unlink);
const debugLauncher = debug('puppeteer:launcher');
const PROCESS_ERROR_EXPLANATION = `Puppeteer was unable to kill the process which ran the browser binary.
@ -136,7 +133,7 @@ export class BrowserRunner {
try {
// When an existing user profile has been used remove the user
// preferences file and restore possibly backuped preferences.
await unlinkAsync(path.join(this.#userDataDir, 'user.js'));
await unlink(path.join(this.#userDataDir, 'user.js'));
const prefsBackupPath = path.join(
this.#userDataDir,
@ -144,8 +141,8 @@ export class BrowserRunner {
);
if (fs.existsSync(prefsBackupPath)) {
const prefsPath = path.join(this.#userDataDir, 'prefs.js');
await unlinkAsync(prefsPath);
await renameAsync(prefsBackupPath, prefsPath);
await unlink(prefsPath);
await rename(prefsBackupPath, prefsPath);
}
} catch (error) {
debugError(error);

View File

@ -14,10 +14,9 @@
* limitations under the License.
*/
import fs from 'fs';
import {mkdtemp} from 'fs/promises';
import os from 'os';
import path from 'path';
import {promisify} from 'util';
import expect from 'expect';
import {
@ -28,8 +27,6 @@ import rimraf from 'rimraf';
import {getTestState} from './mocha-utils.js';
const mkdtempAsync = promisify(fs.mkdtemp);
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
const extensionPath = path.join(__dirname, '../assets', 'simple-extension');
@ -215,7 +212,7 @@ describe('headful tests', function () {
/* Needs investigation into why but this fails consistently on Windows CI. */
const {server, puppeteer} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
// Write a cookie in headful chrome
const headfulBrowser = await launchBrowser(
puppeteer,

View File

@ -14,10 +14,10 @@
* limitations under the License.
*/
import fs from 'fs';
import {mkdtemp, readFile, stat, writeFile} from 'fs/promises';
import os from 'os';
import path from 'path';
import {TLSSocket} from 'tls';
import {promisify} from 'util';
import {Protocol} from 'devtools-protocol';
import expect from 'expect';
@ -32,10 +32,6 @@ import utils from './utils.js';
const rmAsync = (filename: string) => {
return rimraf(filename);
};
const mkdtempAsync = promisify(fs.mkdtemp);
const readFileAsync = promisify(fs.readFile);
const statAsync = promisify(fs.stat);
const writeFileAsync = promisify(fs.writeFile);
const TMP_FOLDER = path.join(os.tmpdir(), 'pptr_tmp_folder-');
const FIREFOX_TIMEOUT = 30 * 1000;
@ -50,7 +46,7 @@ describe('Launcher specs', function () {
it('should download and extract chrome linux binary', async () => {
const {server} = getTestState();
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
const downloadsFolder = await mkdtemp(TMP_FOLDER);
const browserFetcher = new BrowserFetcher({
platform: 'linux',
path: downloadsFolder,
@ -74,13 +70,13 @@ describe('Launcher specs', function () {
revisionInfo = (await browserFetcher.download(expectedRevision))!;
expect(revisionInfo.local).toBe(true);
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
expect(await readFile(revisionInfo.executablePath, 'utf8')).toBe(
'LINUX BINARY\n'
);
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
expect(
(await statAsync(revisionInfo.executablePath)).mode & 0o777
).toBe(expectedPermissions);
expect((await stat(revisionInfo.executablePath)).mode & 0o777).toBe(
expectedPermissions
);
expect(await browserFetcher.localRevisions()).toEqual([
expectedRevision,
]);
@ -91,7 +87,7 @@ describe('Launcher specs', function () {
it('should download and extract firefox linux binary', async () => {
const {server} = getTestState();
const downloadsFolder = await mkdtempAsync(TMP_FOLDER);
const downloadsFolder = await mkdtemp(TMP_FOLDER);
const browserFetcher = new BrowserFetcher({
platform: 'linux',
path: downloadsFolder,
@ -119,13 +115,13 @@ describe('Launcher specs', function () {
revisionInfo = (await browserFetcher.download(expectedVersion))!;
expect(revisionInfo.local).toBe(true);
expect(await readFileAsync(revisionInfo.executablePath, 'utf8')).toBe(
expect(await readFile(revisionInfo.executablePath, 'utf8')).toBe(
'FIREFOX LINUX BINARY\n'
);
const expectedPermissions = os.platform() === 'win32' ? 0o666 : 0o755;
expect(
(await statAsync(revisionInfo.executablePath)).mode & 0o777
).toBe(expectedPermissions);
expect((await stat(revisionInfo.executablePath)).mode & 0o777).toBe(
expectedPermissions
);
expect(await browserFetcher.localRevisions()).toEqual([
expectedVersion,
]);
@ -243,7 +239,7 @@ describe('Launcher specs', function () {
it('userDataDir option', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
// Open a page to make sure its functional.
@ -288,11 +284,11 @@ describe('Launcher specs', function () {
it('userDataDir option restores preferences', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
const prefsJSPath = path.join(userDataDir, 'prefs.js');
const prefsJSContent = 'user_pref("browser.warnOnQuit", true)';
await writeFileAsync(prefsJSPath, prefsJSContent);
await writeFile(prefsJSPath, prefsJSContent);
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
@ -302,7 +298,7 @@ describe('Launcher specs', function () {
await browser.close();
expect(fs.readdirSync(userDataDir).length).toBeGreaterThan(0);
expect(await readFileAsync(prefsJSPath, 'utf8')).toBe(prefsJSContent);
expect(await readFile(prefsJSPath, 'utf8')).toBe(prefsJSContent);
// This might throw. See https://github.com/puppeteer/puppeteer/issues/2778
await rmAsync(userDataDir).catch(() => {});
@ -310,7 +306,7 @@ describe('Launcher specs', function () {
it('userDataDir argument', async () => {
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
const options = Object.assign({}, defaultBrowserOptions);
if (isChrome) {
options.args = [
@ -334,7 +330,7 @@ describe('Launcher specs', function () {
it('userDataDir argument with non-existent dir', async () => {
const {isChrome, puppeteer, defaultBrowserOptions} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
await rmAsync(userDataDir);
const options = Object.assign({}, defaultBrowserOptions);
if (isChrome) {
@ -359,7 +355,7 @@ describe('Launcher specs', function () {
it('userDataDir option should restore state', async () => {
const {server, puppeteer, defaultBrowserOptions} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
const page = await browser.newPage();
@ -384,7 +380,7 @@ describe('Launcher specs', function () {
it('userDataDir option should restore cookies', async () => {
const {server, puppeteer, defaultBrowserOptions} = getTestState();
const userDataDir = await mkdtempAsync(TMP_FOLDER);
const userDataDir = await mkdtemp(TMP_FOLDER);
const options = Object.assign({userDataDir}, defaultBrowserOptions);
const browser = await puppeteer.launch(options);
const page = await browser.newPage();