From 2fcc2668a3edb33c5a29295f2b9c52e761ba1ca9 Mon Sep 17 00:00:00 2001 From: Spencer Date: Wed, 25 Dec 2019 06:27:21 -0700 Subject: [PATCH] remove use of experimental fs.promises api (#53346) * remove use of experimental fs.promises api * remove one more usage of fs.promises * switch to an alternate fs module to maintain testing strategy Co-authored-by: Elastic Machine --- src/core/server/uuid/fs.ts | 24 +++++++++++++++++++++++ src/core/server/uuid/resolve_uuid.test.ts | 19 +++++------------- src/core/server/uuid/resolve_uuid.ts | 4 +--- utilities/visual_regression.js | 6 +++--- 4 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 src/core/server/uuid/fs.ts diff --git a/src/core/server/uuid/fs.ts b/src/core/server/uuid/fs.ts new file mode 100644 index 0000000000000..f10d6370c09d1 --- /dev/null +++ b/src/core/server/uuid/fs.ts @@ -0,0 +1,24 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you 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 { promisify } from 'util'; + +export const readFile = promisify(Fs.readFile); +export const writeFile = promisify(Fs.writeFile); diff --git a/src/core/server/uuid/resolve_uuid.test.ts b/src/core/server/uuid/resolve_uuid.test.ts index 1ddd667eacdee..d1332daa02057 100644 --- a/src/core/server/uuid/resolve_uuid.test.ts +++ b/src/core/server/uuid/resolve_uuid.test.ts @@ -17,31 +17,22 @@ * under the License. */ -import { promises } from 'fs'; import { join } from 'path'; +import { readFile, writeFile } from './fs'; import { resolveInstanceUuid } from './resolve_uuid'; import { configServiceMock } from '../config/config_service.mock'; import { loggingServiceMock } from '../logging/logging_service.mock'; import { BehaviorSubject } from 'rxjs'; import { Logger } from '../logging'; -const { readFile, writeFile } = promises; - jest.mock('uuid', () => ({ v4: () => 'NEW_UUID', })); -jest.mock('fs', () => { - const actual = jest.requireActual('fs'); - return { - ...actual, - promises: { - ...actual.promises, - readFile: jest.fn(() => Promise.resolve('')), - writeFile: jest.fn(() => Promise.resolve('')), - }, - }; -}); +jest.mock('./fs', () => ({ + readFile: jest.fn(() => Promise.resolve('')), + writeFile: jest.fn(() => Promise.resolve('')), +})); const DEFAULT_FILE_UUID = 'FILE_UUID'; const DEFAULT_CONFIG_UUID = 'CONFIG_UUID'; diff --git a/src/core/server/uuid/resolve_uuid.ts b/src/core/server/uuid/resolve_uuid.ts index 17412bfa0544c..3f5bdc7387392 100644 --- a/src/core/server/uuid/resolve_uuid.ts +++ b/src/core/server/uuid/resolve_uuid.ts @@ -18,16 +18,14 @@ */ import uuid from 'uuid'; -import { promises } from 'fs'; import { join } from 'path'; import { take } from 'rxjs/operators'; +import { readFile, writeFile } from './fs'; import { IConfigService } from '../config'; import { PathConfigType, config as pathConfigDef } from '../path'; import { HttpConfigType, config as httpConfigDef } from '../http'; import { Logger } from '../logging'; -const { readFile, writeFile } = promises; - const FILE_ENCODING = 'utf8'; const FILE_NAME = 'uuid'; diff --git a/utilities/visual_regression.js b/utilities/visual_regression.js index f174dc4764aed..d95b3018bea96 100644 --- a/utilities/visual_regression.js +++ b/utilities/visual_regression.js @@ -103,8 +103,8 @@ async function compareScreenshots() { const diffImagePath = path.resolve(DIFF_SCREENSHOTS_DIR, screenshot); - const sessionImage = PNG.sync.read(await fs.promises.readFile(sessionImagePath)); - const baselineImage = PNG.sync.read(await fs.promises.readFile(baselineImagePath)); + const sessionImage = PNG.sync.read(await readFileAsync(sessionImagePath)); + const baselineImage = PNG.sync.read(await readFileAsync(baselineImagePath)); const { width, height } = sessionImage; const diff = new PNG({ width, height }); @@ -117,7 +117,7 @@ async function compareScreenshots() { { threshold: 0 } ); - await fs.promises.writeFile(diffImagePath, PNG.sync.write(diff)); + await writeFileAsync(diffImagePath, PNG.sync.write(diff)); const change = numDiffPixels / (width * height); const changePercentage = (change * 100).toFixed(2);