1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Tweaks to make sure Nextcloud driver passes all test units

This commit is contained in:
Laurent Cozic 2018-01-25 21:15:58 +00:00
parent 7ab135c099
commit 8f3fdb3afe
11 changed files with 89 additions and 24 deletions

View File

@ -304,7 +304,7 @@ describe('Synchronizer', function() {
expect(items.length).toBe(1); expect(items.length).toBe(1);
let deletedItems = await BaseItem.deletedItems(syncTargetId()); let deletedItems = await BaseItem.deletedItems(syncTargetId());
expect(deletedItems.length).toBe(0); expect(deletedItems.length).toBe(0);
})); }));
it('should delete remote folder', asyncTest(async () => { it('should delete remote folder', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" }); let folder1 = await Folder.save({ title: "folder1" });
@ -322,8 +322,8 @@ describe('Synchronizer', function() {
await synchronizer().start(); await synchronizer().start();
let all = await allItems(); let all = await allItems();
localItemsSameAsRemote(all, expect); await localItemsSameAsRemote(all, expect);
})); }));
it('should delete local folder', asyncTest(async () => { it('should delete local folder', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" }); let folder1 = await Folder.save({ title: "folder1" });
@ -345,8 +345,8 @@ describe('Synchronizer', function() {
await synchronizer().start(); await synchronizer().start();
let items = await allItems(); let items = await allItems();
localItemsSameAsRemote(items, expect); await localItemsSameAsRemote(items, expect);
})); }));
it('should resolve conflict if remote folder has been deleted, but note has been added to folder locally', asyncTest(async () => { it('should resolve conflict if remote folder has been deleted, but note has been added to folder locally', asyncTest(async () => {
let folder1 = await Folder.save({ title: "folder1" }); let folder1 = await Folder.save({ title: "folder1" });
@ -388,8 +388,8 @@ describe('Synchronizer', function() {
expect(items.length).toBe(1); expect(items.length).toBe(1);
expect(items[0].title).toBe('folder'); expect(items[0].title).toBe('folder');
localItemsSameAsRemote(items, expect); await localItemsSameAsRemote(items, expect);
})); }));
it('should cross delete all folders', asyncTest(async () => { it('should cross delete all folders', asyncTest(async () => {
// If client1 and 2 have two folders, client 1 deletes item 1 and client // If client1 and 2 have two folders, client 1 deletes item 1 and client

View File

@ -15,6 +15,7 @@ const { Synchronizer } = require('lib/synchronizer.js');
const { FileApi } = require('lib/file-api.js'); const { FileApi } = require('lib/file-api.js');
const { FileApiDriverMemory } = require('lib/file-api-driver-memory.js'); const { FileApiDriverMemory } = require('lib/file-api-driver-memory.js');
const { FileApiDriverLocal } = require('lib/file-api-driver-local.js'); const { FileApiDriverLocal } = require('lib/file-api-driver-local.js');
const { FileApiDriverWebDav } = require('lib/file-api-driver-webdav.js');
const { FsDriverNode } = require('lib/fs-driver-node.js'); const { FsDriverNode } = require('lib/fs-driver-node.js');
const { time } = require('lib/time-utils.js'); const { time } = require('lib/time-utils.js');
const { shimInit } = require('lib/shim-init-node.js'); const { shimInit } = require('lib/shim-init-node.js');
@ -22,8 +23,10 @@ const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
const SyncTargetMemory = require('lib/SyncTargetMemory.js'); const SyncTargetMemory = require('lib/SyncTargetMemory.js');
const SyncTargetFilesystem = require('lib/SyncTargetFilesystem.js'); const SyncTargetFilesystem = require('lib/SyncTargetFilesystem.js');
const SyncTargetOneDrive = require('lib/SyncTargetOneDrive.js'); const SyncTargetOneDrive = require('lib/SyncTargetOneDrive.js');
const SyncTargetNextcloud = require('lib/SyncTargetNextcloud.js');
const EncryptionService = require('lib/services/EncryptionService.js'); const EncryptionService = require('lib/services/EncryptionService.js');
const DecryptionWorker = require('lib/services/DecryptionWorker.js'); const DecryptionWorker = require('lib/services/DecryptionWorker.js');
const WebDavApi = require('lib/WebDavApi');
let databases_ = []; let databases_ = [];
let synchronizers_ = []; let synchronizers_ = [];
@ -46,13 +49,17 @@ fs.mkdirpSync(logDir, 0o755);
SyncTargetRegistry.addClass(SyncTargetMemory); SyncTargetRegistry.addClass(SyncTargetMemory);
SyncTargetRegistry.addClass(SyncTargetFilesystem); SyncTargetRegistry.addClass(SyncTargetFilesystem);
SyncTargetRegistry.addClass(SyncTargetOneDrive); SyncTargetRegistry.addClass(SyncTargetOneDrive);
SyncTargetRegistry.addClass(SyncTargetNextcloud);
//const syncTargetId_ = SyncTargetRegistry.nameToId('memory'); //const syncTargetId_ = SyncTargetRegistry.nameToId('nextcloud');
const syncTargetId_ = SyncTargetRegistry.nameToId('filesystem'); const syncTargetId_ = SyncTargetRegistry.nameToId('memory');
//const syncTargetId_ = SyncTargetRegistry.nameToId('filesystem');
const syncDir = __dirname + '/../tests/sync'; const syncDir = __dirname + '/../tests/sync';
const sleepTime = syncTargetId_ == SyncTargetRegistry.nameToId('filesystem') ? 1001 : 400; const sleepTime = syncTargetId_ == SyncTargetRegistry.nameToId('filesystem') ? 1001 : 400;
console.info('Testing with sync target: ' + SyncTargetRegistry.idToName(syncTargetId_));
const logger = new Logger(); const logger = new Logger();
logger.addTarget('console'); logger.addTarget('console');
logger.addTarget('file', { path: logDir + '/log.txt' }); logger.addTarget('file', { path: logDir + '/log.txt' });
@ -174,12 +181,14 @@ async function setupDatabaseAndSynchronizer(id = null) {
decryptionWorkers_[id] = new DecryptionWorker(); decryptionWorkers_[id] = new DecryptionWorker();
decryptionWorkers_[id].setEncryptionService(encryptionServices_[id]); decryptionWorkers_[id].setEncryptionService(encryptionServices_[id]);
if (syncTargetId_ == SyncTargetRegistry.nameToId('filesystem')) { // if (syncTargetId_ == SyncTargetRegistry.nameToId('filesystem')) {
fs.removeSync(syncDir) // fs.removeSync(syncDir)
fs.mkdirpSync(syncDir, 0o755); // fs.mkdirpSync(syncDir, 0o755);
} else { // } else {
await fileApi().format(); await fileApi().clearRoot();
}
//await fileApi().format();
//}
} }
function db(id = null) { function db(id = null) {
@ -230,7 +239,17 @@ function fileApi() {
fileApi_ = new FileApi(syncDir, new FileApiDriverLocal()); fileApi_ = new FileApi(syncDir, new FileApiDriverLocal());
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('memory')) { } else if (syncTargetId_ == SyncTargetRegistry.nameToId('memory')) {
fileApi_ = new FileApi('/root', new FileApiDriverMemory()); fileApi_ = new FileApi('/root', new FileApiDriverMemory());
} else if (syncTargetId_ == SyncTargetRegistry.nameToId('nextcloud')) {
const options = {
baseUrl: () => 'http://nextcloud.local/remote.php/dav/files/admin/JoplinTest',
username: () => 'admin',
password: () => '123456',
};
const api = new WebDavApi(options);
fileApi_ = new FileApi('', new FileApiDriverWebDav(api));
} }
// } else if (syncTargetId == Setting.SYNC_TARGET_ONEDRIVE) { // } else if (syncTargetId == Setting.SYNC_TARGET_ONEDRIVE) {
// let auth = require('./onedrive-auth.json'); // let auth = require('./onedrive-auth.json');
// if (!auth) { // if (!auth) {

View File

@ -31,6 +31,10 @@ class SyncTargetRegistry {
throw new Error('ID not found: ' + id); throw new Error('ID not found: ' + id);
} }
static idToName(id) {
return this.idToMetadata(id).name;
}
static idAndLabelPlainObject() { static idAndLabelPlainObject() {
let output = {}; let output = {};
for (let n in this.reg_) { for (let n in this.reg_) {

View File

@ -195,7 +195,7 @@ class WebDavApi {
if (json['d:error']) { if (json['d:error']) {
const code = json['d:error']['s:exception'] ? json['d:error']['s:exception'].join(' ') : response.status; const code = json['d:error']['s:exception'] ? json['d:error']['s:exception'].join(' ') : response.status;
const message = json['d:error']['s:message'] ? json['d:error']['s:message'].join("\n") : shortResponseText(); const message = json['d:error']['s:message'] ? json['d:error']['s:message'].join("\n") : shortResponseText();
throw new JoplinError(message + ' (' + code + ')', response.status); throw new JoplinError(method + ' ' + path + ': ' + message + ' (' + code + ')', response.status);
} }
throw new JoplinError(shortResponseText(), response.status); throw new JoplinError(shortResponseText(), response.status);

View File

@ -224,6 +224,11 @@ class FileApiDriverLocal {
throw new Error('Not supported'); throw new Error('Not supported');
} }
async clearRoot(baseDir) {
await this.fsDriver().remove(baseDir);
await this.fsDriver().mkdir(baseDir);
}
} }
module.exports = { FileApiDriverLocal }; module.exports = { FileApiDriverLocal };

View File

@ -159,6 +159,11 @@ class FileApiDriverMemory {
return output; return output;
} }
clearRoot() {
this.items_ = [];
return Promise.resolve();
}
} }
module.exports = { FileApiDriverMemory }; module.exports = { FileApiDriverMemory };

View File

@ -189,6 +189,10 @@ class FileApiDriverOneDrive {
return this.pathCache_[path]; return this.pathCache_[path];
} }
clearRoot() {
throw new Error('Not implemented');
}
async delta(path, options = null) { async delta(path, options = null) {
let output = { let output = {
hasMore: false, hasMore: false,

View File

@ -66,20 +66,26 @@ class FileApiDriverWebDav {
async delta(path, options) { async delta(path, options) {
const getDirStats = async (path) => { const getDirStats = async (path) => {
const result = await this.api().execPropFind(path, 1, [ const result = await this.list(path);
'd:getlastmodified', return result.items;
'd:resourcetype',
]);
const resources = this.api().arrayFromJson(result, ['d:multistatus', 'd:response']);
return this.statsFromResources_(resources);
}; };
return await basicDelta(path, getDirStats, options); return await basicDelta(path, getDirStats, options);
} }
async list(path, options) { async list(path, options) {
throw new Error('Not implemented'); // Not needed const result = await this.api().execPropFind(path, 1, [
'd:getlastmodified',
'd:resourcetype',
]);
const resources = this.api().arrayFromJson(result, ['d:multistatus', 'd:response']);
return {
items: this.statsFromResources_(resources),
hasMore: false,
context: null,
};
} }
async get(path, options) { async get(path, options) {
@ -120,6 +126,11 @@ class FileApiDriverWebDav {
throw new Error('Not supported'); throw new Error('Not supported');
} }
async clearRoot() {
await this.delete('');
await this.mkdir('');
}
} }
module.exports = { FileApiDriverWebDav }; module.exports = { FileApiDriverWebDav };

View File

@ -64,6 +64,7 @@ class FileApi {
}); });
} }
// Deprectated
setTimestamp(path, timestampMs) { setTimestamp(path, timestampMs) {
this.logger().debug('setTimestamp ' + this.fullPath_(path)); this.logger().debug('setTimestamp ' + this.fullPath_(path));
return this.driver_.setTimestamp(this.fullPath_(path), timestampMs); return this.driver_.setTimestamp(this.fullPath_(path), timestampMs);
@ -105,15 +106,21 @@ class FileApi {
return this.driver_.delete(this.fullPath_(path)); return this.driver_.delete(this.fullPath_(path));
} }
// Deprectated
move(oldPath, newPath) { move(oldPath, newPath) {
this.logger().debug('move ' + this.fullPath_(oldPath) + ' => ' + this.fullPath_(newPath)); this.logger().debug('move ' + this.fullPath_(oldPath) + ' => ' + this.fullPath_(newPath));
return this.driver_.move(this.fullPath_(oldPath), this.fullPath_(newPath)); return this.driver_.move(this.fullPath_(oldPath), this.fullPath_(newPath));
} }
// Deprectated
format() { format() {
return this.driver_.format(); return this.driver_.format();
} }
clearRoot() {
return this.driver_.clearRoot(this.baseDir_);
}
delta(path, options = null) { delta(path, options = null) {
this.logger().debug('delta ' + this.fullPath_(path)); this.logger().debug('delta ' + this.fullPath_(path));
return this.driver_.delta(this.fullPath_(path), options); return this.driver_.delta(this.fullPath_(path), options);

View File

@ -20,6 +20,11 @@ class FsDriverNode {
return fs.writeFile(path, string, { encoding: encoding }); return fs.writeFile(path, string, { encoding: encoding });
} }
// same as rm -rf
async remove(path) {
return fs.remove(path);
}
async move(source, dest) { async move(source, dest) {
let lastError = null; let lastError = null;

View File

@ -14,6 +14,11 @@ class FsDriverRN {
return RNFS.writeFile(path, string, encoding); return RNFS.writeFile(path, string, encoding);
} }
// same as rm -rf
async remove(path) {
throw new Error('Not implemented');
}
writeBinaryFile(path, content) { writeBinaryFile(path, content) {
throw new Error('Not implemented'); throw new Error('Not implemented');
} }