refactor: replace fs-extra with node:fs (#1772)

* spec: add devDependencies "tmp"
This commit is contained in:
エリス
2025-01-29 10:39:11 +09:00
committed by GitHub
parent b623311efa
commit e012478537
20 changed files with 188 additions and 164 deletions
+37 -23
View File
@@ -19,26 +19,40 @@
const rewire = require('rewire');
const common = rewire('../../../lib/pluginHandlers');
const path = require('node:path');
const fs = require('fs-extra');
const osenv = require('node:os');
const fs = require('node:fs');
const tmp = require('tmp');
const test_dir = path.join(osenv.tmpdir(), 'test_plugman');
tmp.setGracefulCleanup();
const tempdir = tmp.dirSync({ unsafeCleanup: true });
const test_dir = path.join(tempdir.name, 'test_plugman');
const project_dir = path.join(test_dir, 'project');
const src = path.join(project_dir, 'src');
const dest = path.join(project_dir, 'dest');
const java_dir = path.join(src, 'one', 'two', 'three');
const java_file = path.join(java_dir, 'test.java');
const symlink_file = path.join(java_dir, 'symlink');
const non_plugin_file = path.join(osenv.tmpdir(), 'non_plugin_file');
const non_plugin_file = path.join(tempdir.name, 'non_plugin_file');
const copyFile = common.__get__('copyFile');
const deleteJava = common.__get__('deleteJava');
const copyNewFile = common.__get__('copyNewFile');
function outputFileSync (file, content) {
const dir = path.dirname(file);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(file, content, 'utf-8');
}
describe('common platform handler', function () {
afterAll(() => {
// Remove tempdir after all specs complete
fs.rmSync(tempdir.name, { recursive: true, force: true });
});
afterEach(() => {
fs.removeSync(test_dir);
fs.removeSync(non_plugin_file);
fs.rmSync(test_dir, { recursive: true, force: true });
fs.rmSync(non_plugin_file, { recursive: true, force: true });
});
describe('copyFile', function () {
@@ -48,15 +62,15 @@ describe('common platform handler', function () {
});
it('Test#002 : should throw if src not in plugin directory', function () {
fs.ensureDirSync(project_dir);
fs.outputFileSync(non_plugin_file, 'contents');
fs.mkdirSync(project_dir, { recursive: true });
outputFileSync(non_plugin_file, 'contents');
const outside_file = '../non_plugin_file';
expect(function () { copyFile(test_dir, outside_file, project_dir, dest); })
.toThrow(new Error('File "' + path.resolve(test_dir, outside_file) + '" is located outside the plugin directory "' + test_dir + '"'));
});
it('Test#003 : should allow symlink src, if inside plugin', function () {
fs.outputFileSync(java_file, 'contents');
outputFileSync(java_file, 'contents');
// This will fail on windows if not admin - ignore the error in that case.
if (ignoreEPERMonWin32(java_file, symlink_file)) {
@@ -67,8 +81,8 @@ describe('common platform handler', function () {
});
it('Test#004 : should throw if symlink is linked to a file outside the plugin', function () {
fs.ensureDirSync(java_dir);
fs.outputFileSync(non_plugin_file, 'contents');
fs.mkdirSync(java_dir, { recursive: true });
outputFileSync(non_plugin_file, 'contents');
// This will fail on windows if not admin - ignore the error in that case.
if (ignoreEPERMonWin32(non_plugin_file, symlink_file)) {
@@ -80,37 +94,37 @@ describe('common platform handler', function () {
});
it('Test#005 : should throw if dest is outside the project directory', function () {
fs.outputFileSync(java_file, 'contents');
outputFileSync(java_file, 'contents');
expect(function () { copyFile(test_dir, java_file, project_dir, non_plugin_file); })
.toThrow(new Error('Destination "' + path.resolve(project_dir, non_plugin_file) + '" for source file "' + path.resolve(test_dir, java_file) + '" is located outside the project'));
});
it('Test#006 : should call mkdir -p on target path', function () {
fs.outputFileSync(java_file, 'contents');
it('Test#006 : should call mkdirSync target path', function () {
outputFileSync(java_file, 'contents');
const s = spyOn(fs, 'ensureDirSync').and.callThrough();
const s = spyOn(fs, 'mkdirSync').and.callThrough();
const resolvedDest = path.resolve(project_dir, dest);
copyFile(test_dir, java_file, project_dir, dest);
expect(s).toHaveBeenCalled();
expect(s).toHaveBeenCalledWith(path.dirname(resolvedDest));
expect(s).toHaveBeenCalledWith(path.dirname(resolvedDest), { recursive: true });
});
it('Test#007 : should call cp source/dest paths', function () {
fs.outputFileSync(java_file, 'contents');
outputFileSync(java_file, 'contents');
const s = spyOn(fs, 'copySync').and.callThrough();
const s = spyOn(fs, 'cpSync').and.callThrough();
const resolvedDest = path.resolve(project_dir, dest);
copyFile(test_dir, java_file, project_dir, dest);
expect(s).toHaveBeenCalled();
expect(s).toHaveBeenCalledWith(java_file, resolvedDest);
expect(s).toHaveBeenCalledWith(java_file, resolvedDest, { recursive: true });
});
it('should handle relative paths when checking for sub paths', () => {
fs.outputFileSync(java_file, 'contents');
outputFileSync(java_file, 'contents');
const relativeProjectPath = path.relative(process.cwd(), project_dir);
expect(() => {
@@ -121,7 +135,7 @@ describe('common platform handler', function () {
describe('copyNewFile', function () {
it('Test#008 : should throw if target path exists', function () {
fs.ensureDirSync(dest);
fs.mkdirSync(dest, { recursive: true });
expect(function () { copyNewFile(test_dir, src, project_dir, dest); })
.toThrow(new Error('"' + dest + '" already exists!'));
});
@@ -129,11 +143,11 @@ describe('common platform handler', function () {
describe('deleteJava', function () {
beforeEach(function () {
fs.outputFileSync(java_file, 'contents');
outputFileSync(java_file, 'contents');
});
it('Test#009 : should call fs.unlinkSync on the provided paths', function () {
const s = spyOn(fs, 'removeSync').and.callThrough();
const s = spyOn(fs, 'rmSync').and.callThrough();
deleteJava(project_dir, java_file);
expect(s).toHaveBeenCalled();
expect(s).toHaveBeenCalledWith(path.resolve(project_dir, java_file));
+35 -35
View File
@@ -21,7 +21,7 @@ const rewire = require('rewire');
const common = rewire('../../../lib/pluginHandlers');
const android = common.__get__('handlers');
const path = require('node:path');
const fs = require('fs-extra');
const fs = require('node:fs');
const os = require('node:os');
const temp = path.join(os.tmpdir(), 'plugman');
const plugins_dir = path.join(temp, 'cordova/plugins');
@@ -56,14 +56,14 @@ describe('android project handler', function () {
let dummyProject;
beforeEach(function () {
fs.ensureDirSync(temp);
fs.mkdirSync(temp, { recursive: true });
dummyProject = AndroidProject.getProjectFile(temp);
copyFileSpy.calls.reset();
common.__set__('copyFile', copyFileSpy);
});
afterEach(function () {
fs.removeSync(temp);
fs.rmSync(temp, { recursive: true, force: true });
common.__set__('copyFile', copyFileOrig);
});
@@ -83,7 +83,7 @@ describe('android project handler', function () {
describe('of <source-file> elements', function () {
beforeEach(function () {
fs.copySync(android_studio_project, temp);
fs.cpSync(android_studio_project, temp, { recursive: true });
});
it('Test#003 : should copy stuff from one location to another by calling common.copyFile', function () {
@@ -102,7 +102,7 @@ describe('android project handler', function () {
it('Test#006 : should throw if target file already exists', function () {
// write out a file
let target = path.resolve(temp, 'app', 'src', 'main', 'java', 'com', 'phonegap', 'plugins', 'dummyplugin');
fs.ensureDirSync(target);
fs.mkdirSync(target, { recursive: true });
target = path.join(target, 'DummyPlugin.java');
fs.writeFileSync(target, 'some bs', 'utf-8');
@@ -192,7 +192,7 @@ describe('android project handler', function () {
const copyNewFileSpy = jasmine.createSpy('copyNewFile');
beforeEach(function () {
fs.copySync(android_studio_project, temp);
fs.cpSync(android_studio_project, temp, { recursive: true });
spyOn(dummyProject, 'addSystemLibrary');
spyOn(dummyProject, 'addSubProject');
@@ -222,7 +222,7 @@ describe('android project handler', function () {
it('Test#010 : should not copy anything if "custom" attribute is not set', function () {
const framework = { src: 'plugin-lib' };
const cpSpy = spyOn(fs, 'copySync');
const cpSpy = spyOn(fs, 'cpSync');
android.framework.install(framework, dummyPluginInfo, dummyProject);
expect(dummyProject.addSystemLibrary).toHaveBeenCalledWith(someString, framework.src);
expect(cpSpy).not.toHaveBeenCalled();
@@ -289,23 +289,23 @@ describe('android project handler', function () {
describe('uninstallation', function () {
const deleteJavaOrig = common.__get__('deleteJava');
const originalRemoveSync = fs.removeSync;
const originalRmSync = fs.rmSync;
const deleteJavaSpy = jasmine.createSpy('deleteJava');
let dummyProject;
let removeSyncSpy;
let rmSyncSpy;
beforeEach(function () {
fs.ensureDirSync(temp);
fs.ensureDirSync(plugins_dir);
fs.copySync(android_studio_project, temp);
fs.mkdirSync(temp, { recursive: true });
fs.mkdirSync(plugins_dir, { recursive: true });
fs.cpSync(android_studio_project, temp, { recursive: true });
AndroidProject.purgeCache();
dummyProject = AndroidProject.getProjectFile(temp);
removeSyncSpy = spyOn(fs, 'removeSync');
rmSyncSpy = spyOn(fs, 'rmSync');
common.__set__('deleteJava', deleteJavaSpy);
});
afterEach(function () {
originalRemoveSync.call(fs, temp);
originalRmSync.call(fs, temp, { recursive: true });
common.__set__('deleteJava', deleteJavaOrig);
});
@@ -313,7 +313,7 @@ describe('android project handler', function () {
it('Test#017 : should remove jar files for Android Studio projects', function () {
android['lib-file'].install(valid_libs[0], dummyPluginInfo, dummyProject);
android['lib-file'].uninstall(valid_libs[0], dummyPluginInfo, dummyProject);
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestLib.jar'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestLib.jar'));
});
});
@@ -321,7 +321,7 @@ describe('android project handler', function () {
it('Test#018 : should remove files for Android Studio projects', function () {
android['resource-file'].install(valid_resources[0], dummyPluginInfo, dummyProject);
android['resource-file'].uninstall(valid_resources[0], dummyPluginInfo, dummyProject);
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app', 'src', 'main', 'res', 'xml', 'dummy.xml'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app', 'src', 'main', 'res', 'xml', 'dummy.xml'));
});
});
@@ -341,49 +341,49 @@ describe('android project handler', function () {
it('Test#019b : should remove stuff by calling common.removeFile for Android Studio projects, of jar with new app target-dir scheme', function () {
android['source-file'].install(valid_source[2], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[2], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestLib.jar'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestLib.jar'));
});
it('Test#019c : should remove stuff by calling common.removeFile for Android Studio projects, of aar with new app target-dir scheme', function () {
android['source-file'].install(valid_source[3], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[3], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestAar.aar'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/TestAar.aar'));
});
it('Test#019d : should remove stuff by calling common.removeFile for Android Studio projects, of xml with old target-dir scheme', function () {
android['source-file'].install(valid_source[4], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[4], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/res/xml/mysettings.xml'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/res/xml/mysettings.xml'));
});
it('Test#019e : should remove stuff by calling common.removeFile for Android Studio projects, of file with other extension with old target-dir scheme', function () {
android['source-file'].install(valid_source[5], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[5], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/res/values/other.extension'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/res/values/other.extension'));
});
it('Test#019f : should remove stuff by calling common.removeFile for Android Studio projects, of aidl with old target-dir scheme (GH-547)', function () {
android['source-file'].install(valid_source[6], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[6], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/aidl/com/mytest/myapi.aidl'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/aidl/com/mytest/myapi.aidl'));
});
it('Test#019g : should remove stuff by calling common.removeFile for Android Studio projects, of aar with old target-dir scheme (GH-547)', function () {
android['source-file'].install(valid_source[7], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[7], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/testaar2.aar'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/testaar2.aar'));
});
it('Test#019h : should remove stuff by calling common.removeFile for Android Studio projects, of jar with old target-dir scheme (GH-547)', function () {
android['source-file'].install(valid_source[8], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[8], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/testjar2.jar'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/libs/testjar2.jar'));
});
it('Test#019i : should remove stuff by calling common.removeFile for Android Studio projects, of .so lib file with old target-dir scheme (GH-547)', function () {
android['source-file'].install(valid_source[9], dummyPluginInfo, dummyProject, { android_studio: true });
android['source-file'].uninstall(valid_source[9], dummyPluginInfo, dummyProject, { android_studio: true });
expect(removeSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/jniLibs/x86/libnative.so'));
expect(rmSyncSpy).toHaveBeenCalledWith(path.join(dummyProject.projectDir, 'app/src/main/jniLibs/x86/libnative.so'));
});
it('Test#019j : should remove stuff by calling common.deleteJava for Android Studio projects, with target-dir that includes "app"', function () {
@@ -397,7 +397,7 @@ describe('android project handler', function () {
const someString = jasmine.any(String);
beforeEach(function () {
fs.ensureDirSync(path.join(dummyProject.projectDir, dummyPluginInfo.id));
fs.mkdirSync(path.join(dummyProject.projectDir, dummyPluginInfo.id), { recursive: true });
spyOn(dummyProject, 'removeSystemLibrary');
spyOn(dummyProject, 'removeSubProject');
@@ -424,13 +424,13 @@ describe('android project handler', function () {
const framework = { src: 'plugin-lib', custom: true };
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
expect(dummyProject.removeSubProject).toHaveBeenCalledWith(dummyProject.projectDir, someString);
expect(removeSyncSpy).toHaveBeenCalledWith(someString);
expect(rmSyncSpy).toHaveBeenCalledWith(someString);
});
it('Test#24 : should install gradleReference using project.removeGradleReference', function () {
const framework = { src: 'plugin-lib', custom: true, type: 'gradleReference' };
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
expect(removeSyncSpy).toHaveBeenCalledWith(someString);
expect(rmSyncSpy).toHaveBeenCalledWith(someString);
expect(dummyProject.removeGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
});
});
@@ -453,14 +453,14 @@ describe('android project handler', function () {
it('Test#025 : should put module to both www and platform_www when options.usePlatformWww flag is specified', function () {
android['js-module'].uninstall(jsModule, dummyPluginInfo, dummyProject, { usePlatformWww: true });
expect(removeSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(removeSyncSpy).toHaveBeenCalledWith(platformWwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(platformWwwDest);
});
it('Test#026 : should put module to www only when options.usePlatformWww flag is not specified', function () {
android['js-module'].uninstall(jsModule, dummyPluginInfo, dummyProject);
expect(removeSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(removeSyncSpy).not.toHaveBeenCalledWith(platformWwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(rmSyncSpy).not.toHaveBeenCalledWith(platformWwwDest);
});
});
@@ -481,14 +481,14 @@ describe('android project handler', function () {
it('Test#027 : should put module to both www and platform_www when options.usePlatformWww flag is specified', function () {
android.asset.uninstall(asset, dummyPluginInfo, dummyProject, { usePlatformWww: true });
expect(removeSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(removeSyncSpy).toHaveBeenCalledWith(platformWwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(platformWwwDest);
});
it('Test#028 : should put module to www only when options.usePlatformWww flag is not specified', function () {
android.asset.uninstall(asset, dummyPluginInfo, dummyProject);
expect(removeSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(removeSyncSpy).not.toHaveBeenCalledWith(platformWwwDest);
expect(rmSyncSpy).toHaveBeenCalledWith(wwwDest);
expect(rmSyncSpy).not.toHaveBeenCalledWith(platformWwwDest);
});
});
});