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
+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);
});
});
});