<commit_before>{-# LANGUAGE OverloadedStrings #-}

import Prelude
import Control.Monad
import Data.Int
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Thyme.Time
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import System.Directory
import System.Exit
import System.FilePath
import System.Posix.Redirect
import System.Random

main :: IO ()
main = do
    defaultMainWithHooksArgs simpleUserHooks
        { buildHook = hook }
        [ "build", "--ghc-option=-ddump-rule-firings" ]
    useless

hook :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
hook pd lbi uh bf = do
    -- more reliable way to force a rebuild?
    forM_ ["hi", "o"] $ \ suf -> removeFile $
        buildDir lbi </> "rewrite" </> "rewrite-tmp" </> "Main" <.> suf

    (err, (out, _)) <- redirectStderr . redirectStdout $
        buildHook simpleUserHooks pd lbi uh bf
    let std = T.decodeUtf8 err `T.append` T.decodeUtf8 out

    let fired = foldr ( maybe id (flip (Map.insertWith (+)) (1 :: Int))
            . T.stripPrefix "Rule fired: " ) Map.empty (T.lines std)
    let unmatched = wanted `Map.difference` fired
    case Map.null unmatched of
        True -> mapM_ print (Map.toList $ fired `Map.intersection` wanted)
        False -> do
            putStrLn "Unmatched rules:"
            mapM_ (T.putStrLn . T.append "  ") (Map.keys unmatched)
            exitWith (ExitFailure 1)<commit_msg>tests/rewrite.hs: cater for --enable-split-objs; fixes #4<commit_after>{-# LANGUAGE OverloadedStrings #-}

import Prelude
import Data.Int
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import Data.Thyme.Time
import Distribution.PackageDescription
import Distribution.Simple
import Distribution.Simple.LocalBuildInfo
import Distribution.Simple.Setup
import System.Directory
import System.Exit
import System.FilePath
import System.Posix.Redirect
import System.Random

main :: IO ()
main = do
    defaultMainWithHooksArgs simpleUserHooks
        { buildHook = hook }
        [ "build", "--ghc-option=-ddump-rule-firings" ]
    useless

hook :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()
hook pd lbi uh bf = do
    -- more reliable way to force a rebuild?
    removeDirectoryRecursive (buildDir lbi </> "rewrite" </> "rewrite-tmp")

    (err, (out, _)) <- redirectStderr . redirectStdout $
        buildHook simpleUserHooks pd lbi uh bf
    let std = T.decodeUtf8 err `T.append` T.decodeUtf8 out

    let fired = foldr ( maybe id (flip (Map.insertWith (+)) (1 :: Int))
            . T.stripPrefix "Rule fired: " ) Map.empty (T.lines std)
    let unmatched = wanted `Map.difference` fired
    case Map.null unmatched of
        True -> mapM_ print (Map.toList $ fired `Map.intersection` wanted)
        False -> do
            putStrLn "Unmatched rules:"
            mapM_ (T.putStrLn . T.append "  ") (Map.keys unmatched)
            exitWith (ExitFailure 1)
{'fraction_non_alphanumeric': 0.10529537802265075, 'fraction_numerical': 0.0027548209366391185, 'mean_word_length': 3.1314791403286977, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3465270', 'n_tokens_mistral': 1002, 'n_tokens_neox': 981, 'n_words': 310}