Skip to content

Commit 2a56450

Browse files
tomjaguarpaw9999years
authored andcommitted
Don't strip whitespace from stdout/stderr
1 parent 9e70f3a commit 2a56450

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
- stm
2222
- transformers
2323
- unliftio-core
24+
- text >=2.0
2425

2526
library:
2627
source-dirs: src

src/System/Process/Typed/Internal.hs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,28 @@ data ExitCodeException = ExitCodeException
621621
instance Exception ExitCodeException
622622
instance Show ExitCodeException where
623623
show ece =
624-
let decodeStrip = T.unpack . T.strip . TL.toStrict . TLE.decodeUtf8With lenientDecode
625-
stdout = decodeStrip $ eceStdout ece
626-
stderr = decodeStrip $ eceStderr ece
627-
stdout' = if null stdout
624+
let decode = TL.toStrict . TLE.decodeUtf8With lenientDecode
625+
626+
isAsciiSpace char = case char of
627+
' ' -> True
628+
'\t' -> True
629+
'\n' -> True
630+
'\r' -> True
631+
_ -> False
632+
isOnlyAsciiWhitespace = T.null . T.dropAround isAsciiSpace
633+
634+
stdout = decode $ eceStdout ece
635+
stderr = decode $ eceStderr ece
636+
stdout' = if isOnlyAsciiWhitespace stdout
628637
then []
629-
else ["\n\nStandard output:\n", stdout]
630-
stderr' = if null stderr
638+
else [ "\n\nStandard output:\n"
639+
, T.unpack stdout
640+
]
641+
stderr' = if isOnlyAsciiWhitespace stderr
631642
then []
632-
else ["\n\nStandard error:\n", stderr]
643+
else [ "\nStandard error:\n"
644+
, T.unpack stderr
645+
]
633646
in concat $
634647
[ "Received "
635648
, show (eceExitCode ece)

test/System/Process/TypedSpec.hs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ spec = do
238238
++ "Copied OK\n"
239239
++ "\n"
240240
++ "Standard error:\n"
241-
++ "Uh oh!"
241+
++ "Uh oh!\n"
242242

243243
it "Show only stdout" $ do
244244
let exitCodeException =
@@ -253,7 +253,7 @@ spec = do
253253
++ "Raw command: show-puppy\n"
254254
++ "\n"
255255
++ "Standard output:\n"
256-
++ "No puppies found???"
256+
++ "No puppies found???\n"
257257

258258
it "Show only stderr" $ do
259259
let exitCodeException =
@@ -266,17 +266,10 @@ spec = do
266266
show exitCodeException `shouldBe`
267267
"Received ExitFailure 1 when running\n"
268268
++ "Raw command: show-puppy\n"
269-
++ "\n"
270269
++ "Standard error:\n"
271-
++ "No puppies found???"
272-
273-
it "Show trims stdout/stderr" $ do
274-
-- This keeps the `Show` output looking nice regardless of how many
275-
-- newlines (if any) the command outputs.
276-
--
277-
-- This also makes sure that the `Show` output doesn't end with a
278-
-- spurious trailing newline, making it easier to compose `Show`
279-
-- instances together.
270+
++ "No puppies found???\n"
271+
272+
it "Show does not trim stdout/stderr" $ do
280273
let exitCodeException =
281274
ExitCodeException
282275
{ eceExitCode = ExitFailure 1
@@ -289,10 +282,10 @@ spec = do
289282
++ "Raw command: detect-doggies\n"
290283
++ "\n"
291284
++ "Standard output:\n"
292-
++ "puppy\n"
285+
++ "\n\npuppy\n\n \n"
293286
++ "\n"
294287
++ "Standard error:\n"
295-
++ "doggy"
288+
++ "\t \ndoggy\n \t\n"
296289

297290
it "Show displays correctly with no newlines in stdout" $ do
298291
-- Sometimes, commands don't output _any_ newlines!

0 commit comments

Comments
 (0)