diff options
author | 握猫猫 <164346864@qq.com> | 2024-08-29 02:52:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-28 20:52:00 +0200 |
commit | 5e8cd318ef52c5043306a47b8ecb2a8d72d76805 (patch) | |
tree | bac2facad6323a1d4163df15718585aa70850954 /tests/osproc | |
parent | 770f8d551372893ed480550062a89e76c60ff4a8 (diff) | |
download | Nim-5e8cd318ef52c5043306a47b8ecb2a8d72d76805.tar.gz |
Fix linux start process errorCode always 0 (#24001)
#23992 The test case provided does not cover the Windows situation, I fixed it in this new PR. Fixed an issue where errorCode was always 0 when startProcess didn't use the poEvalCommand flag. Tthe sleep command might not be available in all Windows installations, so I skipped the relevant test. Added a test case, tested on my fedora and windows systems.
Diffstat (limited to 'tests/osproc')
-rw-r--r-- | tests/osproc/tnoexe.nim | 27 | ||||
-rw-r--r-- | tests/osproc/twaitforexit.nim | 25 |
2 files changed, 42 insertions, 10 deletions
diff --git a/tests/osproc/tnoexe.nim b/tests/osproc/tnoexe.nim new file mode 100644 index 000000000..19a3cca67 --- /dev/null +++ b/tests/osproc/tnoexe.nim @@ -0,0 +1,27 @@ +discard """ + output: '''true +true''' +""" + +import std/osproc + +const command = "lsaaa -lah" + +try: + let process = startProcess(command, options = {poUsePath}) + discard process.waitForExit() +except OSError as e: + echo e.errorCode != 0 + +# `poEvalCommand`, invokes the system shell to run the specified command +try: + let process = startProcess(command, options = {poUsePath, poEvalCommand}) + # linux + let exitCode = process.waitForExit() + echo exitCode != 0 +except OSError as e: + # Because the implementation of `poEvalCommand` on different platforms is inconsistent, + # Linux will not throw an exception, but Windows will throw an exception + + # windows + echo e.errorCode != 0 diff --git a/tests/osproc/twaitforexit.nim b/tests/osproc/twaitforexit.nim index 67caa4165..535faca63 100644 --- a/tests/osproc/twaitforexit.nim +++ b/tests/osproc/twaitforexit.nim @@ -18,16 +18,21 @@ block: # bug #5091 doAssert(getTime() < atStart + milliseconds(msWait)) block: # bug #23825 - var thr: array[0..99, Thread[int]] - proc threadFunc(i: int) {.thread.} = - let sleepTime = float(i) / float(thr.len + 1) - doAssert sleepTime < 1.0 - let p = startProcess("sleep", workingDir = "", args = @[$sleepTime], options = {poUsePath, poParentStreams}) - # timeout = 1_000_000 seconds ~= 278 hours ~= 11.5 days - doAssert p.waitForExit(timeout=1_000_000_000) == 0 + # the sleep command might not be available in all Windows installations - for i in low(thr)..high(thr): - createThread(thr[i], threadFunc, i) + when defined(linux): + + var thr: array[0..99, Thread[int]] + + proc threadFunc(i: int) {.thread.} = + let sleepTime = float(i) / float(thr.len + 1) + doAssert sleepTime < 1.0 + let p = startProcess("sleep", workingDir = "", args = @[$sleepTime], options = {poUsePath, poParentStreams}) + # timeout = 1_000_000 seconds ~= 278 hours ~= 11.5 days + doAssert p.waitForExit(timeout=1_000_000_000) == 0 + + for i in low(thr)..high(thr): + createThread(thr[i], threadFunc, i) - joinThreads(thr) + joinThreads(thr) |