harness.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import { Context } from "contextlink";
  20. import { SyncLinesReader } from '../../src/ansi-shell/ioutil/SyncLinesReader.js';
  21. import { CommandStdinDecorator } from '../../src/ansi-shell/pipeline/iowrappers.js';
  22. import { ReadableStream, WritableStream } from 'stream/web'
  23. class WritableStringStream extends WritableStream {
  24. constructor() {
  25. super({
  26. write: (chunk) => {
  27. if (this.output_ === undefined)
  28. this.output_ = "";
  29. this.output_ += chunk;
  30. }
  31. });
  32. }
  33. write(chunk) {
  34. if (!this.writer_)
  35. this.writer_ = this.getWriter();
  36. return this.writer_.write(chunk);
  37. }
  38. get output() { return this.output_ || ""; }
  39. }
  40. // TODO: Flesh this out as needed.
  41. export const MakeTestContext = (command, { positionals = [], values = {}, stdinInputs = [], env = {} }) => {
  42. // This is a replacement to ReadableStream.from() in earlier Node versions
  43. // Sourece: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#convert_an_iterator_or_async_iterator_to_a_stream
  44. function iteratorToStream(iterator) {
  45. return new ReadableStream({
  46. async pull(controller) {
  47. const { value, done } = await iterator.next();
  48. if (done) {
  49. controller.close();
  50. } else {
  51. controller.enqueue(value);
  52. }
  53. },
  54. });
  55. }
  56. let in_ = iteratorToStream(stdinInputs.values()).getReader();
  57. if (command.input?.syncLines) {
  58. in_ = new SyncLinesReader({ delegate: in_ });
  59. }
  60. in_ = new CommandStdinDecorator(in_);
  61. return new Context({
  62. cmdExecState: { valid: true },
  63. externs: new Context({
  64. in_,
  65. out: new WritableStringStream(),
  66. err: new WritableStringStream(),
  67. sig: null,
  68. }),
  69. locals: new Context({
  70. args: [],
  71. command,
  72. positionals,
  73. values,
  74. }),
  75. platform: new Context({}),
  76. plugins: new Context({}),
  77. registries: new Context({}),
  78. env: env,
  79. });
  80. }