// // # File System // Using the file system requires the FileSystem module. // ``` JavaScript var fs = require("filesystem"); // ``` // The pre-required `fs` module is used throughout the following code snippets. // var TKUnit = require("Tests/TKUnit"); // // ## Create // // TODO: Split the "Read and Write" in "Read" and "Write" export var testFileReadWrite = function () { // TODO: Add description if writeText replaces the file? Does it create the file? Does it append to existing file? // // ### Writing and Reading a String to a File // The following example writes some text to a file and then reads it back. // ``` JavaScript var documents = fs.knownFolders.documents(); var myFile = documents.getFile("Test_Write.txt"); var written: boolean; //// Writing text to the file. myFile.writeText("Something") .then(function () { //// Succeeded writing to the file. //// Getting back the contents of the file. myFile.readText() .then(function (content) { //// Successfuly read the file's content. // written = content === "Something"; TKUnit.assert(written, "File read/write not working."); myFile.remove(); // }) .fail(function (error) { //// Failed to read from the file. // console.error("Failed to read/write text"); console.dump(error); // }); }) .fail(function (error) { //// Failed to write to the file. // console.error("Failed to read/write text"); console.dump(error); // }); // ``` // }; // // ## Read // export var testGetKnownFolders = function () { // // ### Getting the Known Folders // Each app has several well known folders. This is how to access them: // ``` JavaScript //// Getting the application's 'documents' folder. var documents = fs.knownFolders.documents(); // TKUnit.assert(documents, "Could not retrieve the Documents known folder."); TKUnit.assert(documents.isKnown, "The Documents folder should have its isKnown property set to true."); // //// Getting the application's 'temp' folder. var temp = fs.knownFolders.temp(); // TKUnit.assert(temp, "Could not retrieve the Temporary known folder."); TKUnit.assert(temp.isKnown, "The Temporary folder should have its isKnown property set to true."); // // ``` // }; export var testGetEntities = function () { // // ### Getting Folder Contents // Getting all files and folders within a folder: // ``` JavaScript var documents = fs.knownFolders.documents(); // var file = documents.getFile("Test.txt"); var file1 = documents.getFile("Test1.txt"); var fileFound, file1Found; // IMPORTANT: console.log is mocked to make the snippet pretty. var globalConsole = console; var console = { log: function (file) { if (file === "Test.txt") { fileFound = true; } else if (file === "Test1.txt") { file1Found = true; } } }; // documents.getEntities() .then(function (entities) { //// entities is array with the document's files and folders. entities.forEach(function (entity) { console.log(entity.name); }); // TKUnit.assert(fileFound, "Failed to enumerate Test.txt"); TKUnit.assert(file1Found, "Failed to enumerate Test1.txt"); file.remove(); file1.remove(); // }) .fail(function (error) { //// Failed to obtain folder's contents. // globalConsole.error(error.message); }); // ``` // }; export var testEnumEntities = function () { // // ### Enumerating Folder Contents // Getting all folder entities in array may be slow with large number of files. // Enumerating the folder entities would itterate the files one by one without blocking the UI. // ``` JavaScript var documents = fs.knownFolders.documents(); // var file = documents.getFile("Test.txt"); var file1 = documents.getFile("Test1.txt"); var fileFound, file1Found; var console = { log: function (file) { if (file === "Test.txt") { fileFound = true; } else if (file === "Test1.txt") { file1Found = true; } } } // documents.eachEntity(function (entity) { console.log(entity.name); }); // TKUnit.assert(fileFound, "Failed to enumerate Test.txt"); TKUnit.assert(file1Found, "Failed to enumerate Test1.txt"); file.remove(); file1.remove(); // // ``` // }; // TODO: testGetFile export var testGetFolder = function () { // // ### Getting Folder by Name // ``` JavaScript var documents = fs.knownFolders.documents(); var myFolder = documents.getFolder("Tests__"); // TKUnit.assert(myFolder, "Folder.getFolder API not working."); TKUnit.assert(fs.Folder.exists(myFolder.path), "Folder.getFolder API not working."); myFolder.remove(); // // ``` // }; export var testGetParent = function () { // // ### Getting Parent Folder // ``` JavaScript var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); // TKUnit.assert(file, "Failed to create file in the Documents folder."); // //// The parent folder of the file would be the documents folder. var parent = file.getParent(); // TKUnit.assert(documents == parent, "The parent folder should be the Documents folder."); file.remove(); // // ``` // }; export var testFileNameExtension = function () { // // ### Getting File Name and Extension // ``` JavaScript var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); //// Getting the file name "Test.txt". var fileName = file.name; //// Getting the file extension ".txt". var fileExtension = file.extension; // TKUnit.assert(fileName, "Test.txt"); TKUnit.assert(fileExtension, ".txt"); file.remove(); // // ``` // }; export var testFileExists = function () { // // ### Checking if a File Exists // ``` JavaScript var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); var exists = fs.File.exists(file.path); // TKUnit.assert(exists, "File.exists API not working."); exists = fs.File.exists(file.path + "_"); TKUnit.assert(!exists, "File.exists API not working."); file.remove(); // // ``` // }; export var testFolderExists = function () { // // ### Checking if a Folder Exists // ``` JavaScript var documents = fs.knownFolders.documents(); var exists = fs.Folder.exists(documents.path); // TKUnit.assert(exists, "Folder.exists API not working."); exists = fs.Folder.exists(documents.path + "_"); TKUnit.assert(!exists, "Folder.exists API not working."); // // ``` // }; export var testContainsFile = function () { var folder = fs.knownFolders.documents(); var file = folder.getFile("Test.txt"); var contains = folder.contains("Test.txt"); TKUnit.assert(contains, "Folder.contains API not working."); contains = folder.contains("Test_xxx.txt"); TKUnit.assert(!contains, "Folder.contains API not working."); file.remove(); }; // // ## Update // export var testFileRename = function () { // // ### Renaming a File // ``` JavaScript var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); file.rename("Test_renamed.txt") .then(function (result) { //// Successfully Renamed. // TKUnit.assert(file.name === "Test_renamed.txt", "File.rename API not working."); file.remove(); documents.getFile("Test.txt").remove(); // }) .fail(function (error) { //// Failed to rename the file. // console.error("Failed to rename file"); // }); // ``` // }; export var testFolderRename = function () { // // ### Renaming a Folder // ``` JavaScript var folder = fs.knownFolders.documents(); var myFolder = folder.getFolder("Test__"); myFolder.rename("Something") .then(function (result) { //// Successfully Renamed. // TKUnit.assert(myFolder.name === "Something", "Folder.rename API not working."); myFolder.remove(); folder.getFolder("Test__").remove(); // }) .fail(function (error) { //// Failed to rename the folder. // TKUnit.assert(false, "Folder.rename API not working."); // }); // ``` // }; // // ## Delete // export var testFileRemove = function () { // // ### Removing a File // To 'delete', 'remove' or 'unlink' a file use the file's remove method: // ``` JavaScript var documents = fs.knownFolders.documents(); var file = documents.getFile("Test.txt"); file.remove() .then(function (result) { //// Success removing the file. // TKUnit.assert(!fs.File.exists(file.path)); // }) .fail(function (error) { //// Failed to remove the file. // TKUnit.assert(false, "File.remove API not working."); // }); // ``` // }; export var testFolderClear = function () { // // ### Clearing the Contents of a Folder // The clear method removes all files within a folder. // ``` JavaScript var documents = fs.knownFolders.documents(); var folder = documents.getFolder("testFolderEmpty"); // var file1 = folder.getFile("Test1.txt"); var file2 = folder.getFile("Test2.txt"); var emptied; // folder.clear() .then(function () { //// Successfully cleared the folder. // emptied = true; // }) .fail(function (error) { //// Failed to clear the folder. // console.error(error.message); // }); // folder.getEntities() .then(function (entities) { TKUnit.assert(entities.length === 0, "Failed to clear a Folder"); folder.remove(); }); // // ``` // }; // TODO: Removing a folder. // misc export var testKnownFolderRename = function () { var folder = fs.knownFolders.documents(); folder.rename("Something") .then(function (result) { console.error("Known folders should not be renamed."); }) .fail(function (error) { TKUnit.assert(true); }); }; export var testPathNormalize = function () { var folder = fs.knownFolders.documents(); var testPath = "///test.txt"; var normalizedPath = fs.path.normalize(folder.path + testPath); var expected = folder.path + "/test.txt"; TKUnit.assert(normalizedPath === expected); }; export var testPathJoin = function () { var folder = fs.knownFolders.documents(); var path = fs.path.join(folder.path, "myFiles", "test.txt"); var expected = folder.path + "/myFiles/test.txt"; TKUnit.assert(path === expected); }; export var testPathSeparator = function () { var separator = fs.path.separator; var expected = "/"; TKUnit.assert(separator === expected); };