-
-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathUtil.hs
281 lines (237 loc) · 9.32 KB
/
Util.hs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
{-# LANGUAGE CPP, OverloadedStrings, NamedFieldPuns #-}
module Test.Hls.Util
(
codeActionSupportCaps
, dummyLspFuncs
, flushStackEnvironment
, getHspecFormattedConfig
, ghcVersion, GhcVersion(..)
, hieCommand
, hieCommandExamplePlugin
, hieCommandVomit
, logConfig
, logFilePath
, noLogConfig
, setupBuildToolFiles
, withFileLogging
, withCurrentDirectoryInTmp
)
where
import Control.Monad
import Data.Default
import Data.List (intercalate)
import Data.Maybe
import Language.Haskell.LSP.Core
import Language.Haskell.LSP.Types
import qualified Language.Haskell.LSP.Test as T
import qualified Language.Haskell.LSP.Types.Capabilities as C
import System.Directory
import System.Environment
import System.FilePath
import qualified System.Log.Logger as L
import System.IO.Temp
import Test.Hspec.Runner
import Test.Hspec.Core.Formatters
import Text.Blaze.Renderer.String (renderMarkup)
import Text.Blaze.Internal
noLogConfig :: T.SessionConfig
noLogConfig = T.defaultConfig { T.logMessages = False }
logConfig :: T.SessionConfig
logConfig = T.defaultConfig { T.logMessages = True }
codeActionSupportCaps :: C.ClientCapabilities
codeActionSupportCaps = def { C._textDocument = Just textDocumentCaps }
where
textDocumentCaps = def { C._codeAction = Just codeActionCaps }
codeActionCaps = C.CodeActionClientCapabilities (Just True) (Just literalSupport)
literalSupport = C.CodeActionLiteralSupport def
withFileLogging :: FilePath -> IO a -> IO a
withFileLogging logFile f = do
let logDir = "./test-logs"
logPath = logDir </> logFile
dirExists <- doesDirectoryExist logDir
unless dirExists $ createDirectory logDir
exists <- doesFileExist logPath
when exists $ removeFile logPath
setupLogger (Just logPath) ["hie"] L.DEBUG
f
-- ---------------------------------------------------------------------
setupBuildToolFiles :: IO ()
setupBuildToolFiles = do
forM_ files setupDirectFilesIn
setupDirectFilesIn :: FilePath -> IO ()
setupDirectFilesIn f =
writeFile (f ++ "hie.yaml") hieYamlCradleDirectContents
-- ---------------------------------------------------------------------
files :: [FilePath]
files =
[ "./test/testdata/"
-- , "./test/testdata/addPackageTest/cabal-exe/"
-- , "./test/testdata/addPackageTest/hpack-exe/"
-- , "./test/testdata/addPackageTest/cabal-lib/"
-- , "./test/testdata/addPackageTest/hpack-lib/"
-- , "./test/testdata/addPragmas/"
-- , "./test/testdata/badProjects/cabal/"
-- , "./test/testdata/completion/"
-- , "./test/testdata/definition/"
-- , "./test/testdata/gototest/"
-- , "./test/testdata/redundantImportTest/"
-- , "./test/testdata/wErrorTest/"
]
data GhcVersion
= GHC88
| GHC86
| GHC84
deriving (Eq,Show)
ghcVersion :: GhcVersion
#if (defined(MIN_VERSION_GLASGOW_HASKELL) && (MIN_VERSION_GLASGOW_HASKELL(8,8,0,0)))
ghcVersion = GHC88
#elif (defined(MIN_VERSION_GLASGOW_HASKELL) && (MIN_VERSION_GLASGOW_HASKELL(8,6,0,0)))
ghcVersion = GHC86
#elif (defined(MIN_VERSION_GLASGOW_HASKELL) && (MIN_VERSION_GLASGOW_HASKELL(8,4,0,0)))
ghcVersion = GHC84
#endif
logFilePath :: String
logFilePath = "hie-" ++ show ghcVersion ++ ".log"
-- | The command to execute the version of hie for the current compiler.
--
-- Both @stack test@ and @cabal new-test@ setup the environment so @hie@ is
-- on PATH. Cabal seems to respond to @build-tool-depends@ specifically while
-- stack just puts all project executables on PATH.
hieCommand :: String
-- hieCommand = "hie --lsp --bios-verbose -d -l test-logs/" ++ logFilePath
-- hieCommand = "haskell-language-server --lsp"
-- hieCommand = "haskell-language-server --lsp --test --shake-profiling=test-logs/" ++ logFilePath
hieCommand = "haskell-language-server --lsp -d -l test-logs/" ++ logFilePath
hieCommandVomit :: String
hieCommandVomit = hieCommand ++ " --vomit"
hieCommandExamplePlugin :: String
hieCommandExamplePlugin = hieCommand ++ " --example"
-- ---------------------------------------------------------------------
hieYamlCradleDirectContents :: String
hieYamlCradleDirectContents = unlines
[ "# WARNING: THIS FILE IS AUTOGENERATED IN test/utils/TestUtils.hs. IT WILL BE OVERWRITTEN ON EVERY TEST RUN"
, "cradle:"
, " direct:"
, " arguments:"
, " - -i."
]
-- ---------------------------------------------------------------------
getHspecFormattedConfig :: String -> IO Config
getHspecFormattedConfig name = do
-- https://circleci.com/docs/2.0/env-vars/#built-in-environment-variables
isCI <- isJust <$> lookupEnv "CI"
-- Only use the xml formatter on CI since it hides console output
if isCI
then do
let subdir = "test-results" </> name
createDirectoryIfMissing True subdir
return $ defaultConfig { configFormatter = Just xmlFormatter
, configOutputFile = Right $ subdir </> "results.xml"
}
else return defaultConfig
-- | A Hspec formatter for CircleCI.
-- Originally from https://github.com/LeastAuthority/hspec-jenkins
xmlFormatter :: Formatter
xmlFormatter = silent {
headerFormatter = do
writeLine "<?xml version='1.0' encoding='UTF-8'?>"
writeLine "<testsuite>"
, exampleSucceeded
, exampleFailed
, examplePending
, footerFormatter = writeLine "</testsuite>"
}
where
#if MIN_VERSION_hspec(2,5,0)
exampleSucceeded path _ =
#else
exampleSucceeded path =
#endif
writeLine $ renderMarkup $ testcase path ""
#if MIN_VERSION_hspec(2,5,0)
exampleFailed path _ err =
#else
exampleFailed path (Left err) =
writeLine $ renderMarkup $ testcase path $
failure ! message (show err) $ ""
exampleFailed path (Right err) =
#endif
writeLine $ renderMarkup $ testcase path $
failure ! message (reasonAsString err) $ ""
#if MIN_VERSION_hspec(2,5,0)
examplePending path _ reason =
#else
examplePending path reason =
#endif
writeLine $ renderMarkup $ testcase path $
case reason of
Just desc -> skipped ! message desc $ ""
Nothing -> skipped ""
failure, skipped :: Markup -> Markup
failure = customParent "failure"
skipped = customParent "skipped"
name, className, message :: String -> Attribute
name = customAttribute "name" . stringValue
className = customAttribute "classname" . stringValue
message = customAttribute "message" . stringValue
testcase :: Path -> Markup -> Markup
testcase (xs,x) = customParent "testcase" ! name x ! className (intercalate "." xs)
reasonAsString :: FailureReason -> String
reasonAsString NoReason = "no reason given"
reasonAsString (Reason x) = x
reasonAsString (ExpectedButGot Nothing expected got) = "Expected " ++ expected ++ " but got " ++ got
reasonAsString (ExpectedButGot (Just src) expected got) = src ++ " expected " ++ expected ++ " but got " ++ got
#if MIN_VERSION_hspec(2,5,0)
reasonAsString (Error Nothing err ) = show err
reasonAsString (Error (Just s) err) = s ++ show err
#endif
-- ---------------------------------------------------------------------
flushStackEnvironment :: IO ()
flushStackEnvironment = do
-- We need to clear these environment variables to prevent
-- collisions with stack usages
-- See https://github.com/commercialhaskell/stack/issues/4875
unsetEnv "GHC_PACKAGE_PATH"
unsetEnv "GHC_ENVIRONMENT"
unsetEnv "HASKELL_PACKAGE_SANDBOX"
unsetEnv "HASKELL_PACKAGE_SANDBOXES"
-- ---------------------------------------------------------------------
dummyLspFuncs :: Default a => LspFuncs a
dummyLspFuncs = LspFuncs { clientCapabilities = def
, config = return (Just def)
, sendFunc = const (return ())
, getVirtualFileFunc = const (return Nothing)
, persistVirtualFileFunc = \uri -> return (uriToFilePath (fromNormalizedUri uri))
, reverseFileMapFunc = return id
, publishDiagnosticsFunc = mempty
, flushDiagnosticsBySourceFunc = mempty
, getNextReqId = pure (IdInt 0)
, rootPath = Nothing
, getWorkspaceFolders = return Nothing
, withProgress = \_ _ f -> f (const (return ()))
, withIndefiniteProgress = \_ _ f -> f
}
-- | Like 'withCurrentDirectory', but will copy the directory over to the system
-- temporary directory first to avoid haskell-language-server's source tree from
-- interfering with the cradle
withCurrentDirectoryInTmp :: FilePath -> IO a -> IO a
withCurrentDirectoryInTmp dir f =
withTempCopy dir $ \newDir ->
withCurrentDirectory newDir f
withTempCopy :: FilePath -> (FilePath -> IO a) -> IO a
withTempCopy srcDir f = do
withSystemTempDirectory "hls-test" $ \newDir -> do
copyDir srcDir newDir
f newDir
copyDir :: FilePath -> FilePath -> IO ()
copyDir src dst = do
cnts <- listDirectory src
forM_ cnts $ \file -> do
unless (file `elem` ignored) $ do
let srcFp = src </> file
dstFp = dst </> file
isDir <- doesDirectoryExist srcFp
if isDir
then createDirectory dstFp >> copyDir srcFp dstFp
else copyFile srcFp dstFp
where ignored = ["dist", "dist-newstyle", ".stack-work"]