Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request #138 from matsubara0507/fix-invalid-byte-sequence
Browse files Browse the repository at this point in the history
Avoid errors on non UTF-8 Windows
  • Loading branch information
lwm authored Feb 26, 2018
2 parents 4a327a4 + 17863f6 commit 84d655a
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions library/Test/Tasty/Discover.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
-- | Automatic test discovery and runner for the tasty framework.

module Test.Tasty.Discover (
Expand All @@ -11,15 +12,24 @@ module Test.Tasty.Discover (
, showTests
) where

import Data.List (dropWhileEnd, intercalate, isPrefixOf,
nub, stripPrefix)
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
import System.FilePath (pathSeparator, takeDirectory)
import System.FilePath.Glob (compile, globDir1, match)
import Test.Tasty.Config (Config (..), GlobPattern)
import Test.Tasty.Generator (Generator (..), Test (..), generators,
getGenerators, mkTest, showSetup)
import Data.List (dropWhileEnd, intercalate,
isPrefixOf, nub, stripPrefix)
import qualified Data.Map.Strict as M
import Data.Maybe (fromMaybe)
#if defined(mingw32_HOST_OS)
import GHC.IO.Encoding.CodePage (mkLocaleEncoding)
import GHC.IO.Encoding.Failure (CodingFailureMode (TransliterateCodingFailure))
import GHC.IO.Handle (hGetContents, hSetEncoding)
#else
import GHC.IO.Handle (hGetContents)
#endif
import System.FilePath (pathSeparator, takeDirectory)
import System.FilePath.Glob (compile, globDir1, match)
import System.IO (IOMode (ReadMode), openFile)
import Test.Tasty.Config (Config (..), GlobPattern)
import Test.Tasty.Generator (Generator (..), Test (..),
generators, getGenerators, mkTest,
showSetup)

-- | Main function generator, along with all the boilerplate which
-- which will run the discovered tests.
Expand Down Expand Up @@ -75,7 +85,12 @@ findTests src config = do
concat <$> traverse (extract directory) filtered
where
extract directory filePath = do
extractTests (dropDirectory directory filePath) <$> readFile filePath
h <- openFile filePath ReadMode
#if defined(mingw32_HOST_OS)
-- Avoid internal error: hGetContents: invalid argument (invalid byte sequence)' non UTF-8 Windows
hSetEncoding h $ mkLocaleEncoding TransliterateCodingFailure
#endif
extractTests (dropDirectory directory filePath) <$> hGetContents h
dropDirectory directory filePath = fromMaybe filePath $
stripPrefix (directory ++ [pathSeparator]) filePath

Expand Down

0 comments on commit 84d655a

Please sign in to comment.