Skip to content
This repository has been archived by the owner on Feb 12, 2021. It is now read-only.

Commit

Permalink
Update tests to use latest util.js. file.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Jun 9, 2019
1 parent fb6c70c commit 216bf4d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion config.json.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"generator": "../your/app",
"generator": "../did-cli/did",
}
11 changes: 11 additions & 0 deletions test/latest/10-basic.js → test/latest/10-identifier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
/* eslint-disable max-len */
const {expect} = require('chai');
const config = require('../../config.json');
const util = require('./util');

describe('Identifier', function() {
let generatorOptions = null;
beforeEach(function() {
generatorOptions = {
generator: config.generator,
command: 'c14n',
args: {},
date: new Date().toGMTString(),
};
});

it('A DID method specification MUST further restrict the generic DID syntax by defining its own method-name and its own method-specific-id syntax.', function() {
expect('did:uuid:0d2bae3e-4915-489c-bfa1-1ba14e8b43cd').to.be.a.DID;
Expand Down
50 changes: 39 additions & 11 deletions test/latest/util.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
'use strict';
const path = require('path');
const util = require('util');

const exec = util.promisify(require('child_process').exec);
const path = require('path');
const {exec} = require('child_process');

async function generate(file, options) {
options = options || {};
const filePath = path.join(__dirname, 'input', `${file}.jsonld`);
const latestDate = `date: ${date}`;
const filePath = path.join(__dirname, 'input', `${file}.httpMessage`);
let args = '';
for(const key in options.args) {
let value = options.args[key];
Expand All @@ -20,12 +18,42 @@ async function generate(file, options) {
}
// this cat filePath - the dash is the last pipe op
const httpMessage = `cat ${filePath} - | `;
const generate = `${options.generator} ${options.command} `;
const {stdout, stderr} = await exec(httpMessage + generate + args);
if(stderr) {
throw new Error(stderr);
}
return stdout;
const binaryOps = `${options.generator} ${options.command} `;
const command = httpMessage + binaryOps + args;
const result = await runTest(command);
return result;
}

function runTest(command) {
return new Promise((resolve, reject) => {
const child = exec(command);
const streams = Promise.all([
streamToString(child.stdout),
streamToString(child.stderr)
]);
child.addListener('exit', async code => {
const [stdout, stderr] = await streams;
if(code === 0) {
resolve(stdout);
} else {
const error = new Error(
`Driver exited with error code ${code}. \n ${stderr}`);
error.code = code;
error.stdout = stdout;
error.stderr = stderr;
reject(error);
}
});
});
}

function streamToString(stream) {
const chunks = [];
return new Promise((resolve, reject) => {
stream.on('data', chunk => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(chunks.join('')));
});
}

module.exports = {
Expand Down

0 comments on commit 216bf4d

Please sign in to comment.