From 72f5846c8e2c289217f446445ad0f737908597bd Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Tue, 14 Jul 2020 11:37:04 +0200 Subject: [PATCH] [INTERNAL] Memory Adapter: Clone resource on write To create a behavior similar to the FS Adapter, ensuring that a resources content can not be changed after it has been written. --- lib/adapters/Memory.js | 57 +++++++++++++++---------------- test/lib/adapters/Memory_write.js | 4 +++ 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/lib/adapters/Memory.js b/lib/adapters/Memory.js index 2be1b95b..7462a8ee 100644 --- a/lib/adapters/Memory.js +++ b/lib/adapters/Memory.js @@ -115,40 +115,37 @@ class Memory extends AbstractAdapter { * @param {module:@ui5/fs.Resource} resource The Resource to write * @returns {Promise} Promise resolving once data has been written */ - _write(resource) { - return new Promise((resolve, reject) => { - const relPath = resource.getPath().substr(this._virBasePath.length); - log.verbose("Writing to virtual path %s", resource.getPath()); - this._virFiles[relPath] = resource; + async _write(resource) { + const relPath = resource.getPath().substr(this._virBasePath.length); + log.verbose("Writing to virtual path %s", resource.getPath()); + this._virFiles[relPath] = await resource.clone(); - // Add virtual directories for all path segments of the written resource - // TODO: Add tests for all this - const pathSegments = relPath.split("/"); - pathSegments.pop(); // Remove last segment representing the resource itself + // Add virtual directories for all path segments of the written resource + // TODO: Add tests for all this + const pathSegments = relPath.split("/"); + pathSegments.pop(); // Remove last segment representing the resource itself - pathSegments.forEach((segment, i) => { - if (i >= 1) { - segment = pathSegments[i - 1] + "/" + segment; - } - pathSegments[i] = segment; - }); - - for (let i = pathSegments.length - 1; i >= 0; i--) { - const segment = pathSegments[i]; - if (!this._virDirs[segment]) { - this._virDirs[segment] = new Resource({ - project: this.project, - statInfo: { // TODO: make closer to fs stat info - isDirectory: function() { - return true; - } - }, - path: this._virBasePath + segment - }); - } + pathSegments.forEach((segment, i) => { + if (i >= 1) { + segment = pathSegments[i - 1] + "/" + segment; } - resolve(); + pathSegments[i] = segment; }); + + for (let i = pathSegments.length - 1; i >= 0; i--) { + const segment = pathSegments[i]; + if (!this._virDirs[segment]) { + this._virDirs[segment] = new Resource({ + project: this.project, + statInfo: { // TODO: make closer to fs stat info + isDirectory: function() { + return true; + } + }, + path: this._virBasePath + segment + }); + } + } } } diff --git a/test/lib/adapters/Memory_write.js b/test/lib/adapters/Memory_write.js index e99c27a0..32bf1b70 100644 --- a/test/lib/adapters/Memory_write.js +++ b/test/lib/adapters/Memory_write.js @@ -13,6 +13,7 @@ test("glob resources from application.a w/ virtual base path prefix", async (t) .then(() => dest.byGlob("/app/*.html")) .then((resources) => { t.deepEqual(resources.length, 1, "Found exactly one resource"); + t.not(resources[0], res, "Not the same resource instance"); }); }); @@ -45,6 +46,9 @@ test("Write resource w/ virtual base path", async (t) => { "test.html": res }, "Adapter added resource with correct path"); + + t.not(readerWriter._virFiles["test.html"], res, "Not the same resource instance"); + t.deepEqual(Object.keys(readerWriter._virDirs), [], "Adapter added correct virtual directories"); });