1/* 2 * Copyright (C) 2021 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 featureAbility from '@ohos.ability.featureAbility' 18 19export const FILE_CONTENT = 'hello world'; 20 21import { 22 describe, it, expect 23} 24from '@ohos/hypium' 25 26export function prepareFile(fpath, content) { 27 try { 28 let fd = fileio.openSync(fpath, 0o102, 0o666) 29 fileio.ftruncateSync(fd) 30 fileio.writeSync(fd, content) 31 fileio.fsyncSync(fd) 32 fileio.closeSync(fd) 33 return true 34 } 35 catch (e) { 36 console.log('Failed to prepareFile for ' + e) 37 return false 38 } 39} 40export function prepareEmptyFile(fpath) { 41 try { 42 let fd = fileio.openSync(fpath, 0o102, 0o777) 43 fileio.closeSync(fd) 44 return true 45 } 46 catch (e) { 47 console.log('Failed to prepareFile for ' + e) 48 return false 49 } 50} 51 52export async function nextFileName(testName) { 53 var context = featureAbility.getContext(); 54 let data = await context.getCacheDir(); 55 let BASE_PATH = data + '/'; 56 return BASE_PATH + testName 57} 58export async function fileName(testName) { 59 var context = featureAbility.getContext(); 60 let data = await context.getFilesDir(); 61 let BASE_PATH = data + '/'; 62 return BASE_PATH + testName 63} 64export async function cacheFileName(testName) { 65 var context = featureAbility.getContext(); 66 let data = await context.getFilesDir(); 67 let BASE_PATH = data + '/cache/'; 68 return BASE_PATH + testName 69} 70 71export function sleep(n) { 72 var start = new Date().getTime(); 73 while (true) { 74 if (new Date().getTime() - start > n) { 75 break; 76 } 77 } 78} 79 80export function randomString(num) { 81 let len= num; 82 var $chars = 'aaaabbbbcccc'; 83 var maxPos = $chars.length; 84 var pwd = ''; 85 for (var i = 0; i < len; i++) { 86 pwd += $chars.charAt(Math.floor(Math.random() * maxPos)); 87 } 88 return pwd; 89} 90 91export { 92 fileio, 93 describe, 94 it, 95 expect 96};