summaryrefslogtreecommitdiff
path: root/tests/Static.hs
blob: c891a74e58bd48e5f1a92059b65f4f3a505d35a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
{-# LANGUAGE ViewPatterns #-}
module Main where

-- Static site generator for failing tests
import Data.Algorithm.Diff (getGroupedDiff)
import Data.Algorithm.DiffOutput (ppDiff)

import System.Directory
import System.FilePath

import Test.CommonUtils
import Control.Monad

import Debug.Trace

import Data.List
import System.Environment
import Data.Maybe
import Text.Read

main :: IO ()
main = do
  createDirectoryIfMissing True failuresHtmlDir
  n <- getArgs
  case readMaybe =<< listToMaybe n of
    Nothing -> site 100
    Just k  -> site k

site :: Int -> IO ()
site n = do
  putStrLn $ "Generating site for first: " ++ show n
  failPaths <- filterM doesFileExist =<< (map (failuresDir </>)  . take n <$> getDirectoryContents failuresDir)
  traceShowM failPaths
  fails <- mapM parseFail failPaths
  writeFile origFailuresFile (intercalate "\n" (map getfname fails))
  -- writeFile "failures/failures.html" (makeIndex failPaths)
  writeFile (failuresHtmlDir </> failuresHtmlFile) (makeIndex failPaths)
  let padded = failuresHtmlFile : (map makeFailLink failPaths ++ [failuresHtmlFile])
  let resolved = zipWith (\x (y,z) -> (x, y, z)) padded (zip (tail padded) (tail (tail padded)))
  mapM_ (uncurry page) (zip resolved fails)

makeFailLink :: FilePath -> String
makeFailLink fp = takeFileName fp  <.> "html"

makeIndex :: [FilePath] -> String
makeIndex files =
  intercalate "</br>" (map mkIndexLink files)
  where
    mkIndexLink f = mkLink (takeFileName f <.> "html") f



page :: (FilePath, FilePath, FilePath) -> Failure -> IO ()
page (prev, out, next) (Failure res fname) = do
--  traceM out
  original <- readFile fname
  -- let diff = getDiff (tokenize original) (tokenize res)
  let lres = lines res
  let maxLines = 50000
  let diff = getGroupedDiff (lines original) (take maxLines lres)
  let l = length lres
  if (l > maxLines)
    then  do -- putStrLn ("Skipping: " ++ fname) >> print l
      let resTrunc = (intercalate "\n" $ take maxLines lres)
                  ++ "\n*****************TRUNCATED*******"
      writeFile (failuresHtmlDir </> out) (mkPage fname (ppDiff diff) prev next original resTrunc)
    else
      -- writeFile ("failures" </> out) (mkPage (ppDiff diff) prev next original res)
      writeFile (failuresHtmlDir </> out) (mkPage fname (ppDiff diff) prev next original res)
  where
    tokenize :: String -> [[String]]
    -- tokenize s = map (:[]) . lines $ s

mkPage :: FilePath -> String -> String -> String -> String -> String -> String
mkPage filename diff prev next original printed  =
  intercalate "</br>"
  [mkLink prev "prev"
  , mkLink failuresHtmlFile "home"
  , mkLink next "next"
  , ""
  , "<pre>" ++ filename ++ "</pre>"
  , ""
  , "<pre>" ++ diff ++ "</pre>"
  , "<h2>original</h2>"
  , "<pre>" ++ original ++ "</pre>"
  , "<h2>printed</h2>"
  , "<pre>" ++ printed ++ "</pre>"
  ]

mkLink :: String -> String -> String
mkLink s label =
  "<a href=\"" ++ s ++ "\">" ++ label ++ "</a>"

data Failure = Failure String FilePath

getfname :: Failure -> FilePath
getfname (Failure _ fp) = fp

parseFail :: FilePath -> IO Failure
parseFail fp = do
  res <- lines <$> readFile fp
  let (finalres, head . tail -> fname) = break (=="==============") res
  return (Failure (unlines finalres) fname)