From 876f88b6df599b3bfaf7a822c0320cad93d5bc68 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Wed, 14 Dec 2022 17:13:23 +0100 Subject: [PATCH 1/4] [BREAKING] Throw an error on write of a resource when path does not match virBasePath of the respective adapter --- lib/adapters/AbstractAdapter.js | 4 ++++ test/lib/resources.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/adapters/AbstractAdapter.js b/lib/adapters/AbstractAdapter.js index 153838ba..7d20324d 100644 --- a/lib/adapters/AbstractAdapter.js +++ b/lib/adapters/AbstractAdapter.js @@ -209,6 +209,10 @@ class AbstractAdapter extends AbstractReaderWriter { } _write(resource) { + if (!resource.getPath().startsWith(this._virBasePath)) { + throw new Error("The path of the resource does not starts with the virBasePath of the adapter"); + } + if (this._project) { // Assign project to resource if necessary if (resource.hasProject()) { diff --git a/test/lib/resources.js b/test/lib/resources.js index e448c762..3a5f27e9 100644 --- a/test/lib/resources.js +++ b/test/lib/resources.js @@ -129,6 +129,22 @@ for (const adapter of adapters) { t.not(resourceOldPath1); }); + test(adapter + ": Create resource with mismatch paths", 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 does not starts with the virBasePath of the adapter"); + }); + test(adapter + ": Filter resources", async (t) => { const source = createAdapter({ fsBasePath: "./test/fixtures/application.a/webapp", From c45c46e0449b17a8e257c7d11ccd2978e2e80138 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Thu, 15 Dec 2022 12:59:06 +0100 Subject: [PATCH 2/4] [INTERNAL] Add unit test and refactor error message --- lib/adapters/AbstractAdapter.js | 3 ++- test/lib/adapters/AbstractAdapter.js | 20 ++++++++++++++++++++ test/lib/resources.js | 5 +++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/adapters/AbstractAdapter.js b/lib/adapters/AbstractAdapter.js index 7d20324d..c0081a5a 100644 --- a/lib/adapters/AbstractAdapter.js +++ b/lib/adapters/AbstractAdapter.js @@ -210,7 +210,8 @@ class AbstractAdapter extends AbstractReaderWriter { _write(resource) { if (!resource.getPath().startsWith(this._virBasePath)) { - throw new Error("The path of the resource does not starts with the virBasePath of the adapter"); + 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) { diff --git a/test/lib/adapters/AbstractAdapter.js b/test/lib/adapters/AbstractAdapter.js index d0c4713e..970e6053 100644 --- a/test/lib/adapters/AbstractAdapter.js +++ b/test/lib/adapters/AbstractAdapter.js @@ -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/'"); +}); diff --git a/test/lib/resources.js b/test/lib/resources.js index 3a5f27e9..1b37be49 100644 --- a/test/lib/resources.js +++ b/test/lib/resources.js @@ -129,7 +129,7 @@ for (const adapter of adapters) { t.not(resourceOldPath1); }); - test(adapter + ": Create resource with mismatch paths", async (t) => { + 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/", @@ -142,7 +142,8 @@ for (const adapter of adapters) { }); const error = await t.throwsAsync(dest.write(resource)); - t.is(error.message, "The path of the resource does not starts with the virBasePath of the adapter"); + 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) => { From f44d050713eadf0ade3bfd932db44ca20c3e6913 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Thu, 15 Dec 2022 13:18:22 +0100 Subject: [PATCH 3/4] [INTERNAL] Adjust directory paths in tests --- test/lib/resources.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/test/lib/resources.js b/test/lib/resources.js index 1b37be49..76292340 100644 --- a/test/lib/resources.js +++ b/test/lib/resources.js @@ -70,7 +70,7 @@ for (const adapter of adapters) { }); const resource = createResource({ - path: "/dest/writer/test.js", + path: "/dest/writer/content/test.js", string: "MyInitialContent" }); @@ -78,7 +78,7 @@ for (const adapter of adapters) { 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"); @@ -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"); }); @@ -100,32 +100,32 @@ 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); }); From a3cc081f616a796c8b0b12d17a4e3c2d0d6b9960 Mon Sep 17 00:00:00 2001 From: Florian Vogt Date: Fri, 16 Dec 2022 11:10:53 +0100 Subject: [PATCH 4/4] [INTERNAL] AbstractAdapter: Use string templates --- lib/adapters/AbstractAdapter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/adapters/AbstractAdapter.js b/lib/adapters/AbstractAdapter.js index c0081a5a..7061a817 100644 --- a/lib/adapters/AbstractAdapter.js +++ b/lib/adapters/AbstractAdapter.js @@ -210,8 +210,8 @@ 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 + "'"); + 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) {