feat(file-system): append, appendText & createFile (#10285)

This commit is contained in:
Osei Fortune
2023-05-04 23:45:01 -04:00
committed by GitHub
parent 7bb0918e08
commit ab32aeaaa3
36 changed files with 2079 additions and 654 deletions

View File

@ -5,7 +5,7 @@ import * as fs from '@nativescript/core/file-system';
import * as TKUnit from '../tk-unit';
import * as appModule from '@nativescript/core/application';
import { isIOS, Device, platformNames } from '@nativescript/core';
import { isIOS, Device, platformNames, isAndroid } from '@nativescript/core';
export var testPathNormalize = function () {
// >> file-system-normalize
@ -719,3 +719,62 @@ export function test_FileCopy(done) {
.then(() => done())
.catch(done);
}
export function testAndroidCreate() {
let testFunc = function testFunc() {
const file = fs.File.android.createFile({
directory: fs.AndroidDirectory.DOWNLOADS,
name: `${Date.now()}.txt`,
mime: 'text/plain',
relativePath: `NativeScript`,
});
file.writeTextSync('some text');
return file;
};
if (isAndroid) {
const file = testFunc();
TKUnit.assertEqual(file.readTextSync(), 'some text', `The contents of the new file created in the 'AndroidDirectory.DOWNLOADS' folder are not as expected.`);
file.removeSync();
TKUnit.assertTrue(!fs.File.exists(file.path));
} else {
TKUnit.assertThrows(testFunc, `Trying to retrieve createFile on a platform different from Android should throw!`, `createFile is available on Android only!`);
}
}
export function test_FileAppend(done) {
const content = 'Hello World';
const hello_world = global.isIOS ? NSString.stringWithString(content).dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String(content).getBytes('UTF-8');
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-app.txt`));
file
.appendText('Hello')
.then(() => file.appendText(' World'))
.then(() => {
TKUnit.assert(file.size === hello_world.length);
return file.readText();
})
.then((value) => {
TKUnit.assert(value === content);
return Promise.allSettled([file.remove()]);
})
.then(() => done())
.catch(done);
}
export function test_FileAppendText(done) {
const content = 'Hello World';
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-app.txt`));
file
.appendText('Hello')
.then(() => file.appendText(' World'))
.then(() => file.readText())
.then((value) => {
TKUnit.assert(value === content);
return Promise.allSettled([file.remove()]);
})
.then(() => done())
.catch(done);
}