Skip to content

Commit dcadf7f

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

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
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: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,9 @@ spec = do
223223

224224
describe "ExitCodeException" $ do
225225
it "Show" $ do
226+
-- Note that the `show` output ends with a newline, so functions
227+
-- like `print` will output an extra blank line at the end of the
228+
-- output.
226229
let exitCodeException =
227230
ExitCodeException
228231
{ eceExitCode = ExitFailure 1
@@ -238,7 +241,7 @@ spec = do
238241
++ "Copied OK\n"
239242
++ "\n"
240243
++ "Standard error:\n"
241-
++ "Uh oh!"
244+
++ "Uh oh!\n"
242245

243246
it "Show only stdout" $ do
244247
let exitCodeException =
@@ -253,7 +256,7 @@ spec = do
253256
++ "Raw command: show-puppy\n"
254257
++ "\n"
255258
++ "Standard output:\n"
256-
++ "No puppies found???"
259+
++ "No puppies found???\n"
257260

258261
it "Show only stderr" $ do
259262
let exitCodeException =
@@ -266,17 +269,12 @@ spec = do
266269
show exitCodeException `shouldBe`
267270
"Received ExitFailure 1 when running\n"
268271
++ "Raw command: show-puppy\n"
269-
++ "\n"
270272
++ "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.
273+
++ "No puppies found???\n"
274+
275+
it "Show does not trim stdout/stderr" $ do
276+
-- This looks weird, and I think it would be better to strip the
277+
-- whitespace from the output.
280278
let exitCodeException =
281279
ExitCodeException
282280
{ eceExitCode = ExitFailure 1
@@ -289,12 +287,12 @@ spec = do
289287
++ "Raw command: detect-doggies\n"
290288
++ "\n"
291289
++ "Standard output:\n"
292-
++ "puppy\n"
290+
++ "\n\npuppy\n\n \n"
293291
++ "\n"
294292
++ "Standard error:\n"
295-
++ "doggy"
293+
++ "\t \ndoggy\n \t\n"
296294

297-
it "Show displays correctly with no newlines in stdout" $ do
295+
it "Show displays weirdly with no newlines in stdout" $ do
298296
-- Sometimes, commands don't output _any_ newlines!
299297
let exitCodeException =
300298
ExitCodeException
@@ -309,3 +307,22 @@ spec = do
309307
++ "\n"
310308
++ "Standard output:\n"
311309
++ "puppy"
310+
311+
it "Show displays weirdly with no newlines in stdout or stderr" $ do
312+
-- If the stderr isn't empty and stdout doesn't end with a newline,
313+
-- the blank line between the two sections disappears.
314+
let exitCodeException =
315+
ExitCodeException
316+
{ eceExitCode = ExitFailure 1
317+
, eceProcessConfig = proc "detect-doggies" []
318+
, eceStdout = fromString "puppy"
319+
, eceStderr = fromString "doggy"
320+
}
321+
show exitCodeException `shouldBe`
322+
"Received ExitFailure 1 when running\n"
323+
++ "Raw command: detect-doggies\n"
324+
++ "\n"
325+
++ "Standard output:\n"
326+
++ "puppy\n"
327+
++ "Standard error:\n"
328+
++ "doggy"

0 commit comments

Comments
 (0)