From 18722e70177f5caf1308b7016bb6015048431353 Mon Sep 17 00:00:00 2001 From: spalger Date: Tue, 17 Dec 2019 15:09:06 -0700 Subject: [PATCH 1/3] remove use of experimental fs.promises api --- utilities/visual_regression.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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); From 522227b10379785b75716036423da1f89a89be7f Mon Sep 17 00:00:00 2001 From: spalger Date: Sun, 22 Dec 2019 08:03:02 -0700 Subject: [PATCH 2/3] remove one more usage of fs.promises --- src/core/server/uuid/resolve_uuid.test.ts | 11 ++++------- src/core/server/uuid/resolve_uuid.ts | 6 ++++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/server/uuid/resolve_uuid.test.ts b/src/core/server/uuid/resolve_uuid.test.ts index 1ddd667eacdee..360baa4123ce0 100644 --- a/src/core/server/uuid/resolve_uuid.test.ts +++ b/src/core/server/uuid/resolve_uuid.test.ts @@ -17,7 +17,7 @@ * under the License. */ -import { promises } from 'fs'; +import Fs from 'fs'; import { join } from 'path'; import { resolveInstanceUuid } from './resolve_uuid'; import { configServiceMock } from '../config/config_service.mock'; @@ -25,7 +25,7 @@ import { loggingServiceMock } from '../logging/logging_service.mock'; import { BehaviorSubject } from 'rxjs'; import { Logger } from '../logging'; -const { readFile, writeFile } = promises; +const { readFile, writeFile } = Fs; jest.mock('uuid', () => ({ v4: () => 'NEW_UUID', @@ -35,11 +35,8 @@ jest.mock('fs', () => { const actual = jest.requireActual('fs'); return { ...actual, - promises: { - ...actual.promises, - readFile: jest.fn(() => Promise.resolve('')), - writeFile: jest.fn(() => Promise.resolve('')), - }, + readFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())), + writeFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())), }; }); diff --git a/src/core/server/uuid/resolve_uuid.ts b/src/core/server/uuid/resolve_uuid.ts index 17412bfa0544c..ebe1250a8a490 100644 --- a/src/core/server/uuid/resolve_uuid.ts +++ b/src/core/server/uuid/resolve_uuid.ts @@ -18,7 +18,8 @@ */ import uuid from 'uuid'; -import { promises } from 'fs'; +import Fs from 'fs'; +import { promisify } from 'util'; import { join } from 'path'; import { take } from 'rxjs/operators'; import { IConfigService } from '../config'; @@ -26,7 +27,8 @@ import { PathConfigType, config as pathConfigDef } from '../path'; import { HttpConfigType, config as httpConfigDef } from '../http'; import { Logger } from '../logging'; -const { readFile, writeFile } = promises; +const readFile = promisify(Fs.readFile); +const writeFile = promisify(Fs.writeFile); const FILE_ENCODING = 'utf8'; const FILE_NAME = 'uuid'; From 84801a61271abf1f8c68cc20b1652f8a275a06f8 Mon Sep 17 00:00:00 2001 From: spalger Date: Mon, 23 Dec 2019 15:16:20 -0700 Subject: [PATCH 3/3] switch to an alternate fs module to maintain testing strategy --- src/core/server/uuid/fs.ts | 24 +++++++++++++++++++++++ src/core/server/uuid/resolve_uuid.test.ts | 16 +++++---------- src/core/server/uuid/resolve_uuid.ts | 6 +----- 3 files changed, 30 insertions(+), 16 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 360baa4123ce0..d1332daa02057 100644 --- a/src/core/server/uuid/resolve_uuid.test.ts +++ b/src/core/server/uuid/resolve_uuid.test.ts @@ -17,28 +17,22 @@ * under the License. */ -import Fs 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 } = Fs; - jest.mock('uuid', () => ({ v4: () => 'NEW_UUID', })); -jest.mock('fs', () => { - const actual = jest.requireActual('fs'); - return { - ...actual, - readFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())), - writeFile: jest.fn().mockImplementation((...args) => process.nextTick(args.pop())), - }; -}); +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 ebe1250a8a490..3f5bdc7387392 100644 --- a/src/core/server/uuid/resolve_uuid.ts +++ b/src/core/server/uuid/resolve_uuid.ts @@ -18,18 +18,14 @@ */ import uuid from 'uuid'; -import Fs from 'fs'; -import { promisify } from 'util'; 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 = promisify(Fs.readFile); -const writeFile = promisify(Fs.writeFile); - const FILE_ENCODING = 'utf8'; const FILE_NAME = 'uuid';