Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] Memory Adapter: Clone resource on write #259

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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