Skip to content

Commit cde91d3

Browse files
RandomByted3xter666
authored andcommitted
[fs][INTERNAL] Resource#clone: Omit project association
When cloning a Resource, the new Resource instance should not be associated with any project. This allows for use cases where a resource is copied from one project to another. The problematic behavior was introduced with SAP/ui5-fs#448 and motivated by the need of the memory adapter to clone resources while keeping the project association. However, since memory adapters are always assigned a project, they can easily just assign that project to the cloned resource. This has been implemented in this PR.
1 parent f01019e commit cde91d3

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

packages/fs/lib/Resource.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,6 @@ class Resource {
325325
options.buffer = this._buffer;
326326
}
327327

328-
if (this.__project) {
329-
options.project = this.__project;
330-
}
331-
332328
return options;
333329
}
334330

packages/fs/lib/adapters/Memory.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,22 @@ class Memory extends AbstractAdapter {
4040
const matchedPaths = micromatch(resourcePaths, patterns, {
4141
dot: true
4242
});
43-
return Promise.all(matchedPaths.map((virPath) => {
44-
return resourceMap[virPath] && resourceMap[virPath].clone();
43+
return await Promise.all(matchedPaths.map((virPath) => {
44+
const resource = resourceMap[virPath];
45+
if (resource) {
46+
return this._cloneResource(resource);
47+
}
4548
}));
4649
}
4750

51+
async _cloneResource(resource) {
52+
const clonedResource = await resource.clone();
53+
if (this._project) {
54+
clonedResource.setProject(this._project);
55+
}
56+
return clonedResource;
57+
}
58+
4859
/**
4960
* Locate resources by glob.
5061
*
@@ -109,7 +120,7 @@ class Memory extends AbstractAdapter {
109120
if (!resource || (options.nodir && resource.getStatInfo().isDirectory())) {
110121
return null;
111122
} else {
112-
return await resource.clone();
123+
return await this._cloneResource(resource);
113124
}
114125
}
115126

packages/fs/test/lib/Resource.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,13 @@ test("Resource: clone resource with source", async (t) => {
341341
t.deepEqual(clonedResource2.getSource(), resource.getSource());
342342
});
343343

344-
test("Resource: clone resource with project", async (t) => {
344+
test("Resource: clone resource with project removes project", async (t) => {
345345
t.plan(2);
346346

347347
const myProject = {
348348
name: "my project"
349349
};
350350

351-
const resourceOptions = {
352-
path: "my/path/to/resource",
353-
project: myProject
354-
};
355-
356351
const resource = new Resource({
357352
path: "my/path/to/resource",
358353
project: myProject
@@ -362,8 +357,7 @@ test("Resource: clone resource with project", async (t) => {
362357
t.pass("Resource cloned");
363358

364359
const clonedResourceProject = await clonedResource.getProject();
365-
t.is(clonedResourceProject, resourceOptions.project, "Cloned resource should have same " +
366-
"project reference as the original resource");
360+
t.falsy(clonedResourceProject, "Cloned resource should not have a project");
367361
});
368362

369363
test("Resource: create resource with modified source", (t) => {

0 commit comments

Comments
 (0)