Skip to content

Commit

Permalink
[BREAKING] Throw an error on write of a resource when path does not s…
Browse files Browse the repository at this point in the history
…tarts with virBasePath of the respective adapter (#453)

BREAKING CHANGE:
An error is thrown when a resource shall be written to an adapter where the path of the resource does not starts with the virtual base path defined in the adapter.
  • Loading branch information
flovogt committed Dec 16, 2022
1 parent 3454bc1 commit d76575f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 13 deletions.
5 changes: 5 additions & 0 deletions lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ class AbstractAdapter extends AbstractReaderWriter {
}

_write(resource) {
if (!resource.getPath().startsWith(this._virBasePath)) {
throw new Error(`The path of the resource '${resource.getPath()}' does not starts with ` +
`the configured virtual base path of the adapter '${this._virBasePath}'`);
}

if (this._project) {
// Assign project to resource if necessary
if (resource.hasProject()) {
Expand Down
20 changes: 20 additions & 0 deletions test/lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,23 @@ test("Write resource with another project than provided in the adapter", (t) =>
t.is(error.message,
"Unable to write resource associated with project test.lib into adapter of project test.lib1: /test.js");
});

test("Create a resource with a path not starting with path configured in the adapter", (t) => {
const resource = createResource({
path: "/dest2/tmp/test.js",
string: "MyContent"
});

const writer = new MyAbstractAdapter({
virBasePath: "/dest2/writer/",
project: {
getName: () => "test.lib1",
getVersion: () => "2.0.0"
}
});

const error = t.throws(() => writer._write(resource));
t.is(error.message,
"The path of the resource '/dest2/tmp/test.js' does not starts with the configured " +
"virtual base path of the adapter '/dest2/writer/'");
});
43 changes: 30 additions & 13 deletions test/lib/resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ for (const adapter of adapters) {
});

const resource = createResource({
path: "/dest/writer/test.js",
path: "/dest/writer/content/test.js",
string: "MyInitialContent"
});

await dest.write(resource);

resource.setString("MyNewContent");

const resource1 = await dest.byPath("/dest/writer/test.js");
const resource1 = await dest.byPath("/dest/writer/content/test.js");

t.is(await resource.getString(), "MyNewContent");
t.is(await resource1.getString(), "MyInitialContent");
Expand All @@ -88,7 +88,7 @@ for (const adapter of adapters) {

await dest.write(resource);

const resource2 = await dest.byPath("/dest/writer/test.js");
const resource2 = await dest.byPath("/dest/writer/content/test.js");
t.is(await resource.getString(), "MyNewContent");
t.is(await resource2.getString(), "MyNewContent");
});
Expand All @@ -100,35 +100,52 @@ for (const adapter of adapters) {
});

const resource = createResource({
path: "/dest/writer/test.js",
path: "/dest/writer/path/test.js",
string: "MyInitialContent"
});

await dest.write(resource);

resource.setPath("/dest/writer/test2.js");
resource.setPath("/dest/writer/path/test2.js");

const resourceOldPath = await dest.byPath("/dest/writer/test.js");
const resourceNewPath = await dest.byPath("/dest/writer/test2.js");
const resourceOldPath = await dest.byPath("/dest/writer/path/test.js");
const resourceNewPath = await dest.byPath("/dest/writer/path/test2.js");

t.is(await resource.getPath(), "/dest/writer/test2.js");
t.is(await resource.getPath(), "/dest/writer/path/test2.js");
t.truthy(resourceOldPath);
t.is(await resourceOldPath.getString(), await resource.getString());
t.is(await resourceOldPath.getPath(), "/dest/writer/test.js");
t.is(await resourceOldPath.getPath(), "/dest/writer/path/test.js");
t.not(resourceNewPath);

await dest.write(resource);

const resourceOldPath1 = await dest.byPath("/dest/writer/test.js");
const resourceNewPath1 = await dest.byPath("/dest/writer/test2.js");
const resourceOldPath1 = await dest.byPath("/dest/writer/path/test.js");
const resourceNewPath1 = await dest.byPath("/dest/writer/path/test2.js");

t.is(await resource.getPath(), "/dest/writer/test2.js");
t.is(await resource.getPath(), "/dest/writer/path/test2.js");
t.truthy(resourceNewPath1);
t.is(await resourceNewPath1.getString(), await resource.getString());
t.is(await resourceNewPath1.getPath(), "/dest/writer/test2.js");
t.is(await resourceNewPath1.getPath(), "/dest/writer/path/test2.js");
t.not(resourceOldPath1);
});

test(adapter + ": Create a resource with a path not starting with path configured in the adapter", async (t) => {
t.pass(2);
const dest = await getAdapter({
fsBasePath: "./test/tmp/writer/",
virBasePath: "/dest2/writer/"
});

const resource = createResource({
path: "/dest2/tmp/test.js",
string: "MyContent"
});

const error = await t.throwsAsync(dest.write(resource));
t.is(error.message, "The path of the resource '/dest2/tmp/test.js' does not starts with the configured " +
"virtual base path of the adapter '/dest2/writer/'");
});

test(adapter + ": Filter resources", async (t) => {
const source = createAdapter({
fsBasePath: "./test/fixtures/application.a/webapp",
Expand Down

0 comments on commit d76575f

Please sign in to comment.