puter/packages/phoenix/test/coreutils/harness.js
2024-04-12 20:53:44 -04:00

71 lines
2.2 KiB
JavaScript

/*
* Copyright (C) 2024 Puter Technologies Inc.
*
* This file is part of Phoenix Shell.
*
* Phoenix Shell is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { Context } from "contextlink";
import { SyncLinesReader } from '../../src/ansi-shell/ioutil/SyncLinesReader.js';
import { CommandStdinDecorator } from '../../src/ansi-shell/pipeline/iowrappers.js';
export class WritableStringStream extends WritableStream {
constructor() {
super({
write: (chunk) => {
if (this.output_ === undefined)
this.output_ = "";
this.output_ += chunk;
}
});
}
write(chunk) {
if (!this.writer_)
this.writer_ = this.getWriter();
return this.writer_.write(chunk);
}
get output() { return this.output_ || ""; }
}
// TODO: Flesh this out as needed.
export const MakeTestContext = (command, { positionals = [], values = {}, stdinInputs = [], env = {} }) => {
let in_ = ReadableStream.from(stdinInputs).getReader();
if (command.input?.syncLines) {
in_ = new SyncLinesReader({ delegate: in_ });
}
in_ = new CommandStdinDecorator(in_);
return new Context({
cmdExecState: { valid: true },
externs: new Context({
in_,
out: new WritableStringStream(),
err: new WritableStringStream(),
sig: null,
}),
locals: new Context({
args: [],
command,
positionals,
values,
}),
platform: new Context({}),
plugins: new Context({}),
registries: new Context({}),
env: env,
});
}