Skip to content

Commit

Permalink
[INTERNAL] Resource#clone: Omit project association
Browse files Browse the repository at this point in the history
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
#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.
  • Loading branch information
RandomByte committed Dec 22, 2022
1 parent 28dcdd6 commit 957cdfb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
4 changes: 0 additions & 4 deletions lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ class Resource {
options.buffer = this._buffer;
}

if (this.__project) {
options.project = this.__project;
}

return options;
}

Expand Down
15 changes: 12 additions & 3 deletions lib/adapters/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,20 @@ class Memory extends AbstractAdapter {
const matchedPaths = micromatch(resourcePaths, patterns, {
dot: true
});
return Promise.all(matchedPaths.map((virPath) => {
return resourceMap[virPath] && resourceMap[virPath].clone();
return await Promise.all(matchedPaths.map((virPath) => {
const resource = resourceMap[virPath];
if (resource) {
return this._cloneResource(resource);
}
}));
}

async _cloneResource(resource) {
const clonedResource = await resource.clone();
clonedResource.setProject(this._project);
return clonedResource;
}

/**
* Locate resources by glob.
*
Expand Down Expand Up @@ -109,7 +118,7 @@ class Memory extends AbstractAdapter {
if (!resource || (options.nodir && resource.getStatInfo().isDirectory())) {
return null;
} else {
return await resource.clone();
return await this._cloneResource(resource);
}
}

Expand Down
10 changes: 2 additions & 8 deletions test/lib/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,13 @@ test("Resource: clone resource with source", async (t) => {
t.deepEqual(clonedResource2.getSource(), resource.getSource());
});

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

const myProject = {
name: "my project"
};

const resourceOptions = {
path: "my/path/to/resource",
project: myProject
};

const resource = new Resource({
path: "my/path/to/resource",
project: myProject
Expand All @@ -362,8 +357,7 @@ test("Resource: clone resource with project", async (t) => {
t.pass("Resource cloned");

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

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

0 comments on commit 957cdfb

Please sign in to comment.