basename.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 runBasenameTests = () => {
  23. describe('basename', function () {
  24. it('expects at least 1 argument', async () => {
  25. let ctx = MakeTestContext(builtins.basename, {});
  26. let hadError = false;
  27. try {
  28. await builtins.basename.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 2 arguments', async () => {
  39. let ctx = MakeTestContext(builtins.basename, {positionals: ['a', 'b', 'c']});
  40. let hadError = false;
  41. try {
  42. await builtins.basename.execute(ctx);
  43. } catch (e) {
  44. hadError = true;
  45. }
  46. if (!hadError) {
  47. assert.fail('should fail when given 3 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 "foo.txt"',
  55. input: ['foo.txt'],
  56. expectedStdout: 'foo.txt\n'
  57. },
  58. {
  59. description: '"./foo.txt" produces "foo.txt"',
  60. input: ['./foo.txt'],
  61. expectedStdout: 'foo.txt\n'
  62. },
  63. {
  64. description: '"/a/b/c/foo.txt" produces "foo.txt"',
  65. input: ['/a/b/c/foo.txt'],
  66. expectedStdout: 'foo.txt\n'
  67. },
  68. {
  69. description: 'two slashes produces "/"',
  70. input: ['//'],
  71. expectedStdout: '/\n'
  72. },
  73. {
  74. description: 'a series of slashes produces "/"',
  75. input: ['/////'],
  76. expectedStdout: '/\n'
  77. },
  78. {
  79. description: 'empty string produces "/"',
  80. input: [''],
  81. expectedStdout: '.\n'
  82. },
  83. {
  84. description: 'trailing slashes are trimmed',
  85. input: ['foo.txt/'],
  86. expectedStdout: 'foo.txt\n'
  87. },
  88. {
  89. description: 'suffix is removed from simple filename',
  90. input: ['foo.txt', '.txt'],
  91. expectedStdout: 'foo\n'
  92. },
  93. {
  94. description: 'suffix is removed from path',
  95. input: ['/a/b/c/foo.txt', '.txt'],
  96. expectedStdout: 'foo\n'
  97. },
  98. {
  99. description: 'suffix is removed only once',
  100. input: ['/a/b/c/foo.txt.txt.txt', '.txt'],
  101. expectedStdout: 'foo.txt.txt\n'
  102. },
  103. {
  104. description: 'suffix is ignored if not found in the input',
  105. input: ['/a/b/c/foo.txt', '.png'],
  106. expectedStdout: 'foo.txt\n'
  107. },
  108. {
  109. description: 'suffix is removed even if input has a trailing slash',
  110. input: ['/a/b/c/foo.txt/', '.txt'],
  111. expectedStdout: 'foo\n'
  112. },
  113. ];
  114. for (const {description, input, expectedStdout} of testCases) {
  115. it(description, async () => {
  116. let ctx = MakeTestContext(builtins.basename, {positionals: input});
  117. try {
  118. const result = await builtins.basename.execute(ctx);
  119. assert.equal(result, undefined, 'should exit successfully, returning nothing');
  120. } catch (e) {
  121. assert.fail(e);
  122. }
  123. assert.equal(ctx.externs.out.output, expectedStdout, 'wrong output written to stdout');
  124. assert.equal(ctx.externs.err.output, '', 'nothing should be written to stderr');
  125. });
  126. }
  127. });
  128. }