<commit_before>{-# LANGUAGE CPP, FlexibleContexts, OverloadedStrings, TupleSections, ScopedTypeVariables, ExtendedDefaultRules, LambdaCase #-}

module Main where

import           Control.Applicative
import           Control.Monad
import           Control.Monad.IO.Class
import           Control.Lens (over, _1)
import           Control.Concurrent.MVar
import           Control.Concurrent
import           Data.Char (isLower, toLower, isDigit, isSpace)
import           Data.IORef
import qualified Data.HashMap.Strict as HM
import           Data.List (partition, isPrefixOf, isInfixOf)
import           Data.Maybe
import           Data.Monoid
import           Data.Traversable (sequenceA)
import qualified Data.ByteString      as B
import qualified Data.ByteString.Lazy as BL
import           Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import           Data.Time.Clock (getCurrentTime, diffUTCTime)
import           Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import           Data.Traversable (traverse)
import           Filesystem (removeTree, isFile, getWorkingDirectory, createDirectory, copyFile)
import           Filesystem.Path ( replaceExtension, basename, directory, extension, addExtension
                                 , filename, addExtensions, dropExtensions)
import           Filesystem.Path.CurrentOS (fromText, toText, encodeString)
import           Prelude hiding (FilePath)
import qualified Prelude
import           Shelly
import           System.Directory (doesFileExist, getCurrentDirectory, findExecutable)
import           System.Environment (getArgs, getEnv)
import           System.Exit
import           System.IO hiding (FilePath)
import           System.IO.Error
import           System.Process ( createProcess, proc, CreateProcess(..), StdStream(..)
                                , terminateProcess, waitForProcess, readProcessWithExitCode
                                , ProcessHandle )
import           System.Random (randomRIO)
import           System.Timeout (timeout)
import           Test.Framework
import           Test.Framework.Providers.HUnit (testCase)
import           Test.HUnit.Base (assertBool, assertFailure, assertEqual, Assertion)
import           Test.HUnit.Lang (HUnitFailure(..), formatFailureReason)
import qualified Data.Yaml as Yaml
import           Data.Yaml (FromJSON(..), Value(..), (.:), (.:?), (.!=))
import           Data.Default
import           Foreign.C.Error (ePIPE, Errno(..))
import           Control.DeepSeq
import           GHC.IO.Exception(IOErrorType(..), IOException(..))
import qualified Control.Exception as C
import           Text.Read (readMaybe)
import           Options.Applicative
import           Options.Applicative.Common
import           Options.Applicative.Types
import           Options.Applicative.Internal
import           Options.Applicative.Help hiding ((</>), fullDesc)
import qualified Options.Applicative.Help as H

import qualified Server
import qualified Client
import           Types

default (Text)

-- | path containing the test cases and data files
getTestDir :: FilePath -> IO FilePath
#ifdef STANDALONE
getTestDir ghcjs = do
  (ec, libDir, _) <- readProcessWithExitCode (encodeString ghcjs) ["--print-libdir"] ""
  when (ec /= ExitSuccess) (error "could not determine GHCJS installation directory")
  let testDir = fromString (trim libDir) </> "test"
  e <- doesFileExist (encodeString $ testDir </> "tests.yaml")
  when (not e) (error $ "test suite not found in " ++ toStringIgnore testDir ++ ", GHCJS might have been installed without tests")
  return testDir
#else
getTestDir _ = do
  testDir <- (</> "test") . fromString <$> getCurrentDirectory
  e <- doesFileExist (encodeString $ testDir </> "tests.yaml")
  when (not e) (error $ "test suite not found in " ++ toStringIgnore testDir)
  return testDir
#endif

main :: IO ()
main = shellyE . silently . withTmpDir $ liftIO . setupTests

showParseError :: ParseError -> String
showParseError (ErrorMsg xs)          = "error: " ++ xs
showParseError (InfoMsg xs)           = "info: " ++ xs
showParseError ShowHelpText           = "help!"
showParseError UnknownError           = "unknown"
showParseError (MissingError {})      = "missing"
showParseError (ExpectsArgError xs)   = "expecting argument: " ++ xs
showParseError (UnexpectedError xs _) = "unexpected: " ++ xs

setupTests :: FilePath -> IO ()
setupTests tmpDir = do<commit_msg>use line buffering in test runner<commit_after>{-# LANGUAGE CPP, FlexibleContexts, OverloadedStrings, TupleSections, ScopedTypeVariables, ExtendedDefaultRules, LambdaCase #-}

module Main where

import           Control.Applicative
import           Control.Monad
import           Control.Monad.IO.Class
import           Control.Lens (over, _1)
import           Control.Concurrent.MVar
import           Control.Concurrent
import           Data.Char (isLower, toLower, isDigit, isSpace)
import           Data.IORef
import qualified Data.HashMap.Strict as HM
import           Data.List (partition, isPrefixOf, isInfixOf)
import           Data.Maybe
import           Data.Monoid
import           Data.Traversable (sequenceA)
import qualified Data.ByteString      as B
import qualified Data.ByteString.Lazy as BL
import           Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as TL
import           Data.Time.Clock (getCurrentTime, diffUTCTime)
import           Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
import           Data.Traversable (traverse)
import           Filesystem (removeTree, isFile, getWorkingDirectory, createDirectory, copyFile)
import           Filesystem.Path ( replaceExtension, basename, directory, extension, addExtension
                                 , filename, addExtensions, dropExtensions)
import           Filesystem.Path.CurrentOS (fromText, toText, encodeString)
import           Prelude hiding (FilePath)
import qualified Prelude
import           Shelly
import           System.Directory (doesFileExist, getCurrentDirectory, findExecutable)
import           System.Environment (getArgs, getEnv)
import           System.Exit
import           System.IO hiding (FilePath)
import           System.IO.Error
import           System.Process ( createProcess, proc, CreateProcess(..), StdStream(..)
                                , terminateProcess, waitForProcess, readProcessWithExitCode
                                , ProcessHandle )
import           System.Random (randomRIO)
import           System.Timeout (timeout)
import           Test.Framework
import           Test.Framework.Providers.HUnit (testCase)
import           Test.HUnit.Base (assertBool, assertFailure, assertEqual, Assertion)
import           Test.HUnit.Lang (HUnitFailure(..), formatFailureReason)
import qualified Data.Yaml as Yaml
import           Data.Yaml (FromJSON(..), Value(..), (.:), (.:?), (.!=))
import           Data.Default
import           Foreign.C.Error (ePIPE, Errno(..))
import           Control.DeepSeq
import           GHC.IO.Exception(IOErrorType(..), IOException(..))
import qualified Control.Exception as C
import           Text.Read (readMaybe)
import           Options.Applicative
import           Options.Applicative.Common
import           Options.Applicative.Types
import           Options.Applicative.Internal
import           Options.Applicative.Help hiding ((</>), fullDesc)
import qualified Options.Applicative.Help as H

import qualified Server
import qualified Client
import           Types

default (Text)

-- | path containing the test cases and data files
getTestDir :: FilePath -> IO FilePath
#ifdef STANDALONE
getTestDir ghcjs = do
  (ec, libDir, _) <- readProcessWithExitCode (encodeString ghcjs) ["--print-libdir"] ""
  when (ec /= ExitSuccess) (error "could not determine GHCJS installation directory")
  let testDir = fromString (trim libDir) </> "test"
  e <- doesFileExist (encodeString $ testDir </> "tests.yaml")
  when (not e) (error $ "test suite not found in " ++ toStringIgnore testDir ++ ", GHCJS might have been installed without tests")
  return testDir
#else
getTestDir _ = do
  testDir <- (</> "test") . fromString <$> getCurrentDirectory
  e <- doesFileExist (encodeString $ testDir </> "tests.yaml")
  when (not e) (error $ "test suite not found in " ++ toStringIgnore testDir)
  return testDir
#endif

main :: IO ()
main = shellyE . silently . withTmpDir $ liftIO . setupTests

showParseError :: ParseError -> String
showParseError (ErrorMsg xs)          = "error: " ++ xs
showParseError (InfoMsg xs)           = "info: " ++ xs
showParseError ShowHelpText           = "help!"
showParseError UnknownError           = "unknown"
showParseError (MissingError {})      = "missing"
showParseError (ExpectsArgError xs)   = "expecting argument: " ++ xs
showParseError (UnexpectedError xs _) = "unexpected: " ++ xs

setupTests :: FilePath -> IO ()
setupTests tmpDir = do
  hSetBuffering stdout LineBuffering
  hSetBuffering stderr LineBuffering
{'fraction_non_alphanumeric': 0.08921850785919859, 'fraction_numerical': 0.00022138587558113792, 'mean_word_length': 2.818681318681319, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7514179', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2412, 'n_words': 730}