@@ -455,12 +455,42 @@ compareFile expected actual = do
455455 c2 <- BC. readFile actual
456456 compareByteString c1 c2
457457
458+ -- XXX: It appears the rewriter has different behaviour on Windows and Linux --
459+ -- specifically relating to newlines. So we use a custom equality check
460+ -- that skips Windows newlines (@\r@ characters are skipped).
461+ --
462+ -- (This is morally grey: the issue is unlikely to rear its head in
463+ -- practical usage, _but_ if you were to be comparing rewriter output from
464+ -- different platforms, you may encounter it. Most cross-platform text
465+ -- tooling recognises/ignores Windows newlines, however.)
458466compareByteString :: BC. ByteString -> BC. ByteString -> IO Bool
459- compareByteString expected actual = if expected == actual
467+ compareByteString expected actual = if expected `eqSkipWinNewlines` actual
460468 then return True
461469 else do
462470 BC. putStrLn " >>>>>>> EXPECTED"
463471 BC. putStrLn expected
464472 BC. putStrLn " >>>>>>> ACTUAL"
465473 BC. putStrLn actual
466474 return False
475+
476+ eqSkipWinNewlines :: BC. ByteString -> BC. ByteString -> Bool
477+ eqSkipWinNewlines x1 x2 = go (BC. uncons x1) (BC. uncons x2)
478+ where
479+ go :: Maybe (Char , BC. ByteString ) -> Maybe (Char , BC. ByteString ) -> Bool
480+
481+ -- both reached EOF simultaneously: identical
482+ go Nothing Nothing = True
483+
484+ -- next character in either bytestring is @\r@: skip
485+ go (Just (' \r ' , bs1)) bs2 = go (BC. uncons bs1) bs2
486+ go bs1 (Just (' \r ' , bs2)) = go bs1 (BC. uncons bs2)
487+
488+ -- only one bytestring is EOF: different
489+ go Nothing (Just _) = False
490+ go (Just _) Nothing = False
491+
492+ -- main case: check equality of next words
493+ go (Just (b1, bs1)) (Just (b2, bs2)) =
494+ if b1 == b2
495+ then go (BC. uncons bs1) (BC. uncons bs2)
496+ else False
0 commit comments