Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover kni command compiled json mode #53

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion kni-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ function testDescribe(kniscript, descript, done) {
}, done);
}

function testCompiledJson(kniscript, transcript, done) {
withTempDir(transcript, function under(dir, fin) {
var outfile = dir + '/out';
var jsonfile = dir + '/json';
runArgs([kniscript, '-j'], jsonfile, function compileDone(err) {
if (err) {
fin(err);
return;
}
runArgs(['-J', jsonfile, '-v', transcript], outfile, fin);
});
}, done);
}

function main() {
fs.readdir('tests', function(err, files) {
if (err) {
Expand Down Expand Up @@ -130,8 +144,8 @@ function main() {

// verification tests
[
// TODO also derive from files and/or reconcile with engine-test
['basic', testBasic],
['compiledJson', testCompiledJson],
].forEach(function eachTestMode(testMode) {
var testModeName = testMode[0];
var runTest = testMode[1];
Expand Down
31 changes: 25 additions & 6 deletions kni.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,26 @@ function run(args, out, done) {
out = tee(config.transcript, out);
}

var kniscript;
var states;

if (config.fromJson) {
states = JSON.parse(kniscripts[0].content); // TODO test needed
if (kniscripts.length !== 1) {
done(new Error('must provide (only) one JSON input file'));
return;
}
try {
kniscript = kniscripts[0].content;
states = JSON.parse(kniscript);
} catch (err) {
done(err);
return;
}
} else {
var story = new Story();

for (var i = 0; i < kniscripts.length; i++) {
var kniscript = kniscripts[i].content;
kniscript = kniscripts[i].content;

if (config.debugInput) {
console.log(kniscript);
Expand Down Expand Up @@ -116,10 +128,12 @@ function run(args, out, done) {
}

if (config.toJson) {
console.log(JSON.stringify(states, null, 4), done);
interactive = false;
// TODO streaming json encoder?
out.write(JSON.stringify(states, null, 4), done);
return;
}

} else if (config.toHtml) {
if (config.toHtml) {
makeHtml(states, config.toHtml, {
title: config.htmlTitle,
color: config.htmlColor,
Expand Down Expand Up @@ -179,7 +193,12 @@ function run(args, out, done) {
done(err);
return;
}
waypoint = JSON.parse(waypoint);
try {
waypoint = JSON.parse(waypoint);
} catch (err) {
done(err);
return;
}
engine.continue(waypoint);
});
} else {
Expand Down