Skip to content
Merged
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
90 changes: 76 additions & 14 deletions Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,15 @@ function statusForEmptyVault() {
// stderr — captured stderr
//
// Returns:
// { ok: true, vault, entries, raw } — stdout was valid JSON
// { ok: false, reason, ... } — non-zero exit, exit 127, or
// stdout was not JSON
// { ok: true, vault, entries, raw } — exit 0, stdout parsed, and the
// summary document says ok:true
// { ok: false, reason, ... } — non-zero exit, exit 127, stdout
// not JSON, or no ok:true summary
//
// stdout is either one JSON document (older geode) or newline-delimited
// geode.event.v1 documents (geode 1.2.x): one per entry carrying `path`,
// then a summary carrying `files`, which is a count. `entries` is always an
// array of path strings; the `files` count is never stored in it.
// Never throws. Never returns ok:true without parsed JSON.
function statusFromRun(code, stdout, stderr) {
var out = typeof stdout === "string" ? stdout : "";
Expand Down Expand Up @@ -75,13 +81,8 @@ function statusFromRun(code, stdout, stderr) {
};
}

var doc = null;
try {
doc = JSON.parse(out);
} catch (e) {
doc = null;
}
if (doc === null || typeof doc !== "object") {
var docs = parseDocs(out);
if (docs === null) {
return {
ok: false,
reason: "geode list printed unparsable JSON",
Expand All @@ -92,10 +93,34 @@ function statusFromRun(code, stdout, stderr) {
}

var entries = [];
if (doc.entries && doc.entries.length !== undefined) {
entries = doc.entries;
} else if (doc.files !== undefined) {
entries = doc.files;
var summary = null;
for (var i = 0; i < docs.length; i++) {
var doc = docs[i];
if (typeof doc.path === "string") {
entries.push(doc.path);
continue;
}
if (Array.isArray(doc.entries)) {
for (var j = 0; j < doc.entries.length; j++) {
var e = doc.entries[j];
if (typeof e === "string") {
entries.push(e);
} else if (e !== null && typeof e === "object" && typeof e.path === "string") {
entries.push(e.path);
}
}
}
summary = doc;
}

if (summary === null || summary.ok !== true) {
return {
ok: false,
reason: "geode list printed no ok summary",
vault: "",
entries: [],
raw: out
};
}

return {
Expand All @@ -119,6 +144,43 @@ function statusFor(vaultPath, code, stdout, stderr) {

// ---- helpers ------------------------------------------------------------

// JSON objects in `out`: the whole text as one document if it parses,
// otherwise one per non-empty line. null when nothing parses, or when any
// line is not a JSON object.
function parseDocs(out) {
var whole = tryParse(out);
if (whole !== null) {
return [whole];
}
var lines = String(out).split(/\r?\n/);
var docs = [];
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim() === "") {
continue;
}
var doc = tryParse(lines[i]);
if (doc === null) {
return null;
}
docs.push(doc);
}
return docs.length === 0 ? null : docs;
}

// One JSON object, or null. Arrays and scalars are not status documents.
function tryParse(text) {
var doc = null;
try {
doc = JSON.parse(text);
} catch (e) {
return null;
}
if (doc === null || typeof doc !== "object" || Array.isArray(doc)) {
return null;
}
return doc;
}

function firstLine(s) {
var str = String(s);
var i = str.indexOf("\n");
Expand Down
77 changes: 77 additions & 0 deletions tests/list_ndjson.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env bash
#
# Issue #12: Model.js statusFromRun must read geode 1.2.1 `list --output json`
# (one geode.event.v1 document per line) and still read one JSON document
# from an older binary. ci.yml already runs every tests/*.sh under `set -e`.
#
# Loads Model.js in node (the `.pragma library` line is dropped; it is QML,
# not JavaScript) and checks the fixtures below. No geode subprocess, no QML
# runtime.
#
# Exit 0 when every fixture folds as expected, 1 otherwise or without node.

set -euo pipefail

root=$(cd -- "$(dirname -- "$0")/.." && pwd)
cd "$root"

if ! command -v node >/dev/null 2>&1; then
echo "FAIL: node is required to load Model.js" >&2
exit 1
fi

node - Model.js <<'JS'
const fs = require("fs");
const vm = require("vm");

const src = fs.readFileSync(process.argv[2], "utf8").replace(/^\.pragma library.*$/m, "");
const M = {};
vm.runInNewContext(src + "\nthis.statusFromRun = statusFromRun;", M);

let failed = 0;
function check(name, got, want) {
const g = JSON.stringify({ ok: got.ok, entries: got.entries });
const w = JSON.stringify(want);
if (g !== w) {
console.error(`FAIL: ${name}: got ${g}, want ${w}`);
failed = 1;
} else {
console.log(`ok: ${name}`);
}
}

// The two-line sample from issue #12, as geode 1.2.1 prints it.
const ndjson =
'{"content_root":"650239ad0d1172ad50cc49fc302f0828f59e280dd8ad2468192def7677206ff6","epoch":1,"key_id":"5d75d47c2269b65dc7159205ce7e5035","ok":true,"path":"demo-key.txt","plain_bytes":18,"schema":"geode.event.v1","vault_id":"7a67f6b5b774d2a8e78f1de88f95b306","verb":"list"}\n' +
'{"epoch":1,"files":1,"key_id":"5d75d47c2269b65dc7159205ce7e5035","ok":true,"plain_bytes":18,"schema":"geode.event.v1","vault_id":"7a67f6b5b774d2a8e78f1de88f95b306","verb":"list"}\n';

check("1.2.1 NDJSON entry + summary", M.statusFromRun(0, ndjson, ""), { ok: true, entries: ["demo-key.txt"] });
check("NDJSON without trailing newline", M.statusFromRun(0, ndjson.trimEnd(), ""), { ok: true, entries: ["demo-key.txt"] });
check("NDJSON with CRLF", M.statusFromRun(0, ndjson.replace(/\n/g, "\r\n"), ""), { ok: true, entries: ["demo-key.txt"] });
check("one document with entries", M.statusFromRun(0, '{"entries":["a.txt"],"ok":true}', ""), { ok: true, entries: ["a.txt"] });
check("one pretty-printed document", M.statusFromRun(0, '{\n "entries": ["a.txt"],\n "ok": true\n}\n', ""), { ok: true, entries: ["a.txt"] });
check(
"empty vault: summary files 0",
M.statusFromRun(0, '{"epoch":1,"files":0,"ok":true,"plain_bytes":0,"schema":"geode.event.v1","verb":"list"}\n', ""),
{ ok: true, entries: [] }
);

// The files count never lands in entries.
const counted = M.statusFromRun(0, '{"files":3,"ok":true}\n', "");
check("files count is not entries", counted, { ok: true, entries: [] });

// Failure shapes.
check("non-zero exit", M.statusFromRun(1, ndjson, "geode list: boom"), { ok: false, entries: [] });
const missing = M.statusFromRun(127, "", "");
check("exit 127", missing, { ok: false, entries: [] });
if (!/install it with: cargo install geode-cli --version 1\.2\.1/.test(missing.reason)) {
console.error(`FAIL: exit 127 lost the install sentence: ${missing.reason}`);
failed = 1;
}
check("garbage line", M.statusFromRun(0, ndjson + "not json\n", ""), { ok: false, entries: [] });
check("empty stdout", M.statusFromRun(0, "", ""), { ok: false, entries: [] });
check("summary ok false", M.statusFromRun(0, '{"files":0,"ok":false}\n', ""), { ok: false, entries: [] });
check("entry lines but no summary", M.statusFromRun(0, ndjson.split("\n")[0] + "\n", ""), { ok: false, entries: [] });

process.exit(failed);
JS
Loading