Skip to content

Commit 5d4dd93

Browse files
committed
add tests for migrate-imports.js and fix for upgradeable
1 parent 30061ba commit 5d4dd93

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

scripts/migrate-imports.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
const { promises: fs } = require('fs');
44
const path = require('path');
55

6-
const versions = [
7-
'@openzeppelin/contracts',
8-
'@openzeppelin/contracts-upgradeable',
9-
];
10-
116
const pathUpdates = {
127
// 'access/AccessControl.sol': undefined,
138
// 'access/Ownable.sol': undefined,
@@ -112,15 +107,15 @@ async function main (paths = [ 'contracts' ]) {
112107
}
113108
}
114109

115-
async function listFilesRecursively (paths, filter = undefined) {
116-
const queue = Array.isArray(paths) ? paths : [ paths ];
110+
async function listFilesRecursively (paths, filter) {
111+
const queue = paths;
117112
const files = [];
118113

119114
while (queue.length > 0) {
120115
const top = queue.shift();
121116
const stat = await fs.stat(top);
122117
if (stat.isFile()) {
123-
if (!filter || top.match(filter)) {
118+
if (top.match(filter)) {
124119
files.push(top);
125120
}
126121
} else if (stat.isDirectory()) {
@@ -146,18 +141,29 @@ async function updateFile (file, update) {
146141

147142
function updateImportPaths (source) {
148143
for (const [ oldPath, newPath ] of Object.entries(pathUpdates)) {
149-
for (const version of versions) {
150-
source = source.replace(
151-
path.join(version, oldPath),
152-
path.join(version, newPath),
153-
);
154-
}
144+
source = source.replace(
145+
path.join('@openzeppelin/contracts', oldPath),
146+
path.join('@openzeppelin/contracts', newPath),
147+
);
148+
source = source.replace(
149+
path.join('@openzeppelin/contracts-upgradeable', getUpgradeablePath(oldPath)),
150+
path.join('@openzeppelin/contracts-upgradeable', getUpgradeablePath(newPath)),
151+
);
155152
}
156153

157154
return source;
158155
}
159156

160-
module.exports = main;
157+
function getUpgradeablePath (file) {
158+
const { dir, name, ext } = path.parse(file);
159+
const upgradeableName = name + 'Upgradeable';
160+
return path.format({ dir, ext, name: upgradeableName });
161+
}
162+
163+
module.exports = {
164+
pathUpdates,
165+
updateImportPaths,
166+
};
161167

162168
if (require.main === module) {
163169
const args = process.argv.length > 2 ? process.argv.slice(2) : undefined;

test/migrate-imports.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const path = require('path');
2+
const { promises: fs, constants: { F_OK } } = require('fs');
3+
const { expect } = require('chai');
4+
5+
const { pathUpdates, updateImportPaths } = require('../scripts/migrate-imports.js');
6+
7+
describe('migrate-imports.js', function () {
8+
it('every new path exists', async function () {
9+
for (const p of Object.values(pathUpdates)) {
10+
await fs.access(path.join('contracts', p), F_OK);
11+
}
12+
});
13+
14+
it('replaces import paths in a file', async function () {
15+
const source = `
16+
import '@openzeppelin/contracts/math/Math.sol';
17+
import '@openzeppelin/contracts-upgradeable/math/MathUpgradeable.sol';
18+
`;
19+
const expected = `
20+
import '@openzeppelin/contracts/utils/math/Math.sol';
21+
import '@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol';
22+
`;
23+
expect(updateImportPaths(source)).to.equal(expected);
24+
});
25+
});

0 commit comments

Comments
 (0)