echo.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 assert from 'assert';
  20. import { MakeTestContext } from './harness.js'
  21. import builtins from '../../src/puter-shell/coreutils/__exports__.js';
  22. export const runEchoTests = () => {
  23. describe('echo', function () {
  24. const testCases = [
  25. {
  26. description: 'empty input prints a newline',
  27. input: [],
  28. options: {},
  29. expectedStdout: '\n'
  30. },
  31. {
  32. description: 'single input is printed',
  33. input: ['hello'],
  34. options: {},
  35. expectedStdout: 'hello\n'
  36. },
  37. {
  38. description: 'multiple inputs are printed, separated by spaces',
  39. input: ['hello', 'world'],
  40. options: {},
  41. expectedStdout: 'hello world\n'
  42. },
  43. {
  44. description: '-n suppresses newlines',
  45. input: ['hello', 'world'],
  46. options: {
  47. n: true
  48. },
  49. expectedStdout: 'hello world'
  50. },
  51. // TODO: Test the `-e` option for interpreting backslash escapes.
  52. ];
  53. for (const {description, input, options, expectedStdout} of testCases) {
  54. it(description, async () => {
  55. let ctx = MakeTestContext(builtins.echo, {positionals: input, values: options});
  56. try {
  57. const result = await builtins.echo.execute(ctx);
  58. assert.equal(result, undefined, 'should exit successfully, returning nothing');
  59. } catch (e) {
  60. assert.fail(e);
  61. }
  62. assert.equal(ctx.externs.out.output, expectedStdout, 'wrong output written to stdout');
  63. assert.equal(ctx.externs.err.output, '', 'nothing should be written to stderr');
  64. });
  65. }
  66. });
  67. }