diff --git a/README.md b/README.md index 3a2c701..ea37cdc 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,12 @@ You can restrict mimeType or require a unique (single) file in the searcher para `drive.x.checkSearch` on the search results. Successful searches are passed to the next `then()` and searches with missing files or duplicates throw errors. (see `drive.x.findPath` above for a descrption of these Boom errors and how to catch them). -The parent folder can be specified from a drive.x.findPath like this: +`drive.x.searcher` tests all returned files/folders mimeTypes against the Google Drive Folder mimeType 'application/vnd.google-apps.folder' and sets +`.isFolder` to `true` or `false` for each file/folder in `files` appropriately. + +You can also use `isFolder:true` or `isFolder:false` as a search term to limit what is returned. If `isFolder` is unspecified, a search can return a mix of files and folders. + +The parent folder can be specified from an earlier promise, such as `drive.x.findPath` like this: Finds the folder "/crime/sprees/murder" and looks for any files in this folder that are .png files, then calls imaginary functions `notGuilty()` or `guilty()`. Here `files` is an array so `files.length` is the number of files found. diff --git a/index.js b/index.js index f1d616c..6ee9cbf 100644 --- a/index.js +++ b/index.js @@ -85,6 +85,10 @@ function extensions(drive, request, rootFolderId, spaces){ return new Promise(function(resolve, reject){ drive.files.list(params, function(err, resp){ if (err) return reject(err); + // add isFolder boolean property to files, comparing mimeType to the Google Drive folder mimeType + if ((resp.files) && (resp.files.length)) + for(var i=0,l=resp.files.length; i{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.mimeType===folderMimeType) && (f.name==='path') && f.isFolder)), "info.files contains folder 'path'"); + }) + ); + }); + + it("searching root for folders should yield folder 'path' with .isFolder===true", function(){ + return (drive.x.searcher({ + trashed: false, + isFolder: true + })('root').then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.mimeType===folderMimeType) && (f.name==='path') && f.isFolder )), "info.files contains folder 'path'"); + }) + ); + }); + + it("searching root for non-folders should be empty ", function(){ + return (drive.x.searcher({ + trashed: false, + isFolder: false + })('root').then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.length===0, "info.files should be empty"); + }) + ); + }); + + it("searching all folders for any non-trashed file should be non-empty and include file README.md in results ", function(){ + return (drive.x.searcher({ + trashed: false, + isFolder: false + })().then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.name==='README.md') && (!f.isFolder)))); + assert.ok(info.files.length>0, "info.files should be non-empty"); + }) + ); + }); + it("checking existence with drive.x.findPath should yield expected file metadata", function(){ return drive.x.findPath("/path/to/test/Files/README.md").then((info)=>{ info.should.have.properties('id','name','mimeType'); diff --git a/testDoc.md b/testDoc.md index 6d59b13..682b7b9 100644 --- a/testDoc.md +++ b/testDoc.md @@ -111,6 +111,57 @@ uploadResult.should.be.type("object"); ## after drive.x.upload2 +searching root for anything should yield folder 'path' with .isFolder===true. + +```js +return (drive.x.searcher({trashed:false})('root') + .then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.mimeType===folderMimeType) && (f.name==='path') && f.isFolder)), "info.files contains folder 'path'"); + }) + ); +``` + +searching root for folders should yield folder 'path' with .isFolder===true. + +```js +return (drive.x.searcher({ + trashed: false, + isFolder: true + })('root').then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.mimeType===folderMimeType) && (f.name==='path') && f.isFolder )), "info.files contains folder 'path'"); + }) + ); +``` + +searching root for non-folders should be empty . + +```js +return (drive.x.searcher({ + trashed: false, + isFolder: false + })('root').then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.length===0, "info.files should be empty"); + }) + ); +``` + +searching all folders for any non-trashed file should be non-empty and include file README.md in results . + +```js +return (drive.x.searcher({ + trashed: false, + isFolder: false + })().then((info)=>{ + assert.ok(Array.isArray(info.files), "info.files is array"); + assert.ok(info.files.some((f)=>((f.name==='README.md') && (!f.isFolder)))); + assert.ok(info.files.length>0, "info.files should be non-empty"); + }) + ); +``` + checking existence with drive.x.findPath should yield expected file metadata. ```js diff --git a/testResults.txt b/testResults.txt index da65d7a..498942a 100644 --- a/testResults.txt +++ b/testResults.txt @@ -6,9 +6,9 @@ ✓ drive should not be undefined ✓ drive.x should be an object drive.x.aboutMe - ✓ should return the test users email address (465ms) - ✓ should return a storageQuota object with properties limit, usage (180ms) - ✓ drive.about.get still works, as well, and the outputs match (207ms) + ✓ should return the test users email address (424ms) + ✓ should return a storageQuota object with properties limit, usage (168ms) + ✓ drive.about.get still works, as well, and the outputs match (173ms) drive.x.appDataFolder.upload2: upload a string to appDataFolder ✓ uploading the string to appDataFolder file myaccount should resolve with expected file metadata ✓ drive.x.appDataFolder.searcher should report there is exactly one myaccount file in the folder and it should match upload file id @@ -16,10 +16,14 @@ drive.x.upload2: upload a file README.md to Drive folder /path/to/test/Files ✓ uploading the README.md file to /path/to/test/Files/README.md should resolve with expected file metadata after drive.x.upload2 - ✓ checking existence with drive.x.findPath should yield expected file metadata (1093ms) - ✓ checking existence on wrong path should throw Boom.notfound (186ms) - ✓ downloading content with drive.x.download should yield contents string including 'License: MIT' (1328ms) - ✓ drive.x.upload2 uploading the file again with {clobber:false} will throw Boom.conflict error because file already exists (1077ms) + ✓ searching root for anything should yield folder 'path' with .isFolder===true (224ms) + ✓ searching root for folders should yield folder 'path' with .isFolder===true (210ms) + ✓ searching root for non-folders should be empty (200ms) + ✓ searching all folders for any non-trashed file should be non-empty and include file README.md in results (197ms) + ✓ checking existence with drive.x.findPath should yield expected file metadata (1117ms) + ✓ checking existence on wrong path should throw Boom.notfound (236ms) + ✓ downloading content with drive.x.download should yield contents string including 'License: MIT' (1286ms) + ✓ drive.x.upload2 uploading the file again with {clobber:false} will throw Boom.conflict error because file already exists (977ms) drive.x.upload2: upload test/test.zip to Drive folder /path/to/test/Files ✓ uploading the test.zip file to /path/to/test/Files/test.zip should resolve with expected file metadata and md5 match create folder /path/to/test2 @@ -30,12 +34,12 @@ use folderId of /path/to/test2 to upload test.zip ✓ uploading the test.zip file to /path/to/test2/test.zip should resolve with expected file metadata and md5 match cleanup via drive.x.janitor - ✓ janitor hopefully deletes the README.md file(s) OK and resolves correctly (1443ms) - ✓ drive.x.findPath will throw Boom.notFound if the file was successfully deleted (1010ms) - ✓ janitor will throw an error if told to delete an invalid file (144ms) + ✓ janitor hopefully deletes the README.md file(s) OK and resolves correctly (1374ms) + ✓ drive.x.findPath will throw Boom.notFound if the file was successfully deleted (1051ms) + ✓ janitor will throw an error if told to delete an invalid file (136ms) ✓ janitor should not throw an error if given an empty filelist - ✓ final cleanup: delete the path folder and check non-existence (1282ms) + ✓ final cleanup: delete the path folder and check non-existence (2839ms) - 25 passing (18s) + 29 passing (21s)