From f3fa969b0f42a971f42f267d74638a7c3260eb80 Mon Sep 17 00:00:00 2001 From: duqingquan Date: Wed, 13 Dec 2023 19:33:01 +0800 Subject: [PATCH] =?UTF-8?q?get-file-system-manager=20=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E8=B0=83=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../get-file-system-manager.test.js | 84 +++++++------ .../get-file-system-manager.uvue | 117 +++++++++++++----- 2 files changed, 133 insertions(+), 68 deletions(-) diff --git a/pages/API/get-file-system-manager/get-file-system-manager.test.js b/pages/API/get-file-system-manager/get-file-system-manager.test.js index c2518f93..7fbc71e1 100644 --- a/pages/API/get-file-system-manager/get-file-system-manager.test.js +++ b/pages/API/get-file-system-manager/get-file-system-manager.test.js @@ -30,29 +30,23 @@ describe('ExtApi-FileManagerTest', () => { let globalUserDataPath = await getData('globalUserDataPath') await page.setData({ + logAble:false, recursiveVal: true, copyToBasePath:globalUserDataPath, basePath: globalUserDataPath, rmDirFile:'a', - unlinkFile:'a/1.txt' + readDir:'a', + writeFile:'a/1.txt', + readFile:'a/1.txt', + unlinkFile:'a/1.txt', + writeFileContent:'锄禾日当午,汗滴禾下土,谁知盘中餐,粒粒皆辛苦' }) // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败 - const btnUnLinkFileButton = await page.$('.btn-unlink-file') - await btnUnLinkFileButton.tap() - await isDone() - - await page.setData({ - unlinkFile:'a/2.txt' - }) + const btnUnLinkFileButton = await page.$('.btn-clear-file') await btnUnLinkFileButton.tap() await isDone() - await page.setData({ - unlinkFile:'a/3.txt' - }) - await btnUnLinkFileButton.tap() - await isDone() // 清除文件夹 const btnRmDirButton = await page.$('.btn-remove-dir') @@ -200,17 +194,18 @@ describe('ExtApi-FileManagerTest', () => { fileListSuccess = await getData('fileListSuccess') expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\",\"1.txt\",\"3.txt\"]") - }); it('TEMP_PATH test', async () => { // 测试 TEMP_PATH let globalTempPath = await getData('globalTempPath') await page.setData({ + logAble:false, recursiveVal: true, basePath: globalTempPath, copyToBasePath:globalTempPath, rmDirFile:'a', + mkdirFile: 'a', unlinkFile:'a/我们经历了一场兵慌马乱的战争.1@2#3$4%5^6&7*8(9)0+-qwertyuiopasdfghjklzxcvbnm;,/中文路径/张三/name/中文文件.mock' }) @@ -220,12 +215,6 @@ describe('ExtApi-FileManagerTest', () => { await btnUnLinkFileButton.tap() await isDone() - await page.setData({ - unlinkFile:'a/提前创建的目录/4.txt' - }) - await btnUnLinkFileButton.tap() - await isDone() - // 清除文件夹 const btnRmDirButton = await page.$('.btn-remove-dir') @@ -244,9 +233,9 @@ describe('ExtApi-FileManagerTest', () => { // 期望通过 recursive = true的 文件夹删除,得到一个空的 /a 目录 let fileListComplete = await getData('fileListComplete') - expect(JSON.stringify(fileListComplete)).toEqual("[\"b\"]") + expect(JSON.stringify(fileListComplete)).toEqual("[]") let fileListSuccess = await getData('fileListSuccess') - expect(JSON.stringify(fileListSuccess)).toEqual("[\"b\"]") + expect(JSON.stringify(fileListSuccess)).toEqual("[]") // 测试 创建多层级文件目录 await page.setData({ @@ -286,10 +275,8 @@ describe('ExtApi-FileManagerTest', () => { * 从资源文件中读取图片为base64,测试写入较大文件场景 * 'static/list-mock/safe.png' 注意,依赖这个资源文件,不能删除 */ - let globalAppResourcePath = await getData('globalAppResourcePath') await page.setData({ - - basePath: globalAppResourcePath, + basePath: "", readFile:'static/list-mock/safe.png', readFileEncoding:'base64' }) @@ -434,7 +421,8 @@ describe('ExtApi-FileManagerTest', () => { expect(JSON.stringify(fileListSuccess)).toEqual("[\"4.txt\"]") await page.setData({ - unlinkFile:'a/提前创建的目录/4.txt' + unlinkFile:'a/提前创建的目录/4.txt', + rmDirFile:'a/提前创建的目录' }) await btnUnLinkFileButton.tap() await isDone() @@ -447,12 +435,10 @@ describe('ExtApi-FileManagerTest', () => { fileListSuccess = await getData('fileListSuccess') expect(JSON.stringify(fileListSuccess)).toEqual("[]") - }); - it('CROSS DIR test', async () => { /** * 跨越用户目录和代码资源目录 @@ -460,6 +446,7 @@ describe('ExtApi-FileManagerTest', () => { let globalRootPath = await getData('globalRootPath') await page.setData({ recursiveVal: true, + logAble:false, basePath: globalRootPath, readDir:'a', rmDirFile:'a', @@ -470,8 +457,8 @@ describe('ExtApi-FileManagerTest', () => { // 先清除文件,需要清除全部可能存在的历史测试文件,避免运行失败 - const btnUnLinkFileButton = await page.$('.btn-unlink-file') - await btnUnLinkFileButton.tap() + const btnClearFileButton = await page.$('.btn-clear-file') + await btnClearFileButton.tap() await isDone() @@ -505,9 +492,8 @@ describe('ExtApi-FileManagerTest', () => { // 准备从资源目录拷贝png - let globalAppResourcePath = await getData('globalAppResourcePath') await page.setData({ - basePath: globalAppResourcePath, + basePath: "", unlinkFile:'static/list-mock/safe.png', accessFile:'static/list-mock/safe.png', }) @@ -519,6 +505,7 @@ describe('ExtApi-FileManagerTest', () => { expect(accessFileRet).toEqual('access:ok') // 尝试删除资源,期望失败 + const btnUnLinkFileButton = await page.$('.btn-unlink-file') await btnUnLinkFileButton.tap() await isDone() @@ -542,6 +529,7 @@ describe('ExtApi-FileManagerTest', () => { basePath:globalRootPath, unlinkFile:'a/从代码目录拷贝的资源.png', accessFile:'a/从代码目录拷贝的资源.png', + rmDirFile:'a', }) await btnAccessFileButton.tap() await isDone() @@ -549,6 +537,7 @@ describe('ExtApi-FileManagerTest', () => { accessFileRet = await getData("accessFileRet") expect(accessFileRet).toEqual('access:ok') + await btnUnLinkFileButton.tap() await isDone() @@ -558,6 +547,31 @@ describe('ExtApi-FileManagerTest', () => { accessFileRet = await getData("accessFileRet") expect(accessFileRet).toEqual('') + // 从页面的按钮触发一次文件复制 + const btnCopyStaticFileButton = await page.$('.btn-copyStatic-file') + await btnCopyStaticFileButton.tap() + await isDone() + + await btnReadDirButton.tap() + await isDone() + + fileListComplete = await getData('fileListComplete') + expect(JSON.stringify(fileListComplete)).toEqual("[\"mock.json\"]") + fileListSuccess = await getData('fileListSuccess') + expect(JSON.stringify(fileListSuccess)).toEqual("[\"mock.json\"]") + + // 从页面的按钮触发一次文件清空 + await btnClearFileButton.tap() + await isDone() + + await btnReadDirButton.tap() + await isDone() + + fileListComplete = await getData('fileListComplete') + expect(JSON.stringify(fileListComplete)).toEqual("[]") + fileListSuccess = await getData('fileListSuccess') + expect(JSON.stringify(fileListSuccess)).toEqual("[]") + }); @@ -568,6 +582,7 @@ describe('ExtApi-FileManagerTest', () => { let globalTempPath = await getData('globalTempPath') await page.setData({ recursiveVal: true, + logAble:false, basePath: globalTempPath, readDir:'d', rmDirFile:'d', @@ -831,6 +846,7 @@ describe('ExtApi-FileManagerTest', () => { expect(statsRet[4].path).toEqual('/storage/emulated/0/Android/data/io.dcloud.uniappx/a/m/3.txt') expect(statsRet[4].stats.size).toEqual(5842) + // 清理文件,避免影响其他测试用例 await page.setData({ unlinkFile:'a/1.txt', @@ -865,10 +881,6 @@ describe('ExtApi-FileManagerTest', () => { fileListSuccess = await getData('fileListSuccess') expect(JSON.stringify(fileListSuccess)).toEqual('[]') - - - - }); }); diff --git a/pages/API/get-file-system-manager/get-file-system-manager.uvue b/pages/API/get-file-system-manager/get-file-system-manager.uvue index b456e0c5..622efb8c 100644 --- a/pages/API/get-file-system-manager/get-file-system-manager.uvue +++ b/pages/API/get-file-system-manager/get-file-system-manager.uvue @@ -15,7 +15,7 @@ - + @@ -29,11 +29,15 @@ data() { return { log: "", + /** + * 自动化测试需要关闭log + */ + logAble:true, fileListSuccess: [] as string[], fileListComplete: [] as string[], accessFileRet: '', - lastFailError: UniError("uni-file-manager", 1300000, "mock error"), - lastCompleteError: UniError("uni-file-manager", 1300000, "mock error"), + lastFailError: new UniError("uni-file-manager", 1300000, "mock error"), + lastCompleteError: new UniError("uni-file-manager", 1300000, "mock error"), readDir: 'a', readFileRet: "", writeFileContent: "中文 en.\r\n\t换行", @@ -64,8 +68,6 @@ copyToBasePath: uni.env.USER_DATA_PATH, globalTempPath: uni.env.CACHE_PATH, globalRootPath: uni.env.SANDBOX_PATH, - globalInnerRootPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH, - globalAppResourcePath: uni.env.APP_RESOURCE_PATH, globalUserDataPath: uni.env.USER_DATA_PATH } }, @@ -81,13 +83,17 @@ path: `${this.basePath}${this.statFile}`, recursive: this.recursiveVal, success: function (res : StatSuccessResult) { - this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'statFileInfoTest success:' + JSON.stringify(res) + '\n\n' + } console.log('statFileInfoTest success', res) this.statsRet = res.stats console.log('this.statsRet', this.statsRet) }, fail: function (res : UniError) { - this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'statFileInfoTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('statFileInfoTest fail', res) this.lastFailError = res }, @@ -108,13 +114,17 @@ filePath: `${this.basePath}${this.getFileInfoFile}`, digestAlgorithm: this.getFileInfoAlgorithm, success: function (res : GetFileInfoSuccessResult) { - this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'getFileInfoTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) this.getFileInfoSize = res.size this.getFileInfoDigest = res.digest }, fail: function (res : UniError) { - this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'getFileInfoTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -137,11 +147,15 @@ srcPath: `${this.basePath}${this.copyFromFile}`, destPath: `${this.copyToBasePath}${this.copyToFile}`, success: function (res : FileManagerSuccessResult) { - this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -163,11 +177,15 @@ oldPath: `${this.basePath}${this.renameFromFile}`, newPath: `${this.basePath}${this.renameToFile}`, success: function (res : FileManagerSuccessResult) { - this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'renameFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'renameFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -186,12 +204,16 @@ fileManager.readdir({ dirPath: `${this.basePath}${this.readDir}`, success: function (res : ReadDirSuccessResult) { - this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'readDirTest success:' + JSON.stringify(res) + '\n\n' + } console.log("success", res) this.fileListSuccess = res.files }, fail: function (res : UniError) { - this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'readDirTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -217,11 +239,15 @@ data: this.writeFileContent, encoding: this.writeFileEncoding, success: function (res : FileManagerSuccessResult) { - this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'writeFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'writeFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail') this.lastFailError = res }, @@ -245,12 +271,16 @@ filePath: `${this.basePath}${this.readFile}`, encoding: this.readFileEncoding, success: function (res : ReadFileSuccessResult) { - this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'readFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) this.readFileRet = res.data }, fail: function (res : UniError) { - this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'readFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -270,11 +300,15 @@ dirPath: `${this.basePath}${this.rmDirFile}`, recursive: this.recursiveVal, success: function (res : FileManagerSuccessResult) { - this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'rmdirTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'rmdirTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -297,11 +331,15 @@ dirPath: `${this.basePath}${this.mkdirFile}`, recursive: this.recursiveVal, success: function (res : FileManagerSuccessResult) { - this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'mkdirTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'mkdirTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -318,16 +356,19 @@ accessFileTest: function (e : any) { this.accessFileRet = '' let fileManager = uni.getFileSystemManager() - fileManager.access({ path: `${this.basePath}${this.accessFile}`, success: function (res : FileManagerSuccessResult) { - this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'accessFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) this.accessFileRet = res.errMsg }, fail: function (res : UniError) { - this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'accessFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -348,11 +389,15 @@ fileManager.unlink({ filePath: `${this.basePath}${this.unlinkFile}`, success: function (res : FileManagerSuccessResult) { - this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'unlinkTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'unlinkTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, @@ -376,11 +421,15 @@ fileManager.unlink({ filePath: `${this.basePath}${this.rmDirFile}/${element}`, success: function (res : FileManagerSuccessResult) { - this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'unlinkAllFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success unlink', res) }, fail: function (res : UniError) { - this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'unlinkAllFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail unlink', res) this.lastFailError = res }, @@ -419,11 +468,15 @@ srcPath: UTSAndroid.getResourcePath("static/list-mock/mock.json"), destPath: `${this.copyToBasePath}/a/mock.json`, success: function (res : FileManagerSuccessResult) { - this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'copyFileTest success:' + JSON.stringify(res) + '\n\n' + } console.log('success', res) }, fail: function (res : UniError) { - this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n' + if(this.logAble){ + this.log += 'copyFileTest fail:' + JSON.stringify(res) + '\n\n' + } console.log('fail', res) this.lastFailError = res }, -- GitLab