1/* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the 'License'); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an 'AS IS' BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import fileio from '@ohos.fileio'; 17import fileIO from '@ohos.file.fs'; 18import featureAbility from '@ohos.ability.featureAbility'; 19 20export const FILE_CONTENT = 'hello world'; 21 22import { 23 describe, it, expect 24} 25from '@ohos/hypium' 26 27export function prepareFile(fpath, content) { 28 try { 29 let fd = fileio.openSync(fpath, 0o102, 0o666) 30 fileio.ftruncateSync(fd) 31 fileio.writeSync(fd, content) 32 fileio.fsyncSync(fd) 33 fileio.closeSync(fd) 34 return true 35 } 36 catch (e) { 37 console.log('Failed to prepareFile for ' + e) 38 return false 39 } 40} 41export function prepareEmptyFile(fpath) { 42 try { 43 let fd = fileio.openSync(fpath, 0o102, 0o777) 44 fileio.closeSync(fd) 45 return true 46 } 47 catch (e) { 48 console.log('Failed to prepareFile for ' + e) 49 return false 50 } 51} 52export function fileToReadOnly(fpath) { 53 try { 54 let fd = fileio.openSync(fpath, 0o1) 55 fileio.fchmodSync(fd, 0o444) 56 fileio.fsyncSync(fd) 57 fileio.closeSync(fd) 58 return true 59 } 60 catch (e) { 61 console.log('Failed to fileToReadOnly for ' + e); 62 return false 63 } 64} 65export function fileToWriteOnly(fpath) { 66 try { 67 let fd = fileio.openSync(fpath, 0o2) 68 fileio.fchmodSync(fd, 0o222) 69 fileio.fsyncSync(fd) 70 fileio.closeSync(fd) 71 return true 72 } 73 catch (e) { 74 console.log('Failed to fileToWriteOnly ' + e) 75 return false 76 } 77} 78 79export async function nextFileName(testName) { 80 let context = featureAbility.getContext(); 81 let data = await context.getCacheDir(); 82 let BASE_PATH = data + '/'; 83 return BASE_PATH + testName + '_' + randomString(testName.length); 84} 85export async function fileName(testName) { 86 let context = featureAbility.getContext(); 87 let data = await context.getFilesDir(); 88 let BASE_PATH = data + '/'; 89 return BASE_PATH + testName + '_' + randomString(testName.length); 90} 91 92export function randomString(num) { 93 let len= num; 94 var $chars = 'aaaabbbbcccc'; 95 var maxPos = $chars.length; 96 var pwd = ''; 97 for (var i = 0; i < len; i++) { 98 pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); 99 } 100 return pwd; 101} 102 103export function forceRemoveDir(path, num) { 104 for (let i = num; i >= 0; i--) { 105 if (i < num) { 106 path = path.replace(`/d${i}`, ""); 107 } 108 fileio.rmdirSync(path); 109 } 110} 111 112function isIntNum(val) { 113 return typeof val === 'number' && val % 1 === 0; 114} 115 116function isBigInt(val) { 117 return typeof val === 'bigint'; 118} 119 120function isString(str) { 121 return (typeof str == 'string') && str.constructor == String; 122} 123 124function isBoolean(val) { 125 return typeof val == 'boolean'; 126} 127 128function isInclude(error, message) { 129 return error.toString().indexOf(message) != -1; 130} 131 132export { 133 fileio, 134 fileIO, 135 isIntNum, 136 isBigInt, 137 isString, 138 isBoolean, 139 isInclude, 140 describe, 141 it, 142 expect 143};