-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReportError.hs
More file actions
84 lines (64 loc) · 2.53 KB
/
ReportError.hs
File metadata and controls
84 lines (64 loc) · 2.53 KB
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
-- Report an error in results.json, for Gradescope autograding
{-# LANGUAGE DeriveAnyClass, DeriveGeneric, DuplicateRecordFields, OverloadedStrings,
LambdaCase, ScopedTypeVariables #-}
{-# OPTIONS_GHC -W #-}
{-# LANGUAGE DuplicateRecordFields, OverloadedStrings #-}
module ReportError where
import GHC.Generics
import Data.Generics ( everywhere', mkT )
import Data.Aeson ( ToJSON(toJSON), Value(..), Object )
import Data.Aeson.Encoding ( encodingToLazyByteString, value )
import Data.Aeson.KeyMap as Map
import Data.Text ( Text )
import qualified Data.Text.IO as T
import qualified Data.ByteString.Lazy as B
import qualified System.Directory as D
import System.Environment
----------------------------------------------------------------
-- From Gradescope.hs, copied here to eliminate dependency
data Visibility = Hidden
| AfterDueDate
| AfterPublished
| Visible
deriving Show
instance ToJSON Visibility where
toJSON Hidden = String "hidden"
toJSON AfterDueDate = String "after_due_date"
toJSON AfterPublished = String "after_published"
toJSON Visible = String "visible"
data AGTest = AGTest deriving (Show, Generic, ToJSON )
data AGResult = AGResult { score :: Maybe Double
, execution_time :: Maybe Double
, output :: Text
, visibility :: Visibility
, tests :: [AGTest] }
deriving (Show, Generic, ToJSON)
def_result = AGResult { score = Nothing
, execution_time = Nothing
, output = ""
, visibility = Visible
, tests = [] }
encodeNoNulls :: ToJSON a => a -> B.ByteString
encodeNoNulls = encodingToLazyByteString . value . noNulls . toJSON
noNulls :: Value -> Value
noNulls = everywhere' (mkT remove_null)
where
remove_null :: Object -> Object
remove_null = Map.filter (\case Null -> False; _ -> True)
writeResult :: AGResult -> IO ()
writeResult result = do
let encoded = encodeNoNulls result
let resultsDir = "/autograder/results/"
dirExists <- D.doesDirectoryExist resultsDir
if dirExists then
B.writeFile (resultsDir ++ "results.json") encoded
else
print result
----------------------------------------------------------------
main = do
[error_log] <- getArgs
error_msgs <- T.readFile error_log
let result :: AGResult
result = def_result { output = "Test harness failure:\n" <> error_msgs
, score = Just 0 }
writeResult result