tail.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 runTailTests = () => {
  23. describe('tail', function () {
  24. // Too many parameters
  25. // Bad -n
  26. const failureCases = [
  27. {
  28. description: 'expects at most 1 argument',
  29. options: {},
  30. positionals: ['1', '2'],
  31. },
  32. {
  33. description: 'expects --lines, if set, to be a number',
  34. options: { lines: 'frog' },
  35. positionals: ['-'],
  36. },
  37. {
  38. description: 'expects --lines, if set, to be an integer',
  39. options: { lines: '1.75' },
  40. positionals: ['-'],
  41. },
  42. {
  43. description: 'expects --lines, if set, to be positive',
  44. options: { lines: '-3' },
  45. positionals: ['-'],
  46. },
  47. {
  48. description: 'expects --lines, if set, to not be 0',
  49. options: { lines: '0' },
  50. positionals: ['-'],
  51. },
  52. ];
  53. for (const { description, options, positionals } of failureCases) {
  54. it(description, async () => {
  55. let ctx = MakeTestContext(builtins.tail, { positionals, values: options });
  56. let hadError = false;
  57. try {
  58. await builtins.tail.execute(ctx);
  59. } catch (e) {
  60. hadError = true;
  61. }
  62. if (!hadError) {
  63. assert.fail('didn\'t return an error code');
  64. }
  65. assert.equal(ctx.externs.out.output, '', 'nothing should be written to stdout');
  66. // Output to stderr is allowed but not required.
  67. });
  68. }
  69. const alphabet = 'a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np\nq\nr\ns\nt\nu\nv\nw\nx\ny\nz\n';
  70. const testCases = [
  71. {
  72. description: 'reads from stdin if no parameter is given',
  73. options: {},
  74. positionals: [],
  75. stdin: alphabet,
  76. expectedStdout: 'q\nr\ns\nt\nu\nv\nw\nx\ny\nz\n',
  77. },
  78. {
  79. description: 'reads from stdin if parameter is `-`',
  80. options: {},
  81. positionals: ['-'],
  82. stdin: alphabet,
  83. expectedStdout: 'q\nr\ns\nt\nu\nv\nw\nx\ny\nz\n',
  84. },
  85. {
  86. description: '--lines/-n specifies how many lines to write',
  87. options: { lines: 5 },
  88. positionals: ['-'],
  89. stdin: alphabet,
  90. expectedStdout: 'v\nw\nx\ny\nz\n',
  91. },
  92. {
  93. description: 'when --lines/-n is greater than the number of lines, write everything',
  94. options: { lines: 500 },
  95. positionals: ['-'],
  96. stdin: alphabet,
  97. expectedStdout: alphabet,
  98. },
  99. // TODO: Test with files once the harness supports that.
  100. ];
  101. for (const { description, options, positionals, stdin, expectedStdout } of testCases) {
  102. it(description, async () => {
  103. let ctx = MakeTestContext(builtins.tail, { positionals, values: options, stdinInputs: [stdin] });
  104. try {
  105. const result = await builtins.tail.execute(ctx);
  106. assert.equal(result, undefined);
  107. } catch (e) {
  108. assert.fail(e);
  109. }
  110. assert.equal(ctx.externs.out.output, expectedStdout, 'wrong output written to stdout');
  111. assert.equal(ctx.externs.err.output, '', 'sleep should not write to stderr');
  112. });
  113. }
  114. });
  115. }