Commit 037394c2 authored by Mike Lynch's avatar Mike Lynch
Browse files

export_stream test was failing because it was await-ing on stream.pipe, rather than wrapping

the stream action in a Promise and awaiting that. Tests passing now.
parent 0338e88e
var chai = require('chai');
var chaiFiles = require('chai-files');
const chai = require('chai');
const chaiFiles = require('chai-files');
chai.use(chaiFiles);
var expect = chai.expect;
var assert = chai.assert;
var file = chaiFiles.file;
const expect = chai.expect;
const assert = chai.assert;
const file = chaiFiles.file;
import * as stream from 'stream';
const path = require('path');
......@@ -15,7 +17,8 @@ import { FixtureDirs } from './FilesApp.fixtures';
const FIXTURE_ID = "fileapp1";
const FIXTURE_DIR = "test/fixtures/fileapp1";
const FIXTURE_FILES = "test/files";
const FIXTURE_FILE = path.join(FIXTURE_FILES, 'green.jpg');
const FIXTURE_FILE_BIN = path.join(FIXTURE_FILES, 'green.jpg');
const FIXTURE_FILE = path.join(FIXTURE_FILES, 'text.txt');
const OUTPUT_ID = "output";
const OUTPUT_DIR = "test/output";
const FIXTURE_TREE = path.join(FIXTURE_FILES, 'small_tree');
......@@ -44,7 +47,17 @@ async function import_tree(fo) {
}
}
// copied from FilesApp.ts - takes a filename and a stream.Readable
// pipes the latter to the former and wraps it in a Promise
async function write_stream(fpath: string, rstream: stream.Readable): Promise<string> {
const wstream = fs.createWriteStream(fpath);
rstream.pipe(wstream);
return new Promise<string>( (resolve, reject) => {
wstream.on('finish', () => { resolve(fpath) });
wstream.on('error', reject);
});
}
describe('FilesDataSet', function() {
this.timeout(10000);
......@@ -105,7 +118,7 @@ describe('FilesDataSet', function() {
it("can import a file", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const instream = fs.createReadStream(FIXTURE_FILE);
const inpath = 'image.jpg';
const inpath = 'test.txt';
const oid = 'object1';
const fo = await fa.create(oid);
const fp = await fo.import_stream(inpath, instream);
......@@ -129,7 +142,7 @@ describe('FilesDataSet', function() {
it("can put a file in a directory", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const inpath = 'dir';
const infile = 'image.jpg';
const infile = 'test.txt';
const oid = 'object1';
const fo = await fa.create(oid);
const fp1 = await fo.import_path(inpath);
......@@ -148,7 +161,8 @@ describe('FilesDataSet', function() {
it("can use export_stream to return a file", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const instream = fs.createReadStream(FIXTURE_FILE);
const inpath = 'image.jpg';
const inpath = 'imported.txt';
const outpath = 'exported.txt'
const oid = 'object1';
const fo = await fa.create(oid);
const fp = await fo.import_stream(inpath, instream);
......@@ -157,12 +171,11 @@ describe('FilesDataSet', function() {
assert(fe, "imported file exists at " + fp);
expect(file(fp)).to.equal(file(FIXTURE_FILE));
let es = fo.export_stream(inpath);
expect(es).to.not.be.empty;
let rstream = fo.export_stream(inpath);
expect(rstream).to.not.be.empty;
let fpath = path.join(OUTPUT_DIR, inpath);
let expstream = fs.createWriteStream(fpath);
await es.pipe(expstream);
let fpath = path.join(OUTPUT_DIR, outpath);
await write_stream(fpath, rstream);
const efe = await fs.pathExists(fpath);
assert(efe, "exported file exists at " + fpath);
......@@ -173,7 +186,7 @@ describe('FilesDataSet', function() {
it("can use export_next to return a file", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const instream = fs.createReadStream(FIXTURE_FILE);
const inpath = 'image.jpg';
const inpath = 'text.txt';
const oid = 'object1';
const fo = await fa.create(oid);
const fp = await fo.import_stream(inpath, instream);
......@@ -189,8 +202,7 @@ describe('FilesDataSet', function() {
do {
let fpath = path.join(OUTPUT_DIR, ea.path);
if( ea.contents ) {
let expstream = fs.createWriteStream(fpath);
await ea.contents.pipe(expstream);
await write_stream(fpath, ea.contents);
} else {
throw new Error("Empty ExportAction");
}
......@@ -200,6 +212,7 @@ describe('FilesDataSet', function() {
let efp = path.join(OUTPUT_DIR, inpath);
const efe = await fs.pathExists(efp);
assert(efe, "exported file exists at " + efp);
expect(file(efp)).to.equal(file(FIXTURE_FILE));
});
......@@ -241,7 +254,7 @@ describe('FilesDataSet', function() {
it("can delete a file", async function() {
const fa = new FilesApp(FIXTURE_ID, FIXTURE_DIR);
const instream = fs.createReadStream(FIXTURE_FILE);
const inpath = 'image.jpg';
const inpath = 'test.txt';
const oid = 'object1';
const fo = await fa.create(oid);
const fp = await fo.import_stream(inpath, instream);
......@@ -276,3 +289,9 @@ describe('FilesDataSet', function() {
});
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