Commit 9874d848 authored by Mike Lynch's avatar Mike Lynch
Browse files

Test written and passing to import and export a small file hierarchy

parent 657cc73b
......@@ -197,5 +197,4 @@ export class FilesExporter extends BaseExporter implements IExporter {
);
}
}
......@@ -14,6 +14,17 @@ const FIXTURE_DIR = "test/fixtures/fileapp1";
const FIXTURE_FILES = "test/files";
const FIXTURE_FILE = path.join(FIXTURE_FILES, 'green.jpg');
const OUTPUT = "test/output";
const FIXTURE_TREE = path.join(FIXTURE_FILES, 'small_tree');
const OUTPUT_TREE = path.join(OUTPUT, 'small_tree');
const SMALL_TREE_FILES = [
'file1.txt',
'file2.txt',
'dir2/file3.txt',
'dir2/dir3/file4.txt',
'dir2/dir3/file5.txt'
];
describe('FilesWorkspace', function() {
this.timeout(10000);
......@@ -146,6 +157,31 @@ describe('FilesWorkspace', function() {
});
it("can import and export a small file tree", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const oid = 'object1';
const fo = await fa.create(oid);
// note: quick and dirty code to import the tree so that it can
// be exported.
for( var i in SMALL_TREE_FILES ) {
let f = SMALL_TREE_FILES[i];
let dir = path.dirname(f);
let fn = path.basename(f);
if( dir ) {
await fo.import_path(dir);
}
let fcont = fs.createReadStream(path.join(FIXTURE_TREE, f));
await fo.import_stream(f, fcont);
}
await fs.ensureDir(OUTPUT_TREE);
await fo.export_all(OUTPUT_TREE);
for( var i in SMALL_TREE_FILES ) {
let f = SMALL_TREE_FILES[i];
let tfp = path.join(OUTPUT_TREE, f);
let tfe = await fs.pathExists(tfp);
assert(tfe, "exported file exists at " + tfp);
}
});
......
......@@ -7,6 +7,8 @@
import * as stream from 'stream';
import * as shortid from 'shortid';
import * as path from 'path';
import * as fs from 'fs-extra';
import { promisify } from 'util';
......@@ -65,7 +67,6 @@ export abstract class BaseWorkspace {
// to another.
abstract import_stream(path: string, data: stream.Readable): Promise<string>;
abstract import_path(path: string): Promise<string>;
abstract import_action(ea: ExportAction): Promise<string>;
......@@ -93,6 +94,35 @@ export abstract class BaseWorkspace {
}
abstract new_exporter(eid: string): IExporter;
// For import_all: remember that an import of a file system is the
// same as an export of a file system, somehow reuse the exporter
// class from the files stuff.
// export_all loops over the export actions and writes the
// contents into a file system - it should be the same for any
// kind of workspace
async export_all(dest: string): Promise<boolean> {
let ea = await this.export_stream();
const token = ea.token;
do {
let fpath = path.join(dest, ea.path);
if( ea.action == ActionType.Dir ) {
await fs.ensureDir(fpath);
} else if( ea.action == ActionType.File ) {
if( ea.contents ) {
let expstream = fs.createWriteStream(fpath);
await ea.contents.pipe(expstream);
} else {
throw new Error("Empty ExportAction");
}
}
ea = await this.export_stream(token);
} while( ea );
return true;
}
}
......@@ -104,6 +134,7 @@ export interface IWorkspace {
import_stream(path: string, data: stream.Readable): Promise<string>;
import_path(path:string): Promise<string>;
export_stream(token?: string): Promise<ExportAction|null>;
export_all(path:string): Promise<boolean>;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment