dirname.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 runDirnameTests = () => {
  23. describe('dirname', function () {
  24. it('expects at least 1 argument', async () => {
  25. let ctx = MakeTestContext(builtins.dirname, {});
  26. let hadError = false;
  27. try {
  28. await builtins.dirname.execute(ctx);
  29. } catch (e) {
  30. hadError = true;
  31. }
  32. if (!hadError) {
  33. assert.fail('should fail when given 0 arguments');
  34. }
  35. assert.equal(ctx.externs.out.output, '', 'nothing should be written to stdout');
  36. // Output to stderr is allowed but not required.
  37. });
  38. it('expects at most 1 argument', async () => {
  39. let ctx = MakeTestContext(builtins.dirname, {positionals: ['a', 'b']});
  40. let hadError = false;
  41. try {
  42. await builtins.dirname.execute(ctx);
  43. } catch (e) {
  44. hadError = true;
  45. }
  46. if (!hadError) {
  47. assert.fail('should fail when given 2 or more arguments');
  48. }
  49. assert.equal(ctx.externs.out.output, '', 'nothing should be written to stdout');
  50. // Output to stderr is allowed but not required.
  51. });
  52. const testCases = [
  53. {
  54. description: '"foo.txt" produces "."',
  55. input: 'foo.txt',
  56. expectedStdout: '.\n'
  57. },
  58. {
  59. description: '"./foo.txt" produces "."',
  60. input: './foo.txt',
  61. expectedStdout: '.\n'
  62. },
  63. {
  64. description: '"/a/b/c/foo.txt" produces "/a/b/c"',
  65. input: '/a/b/c/foo.txt',
  66. expectedStdout: '/a/b/c\n'
  67. },
  68. {
  69. description: '"a/b/c/foo.txt" produces "a/b/c"',
  70. input: 'a/b/c/foo.txt',
  71. expectedStdout: 'a/b/c\n'
  72. },
  73. {
  74. description: 'two slashes produces "/"',
  75. input: '//',
  76. expectedStdout: '/\n'
  77. },
  78. {
  79. description: 'a series of slashes produces "/"',
  80. input: '/////',
  81. expectedStdout: '/\n'
  82. },
  83. {
  84. description: 'empty string produces "/"',
  85. input: '',
  86. expectedStdout: '/\n'
  87. },
  88. {
  89. description: 'trailing slashes are trimmed',
  90. input: 'a/b/c////foo//',
  91. expectedStdout: 'a/b/c\n'
  92. },
  93. ];
  94. for (const {description, input, expectedStdout} of testCases) {
  95. it(description, async () => {
  96. let ctx = MakeTestContext(builtins.dirname, {positionals: [input]});
  97. try {
  98. const result = await builtins.dirname.execute(ctx);
  99. assert.equal(result, undefined, 'should exit successfully, returning nothing');
  100. } catch (e) {
  101. assert.fail(e);
  102. }
  103. assert.equal(ctx.externs.out.output, expectedStdout, 'wrong output written to stdout');
  104. assert.equal(ctx.externs.err.output, '', 'nothing should be written to stderr');
  105. });
  106. }
  107. });
  108. }