Skip to content

Commit

Permalink
remove use of experimental fs.promises api (#53346) (#53796)
Browse files Browse the repository at this point in the history
* 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 <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
Spencer and elasticmachine authored Dec 26, 2019
1 parent 88f8bef commit dbf1c3c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
24 changes: 24 additions & 0 deletions src/core/server/uuid/fs.ts
Original file line number Diff line number Diff line change
@@ -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);
19 changes: 5 additions & 14 deletions src/core/server/uuid/resolve_uuid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
4 changes: 1 addition & 3 deletions src/core/server/uuid/resolve_uuid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
6 changes: 3 additions & 3 deletions utilities/visual_regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

Expand All @@ -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);
Expand Down

0 comments on commit dbf1c3c

Please sign in to comment.