Skip to content

Commit

Permalink
[INTERNAL] Memory Adapter: Clone resource on write
Browse files Browse the repository at this point in the history
To create a behavior similar to the FS Adapter, ensuring that a
resources content can not be changed after it has been written.
  • Loading branch information
RandomByte committed Jul 14, 2020
1 parent 9d2cc6c commit 72f5846
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
57 changes: 27 additions & 30 deletions lib/adapters/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,40 +115,37 @@ class Memory extends AbstractAdapter {
* @param {module:@ui5/fs.Resource} resource The Resource to write
* @returns {Promise<undefined>} 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
});
}
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions test/lib/adapters/Memory_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});

Expand Down Expand Up @@ -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");
});

Expand Down

0 comments on commit 72f5846

Please sign in to comment.