geelen / shellac

Coat your shell scripts in something a bit more beautiful
MIT License
156 stars 4 forks source link

Make most tests pass on Windows #15

Open Macil opened 5 months ago

Macil commented 5 months ago

While investigating https://github.com/geelen/shellac/issues/14, I found that the tests couldn't be run on Windows:

  1. Running npm test failed with this output:
PS E:\chris\Coding\shellac> npm test

> shellac@0.8.0 test
> NODE_OPTIONS=--experimental-vm-modules jest

'NODE_OPTIONS' is not recognized as an internal or external command,
operable program or batch file.

This PR uses cross-env to fix that.

  1. After that, running npm test failed with output like this:
 FAIL  test/failures.test.ts
  ● Test suite failed to run

    SyntaxError: The requested module './shell' does not provide an export named 'default'                                             

      at Runtime.linkAndEvaluateModule (node_modules/.pnpm/jest-runtime@27.5.1/node_modules/jest-runtime/build/index.js:779:5)

This appears to be an issue with Jest with ESM on Windows that has been fixed in newer Jest versions, so I updated Jest and most dev dependencies to fix that.

  1. A lot of tests were broken because stuff like Bash on Windows normalizing file paths to look Unix style (C:\Users into /c/Users) or because of assumptions that process pids are strictly monotonically increasing or that the user has "EDITOR" and "TMPDIR" env variables set, so I fixed the tests to not depend on these things.

With these changes, the tests go from not starting at all on Windows to having 49/51 passing (depending on the build of Bash installed).

Full npm test output on Windows ``` PS E:\chris\Coding\shellac> npm test > shellac@0.8.0 test > cross-env NODE_OPTIONS=--experimental-vm-modules jest (node:34528) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) PASS test/parser.test.ts (node:43460) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) PASS test/failures.test.ts (node:5184) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) (node:46572) ExperimentalWarning: VM Modules is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) 1 2 3 PASS test/env.test.ts one - two node:fs:1634 const stats = binding.lstat( ^ Error: ENOENT: no such file or directory, lstat 'C:\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' at Object.lstatSync (node:fs:1634:25) at print (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\src\index.js:50:20) at tree (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\src\index.js:160:10) at Object. (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\bin\tree.js:60:16) at Module._compile (node:internal/modules/cjs/loader:1368:14) at Module._extensions..js (node:internal/modules/cjs/loader:1426:10) at Module.load (node:internal/modules/cjs/loader:1205:32) at Module._load (node:internal/modules/cjs/loader:1021:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) at node:internal/main/run_main_module:28:49 { errno: -4058, code: 'ENOENT', syscall: 'lstat', path: 'C:\\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' } Node.js v21.7.3 EXIT WHEN NOT EXPECTED SHELLAC COMMAND FAILED! Executing: npx tree-node-cli C:\Users\agent\AppData\Local\Temp\tmp-5184-CBUlxIGDbZHZ in E:\chris\Coding\shellac STDOUT: STDERR: node:fs:1634 const stats = binding.lstat( ^ Error: ENOENT: no such file or directory, lstat 'C:\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' at Object.lstatSync (node:fs:1634:25) at print (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\src\index.js:50:20) at tree (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\src\index.js:160:10) at Object. (E:\chris\Coding\shellac\node_modules\.pnpm\tree-node-cli@1.6.0\node_modules\tree-node-cli\bin\tree.js:60:16) at Module._compile (node:internal/modules/cjs/loader:1368:14) at Module._extensions..js (node:internal/modules/cjs/loader:1426:10) at Module.load (node:internal/modules/cjs/loader:1205:32) at Module._load (node:internal/modules/cjs/loader:1021:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) at node:internal/main/run_main_module:28:49 { errno: -4058, code: 'ENOENT', syscall: 'lstat', path: 'C:\\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' } Node.js v21.7.3 EXIT WHEN NOT EXPECTED/tmp/tmp-5184-CcKbT3NT7TRg/lol boats /tmp/tmp-5184-yPkFGjx16iyQ/lol boats /tmp/tmp-5184-yPkFGjx16iyQ/lol boats/subdir (node:5184) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [ChildProcess]. Use emitter.setMaxListeners() to increase limit background boi start 1 2 3 background boi done background boi start 1 PID PPID PGID WINPID TTY UID STIME COMMAND 1502 1501 1501 49508 ? 197609 20:20:47 /usr/bin/ps 1500 1 1500 41032 ? 197609 20:20:47 /usr/bin/bash 1503 1500 1500 50560 ? 197609 20:20:47 /usr/bin/sleep 1008 1 1008 31292 cons0 197609 19:35:37 /usr/bin/bash 1501 1 1501 52036 ? 197609 20:20:47 /usr/bin/bash /usr/bin/bash: line 6: kill: (49952) - No such process 2 EXIT WHEN NOT EXPECTED SHELLAC COMMAND FAILED! Executing: kill 49952 in E:\chris\Coding\shellac STDOUT: STDERR: /usr/bin/bash: line 6: kill: (49952) - No such process FAIL test/basic.test.ts (7.95 s) ● getting started › should create files and directories thrown: Object { "cmd": "npx tree-node-cli C:\\Users\\agent\\AppData\\Local\\Temp\\tmp-5184-CBUlxIGDbZHZ", "retCode": 1, "stderr": "node:fs:1634 const stats = binding.lstat( ^ Error: ENOENT: no such file or directory, lstat 'C:\\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' at Object.lstatSync (node:fs:1634:25) at print (node_modules/.pnpm/tree-node-cli@1.6.0/node_modules/tree-node-cli/src/index.js:50:20) at tree (node_modules/.pnpm/tree-node-cli@1.6.0/node_modules/tree-node-cli/src/index.js:160:10) at Object. (node_modules/.pnpm/tree-node-cli@1.6.0/node_modules/tree-node-cli/bin/tree.js:60:16) at Module._compile (node:internal/modules/cjs/loader:1368:14) at Module._extensions..js (node:internal/modules/cjs/loader:1426:10) at Module.load (node:internal/modules/cjs/loader:1205:32) at Module._load (node:internal/modules/cjs/loader:1021:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) at node:internal/main/run_main_module:28:49 { errno: -4058, code: 'ENOENT', syscall: 'lstat', path: 'C:\\\\UsersagentAppDataLocalTemptmp-5184-CBUlxIGDbZHZ' } Node.js v21.7.3 ", "stdout": "", } at test/basic.test.ts:226:3 at test/basic.test.ts:11:1 ● getting started › should permit killing background tasks thrown: Object { "cmd": "kill 49952", "retCode": 1, "stderr": "/usr/bin/bash: line 6: kill: (49952) - No such process ", "stdout": "", } 374 | }) 375 | > 376 | it('should permit killing background tasks', async () => { | ^ 377 | const { pid, promise } = await shellac.bg` 378 | $$ echo background boi start 379 | exits { at test/basic.test.ts:376:3 at test/basic.test.ts:11:1 A worker process has failed to exit gracefully and has been force exited. This is likely caused by tests leaking due to improper teardown. Try running with --detectOpenHandles to find leaks. Active timers can also cause this, ensure that .unref() was called on them. Test Suites: 1 failed, 3 passed, 4 total Tests: 2 failed, 49 passed, 51 total Snapshots: 0 total Time: 8.836 s, estimated 9 s Ran all test suites. ```