From eb75c3f0c044f93ea56016a6b5c5f769a647729c Mon Sep 17 00:00:00 2001 From: "A. Diamond" Date: Mon, 9 Aug 2021 12:22:06 -0400 Subject: [PATCH] Fixed issue #476 --- core/util.js | 11 +++++++++++ core/util.test.js | 6 ++++++ ui/controllers/job_files_controller.js | 2 +- ui/controllers/job_files_controller.test.js | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/util.js b/core/util.js index e437b2fc4..ed4fddf39 100644 --- a/core/util.js +++ b/core/util.js @@ -609,6 +609,17 @@ class Util { return canWrite; } + /** + * Returns true if dirpath is a directory. + * + * @path {string} dirpath - The path to check + * + * @returns {boolean} + */ + static isDirectory(dirpath) { + return fs.existsSync(dirpath) && fs.lstatSync(dirpath).isDirectory() + } + /** * Returns true if dirpath is a non-empty directory. * diff --git a/core/util.test.js b/core/util.test.js index f4c471b09..e936a181c 100644 --- a/core/util.test.js +++ b/core/util.test.js @@ -334,6 +334,12 @@ test('Util.canWrite()', () => { expect(Util.canWrite(path.join(testDir, 'does-not-exist.txt'))).toBe(false); }); +test('Util.isDirectory()', () => { + expect(Util.isDirectory(__dirname)).toBe(true) + expect(Util.isDirectory(__dirname + 'zzzz8080')).toBe(false) + expect(Util.isDirectory('')).toBe(false) +}); + test('Util.isNonEmptyDirectory()', () => { expect(Util.isNonEmptyDirectory(__dirname)).toBe(true) expect(Util.isNonEmptyDirectory(__dirname + 'zzzz8080')).toBe(false) diff --git a/ui/controllers/job_files_controller.js b/ui/controllers/job_files_controller.js index 5c3c9c8bd..e9d5e0869 100644 --- a/ui/controllers/job_files_controller.js +++ b/ui/controllers/job_files_controller.js @@ -332,7 +332,7 @@ class JobFilesController extends BaseController { findContainingItem(filepath) { if (Array.isArray(this.job.packageOp.sourceFiles)) { for (let item of this.job.packageOp.sourceFiles) { - if (filepath.startsWith(item)) { + if (item === filepath || (filepath.startsWith(item) && Util.isDirectory(item))) { return item; } } diff --git a/ui/controllers/job_files_controller.test.js b/ui/controllers/job_files_controller.test.js index 40a1a2bf6..4e2754ae2 100644 --- a/ui/controllers/job_files_controller.test.js +++ b/ui/controllers/job_files_controller.test.js @@ -183,3 +183,19 @@ test('deleting a file removes it from the UI and job', done => { done(); }, timeout); }); + +// https://github.com/APTrust/dart/issues/476 +test('findContainingItem', () => { + let job = new Job() + job.packageOp = new PackageOperation('test', 'testy/test.tar') + job.packageOp.sourceFiles = [ + __dirname, + "D:/Baby Jan", + ] + let controller = new JobFilesController(new URLSearchParams()) + controller.job = job + + expect(controller.findContainingItem(path.join(__dirname, "photos", "image1"))).not.toBeNull() + expect(controller.findContainingItem("D:/Baby Jan")).not.toBeNull() + expect(controller.findContainingItem("D:/Baby Jan and Grandpa")).toBeNull() +});