-
Notifications
You must be signed in to change notification settings - Fork 461
/
testUtils.js
50 lines (43 loc) · 1.38 KB
/
testUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const temp = require('temp');
function renameFileTo(oldPath, newFilename, extension = '') {
const projectPath = path.dirname(oldPath);
const newPath = path.join(projectPath, newFilename + extension);
mkdirp.sync(path.dirname(newPath));
fs.renameSync(oldPath, newPath);
return newPath;
}
function createTempFileWith(content, filename, extension) {
const info = temp.openSync({ suffix: extension });
let filePath = info.path;
fs.writeSync(info.fd, content);
fs.closeSync(info.fd);
if (filename) {
filePath = renameFileTo(filePath, filename, extension);
}
return filePath;
}
exports.createTempFileWith = createTempFileWith;
// Test transform files need a js extension to work with @babel/register
// .ts or .tsx work as well
function createTransformWith(content, ext='.js') {
return createTempFileWith(
'module.exports = function(fileInfo, api, options) { ' + content + ' }',
undefined,
ext
);
}
exports.createTransformWith = createTransformWith;
function getFileContent(filePath) {
return fs.readFileSync(filePath).toString();
}
exports.getFileContent = getFileContent;