Skip to content

Commit

Permalink
[FIX] Allow resource migration (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
flovogt committed Oct 24, 2022
1 parent 1060810 commit 1722d71
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 14 deletions.
47 changes: 34 additions & 13 deletions lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,44 @@ class AbstractAdapter extends AbstractReaderWriter {
return new Resource(parameters);
}

_write(resource) {
if (!this._project) {
return;
async _migrateResource(resource) {
// Check if its a fs/Resource v3, function 'hasProject' was
// introduced with v3 therefore take it as the indicator
if (resource.hasProject) {
return resource;
}

const options = {
path: resource._path,
statInfo: resource._statInfo,
source: resource._source
};

if (resource._stream) {
options.buffer = await resource._getBufferFromStream();
} else if (resource._createStream) {
options.createStream = resource._createStream;
} else if (resource._buffer) {
options.buffer = resource._buffer;
}
return new Resource(options);
}

// Assign project to resource if necessary
if (resource.hasProject()) {
if (resource.getProject() !== this._project) {
throw new Error(
`Unable to write resource associated with project ` +
`${resource.getProject().getName()} into adapter of project ${this._project.getName()}: ` +
resource.getPath());
_write(resource) {
if (this._project) {
// Assign project to resource if necessary
if (resource.hasProject()) {
if (resource.getProject() !== this._project) {
throw new Error(
`Unable to write resource associated with project ` +
`${resource.getProject().getName()} into adapter of project ${this._project.getName()}: ` +
resource.getPath());
}
return;
}
return;
log.silly(`Associating resource ${resource.getPath()} with project ${this._project.getName()}`);
resource.setProject(this._project);
}
log.silly(`Associating resource ${resource.getPath()} with project ${this._project.getName()}`);
resource.setProject(this._project);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/adapters/FileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ class FileSystem extends AbstractAdapter {
* @returns {Promise<undefined>} Promise resolving once data has been written
*/
async _write(resource, {drain, readOnly}) {
resource = await this._migrateResource(resource);
super._write(resource);
if (drain && readOnly) {
throw new Error(`Error while writing resource ${resource.getPath()}: ` +
Expand Down
3 changes: 2 additions & 1 deletion lib/adapters/Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class Memory extends AbstractAdapter {
* @param {@ui5/fs/Resource} resource The Resource to write
* @returns {Promise<undefined>} Promise resolving once data has been written
*/
_write(resource) {
async _write(resource) {
resource = await this._migrateResource(resource);
super._write(resource);
return new Promise((resolve, reject) => {
const relPath = resource.getPath().substr(this._virBasePath.length);
Expand Down
18 changes: 18 additions & 0 deletions test/lib/adapters/AbstractAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from "ava";
import AbstractAdapter from "../../../lib/adapters/AbstractAdapter.js";

class MyAbstractAdapter extends AbstractAdapter {}

test("_migrateResource", async (t) => {
const resource = {
_path: "/test.js"
};

const writer = new MyAbstractAdapter({
virBasePath: "/"
});

const migratedResource = await writer._migrateResource(resource);

t.is(migratedResource.getPath(), "/test.js");
});
19 changes: 19 additions & 0 deletions test/lib/adapters/FileSystem_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import chai from "chai";
import chaifs from "chai-fs";
chai.use(chaifs);
const assert = chai.assert;
import sinon from "sinon";

import {createAdapter} from "../../../lib/resourceFactory.js";

Expand Down Expand Up @@ -129,3 +130,21 @@ test("Writing with readOnly and drain options set should fail", async (t) => {
"Do not use options 'drain' and 'readOnly' at the same time."
});
});

test("Migration of resource is executed", async (t) => {
const readerWriters = t.context.readerWriters;
const migrateResourceWriterSpy = sinon.spy(readerWriters.dest, "_migrateResource");
const destFsPath = path.join(t.context.tmpDirPath, "index.html");

// Get resource from one readerWriter
const resource = await readerWriters.source.byPath("/app/index.html");

// Write resource content to another readerWriter
await readerWriters.dest.write(resource);

t.is(migrateResourceWriterSpy.callCount, 1);
t.notThrows(() => {
assert.fileEqual(destFsPath, "./test/fixtures/application.a/webapp/index.html");
});
await t.notThrowsAsync(resource.getBuffer(), "Resource content can still be accessed");
});
15 changes: 15 additions & 0 deletions test/lib/adapters/Memory_write.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from "ava";
import {createAdapter, createResource} from "../../../lib/resourceFactory.js";
import sinon from "sinon";

test("glob resources from application.a w/ virtual base path prefix", async (t) => {
const dest = createAdapter({
Expand Down Expand Up @@ -118,3 +119,17 @@ test("Write resource w/ crazy virtual base path", async (t) => {
"one\\"
], "Adapter added correct virtual directories");
});

test("Migration of resource is executed", async (t) => {
const writer = createAdapter({
virBasePath: "/"
});

const resource = createResource({
path: "/test.js"
});

const migrateResourceWriterSpy = sinon.spy(writer, "_migrateResource");
await writer.write(resource);
t.is(migrateResourceWriterSpy.callCount, 1);
});

0 comments on commit 1722d71

Please sign in to comment.