git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
[psc-ide] adds a reset command (#2112)

* [psc-ide] adds a reset command * adds a --no-watch flag to psc-ide-server Specifying this flag will disable the file watcher * keep a single psc-ide process alive during the tests, and use the new reset command to clear loaded modules for the tests * compile the psc-ide support code once

commitb5af366e027a2d0fa5d3348f476d422b7b5291a0 authorChristoph Hegemann <kRITZCREEK@users.noreply.github.com> dateThu 12 May 2016, 21:50 (10 years ago) parentf7114e3
11 files changed+69−79
psc-ide-server/Main.hs+15−22 psc-ide-server/PROTOCOL.md+4−2 psc-ide-server/README.md+1 src/Language/PureScript/Ide.hs+1 src/Language/PureScript/Ide/Command.hs+2 src/Language/PureScript/Ide/State.hs+5 tests/Language/PureScript/Ide/Imports/IntegrationSpec.hs+3−10 tests/Language/PureScript/Ide/Integration.hs+19−10 tests/Language/PureScript/Ide/MatcherSpec.hs+8−16 tests/Language/PureScript/Ide/RebuildSpec.hs+2−17 tests/TestPscIde.hs+9−2
psc-ide-server/Main.hs+15−22
@@ -72,12 +72,13 @@ data Options = Options
7272 { optionsDirectory :: Maybe FilePath
7373 , optionsOutputPath :: FilePath
7474 , optionsPort :: PortID
75+ , optionsNoWatch :: Bool
7576 , optionsDebug :: Bool
7677 }
7778
7879main :: IO ()
7980main = do
80− Options dir outputPath port debug <- execParser opts
81+ Options dir outputPath port noWatch debug <- execParser opts
8182 maybe (pure ()) setCurrentDirectory dir
8283 serverState <- newTVarIO emptyPscIdeState
8384 cwd <- getCurrentDirectory
@@ -88,31 +89,23 @@ main = do
8889 (do putStrLn ("Your output directory didn't exist. I'll create it at: " <> fullOutputPath)
8990 createDirectory fullOutputPath
9091 putStrLn "This usually means you didn't compile your project yet."
91− putStrLn "psc-ide needs you to compile your project (for example by running pulp build)"
92− )
92+ putStrLn "psc-ide needs you to compile your project (for example by running pulp build)")
9393
94− _ <- forkFinally (watcher serverState fullOutputPath) print
95− let conf =
96− Configuration
97− {
98− confDebug = debug
99− , confOutputPath = outputPath
100− }
101− let env =
102− PscIdeEnvironment
103− {
104− envStateVar = serverState
105− , envConfiguration = conf
106− }
94+ unless noWatch $
95+ void (forkFinally (watcher serverState fullOutputPath) print)
96+
97+ let conf = Configuration {confDebug = debug, confOutputPath = outputPath}
98+ env = PscIdeEnvironment {envStateVar = serverState, envConfiguration = conf}
10799 startServer port env
108100 where
109101 parser =
110− Options <$>
111− optional (strOption (long "directory" <> short 'd')) <*>
112− strOption (long "output-directory" <> value "output/") <*>
113− (PortNumber . fromIntegral <$>
114− option auto (long "port" <> short 'p' <> value (4242 :: Integer))) <*>
115− switch (long "debug")
102+ Options
103+ <$> optional (strOption (long "directory" <> short 'd'))
104+ <*> strOption (long "output-directory" <> value "output/")
105+ <*> (PortNumber . fromIntegral <$>
106+ option auto (long "port" <> short 'p' <> value (4242 :: Integer)))
107+ <*> switch (long "no-watch")
108+ <*> switch (long "debug")
116109 opts = info (version <*> helper <*> parser) mempty
117110 version = abortOption
118111 (InfoMsg (showVersion Paths.version))
psc-ide-server/PROTOCOL.md+4−2
@@ -407,14 +407,16 @@ Hiding Import(`import Data.Array hiding (filter, filterM, join)`):
407407 }
408408]
409409```
410−### Cwd/Quit
410+### Cwd/Quit/Reset
411411`cwd` returns the working directory of the server(should be your project root).
412412
413413`quit` quits the server.
414414
415+`reset` resets all loaded modules.
416+
415417```json
416418{
417− "command": "cwd|quit"
419+ "command": "cwd|quit|reset"
418420}
419421```
420422
psc-ide-server/README.md+1
@@ -20,6 +20,7 @@ It supports the following options:
2020 project directory. Defaults to `output/`, relative to either the current
2121 directory or the directory specified by `-d`.
2222- `--debug`: Enables some logging meant for debugging
23+- `--no-watch`: Disables the filewatcher
2324- `--version`: Output psc-ide version
2425
2526## Issuing queries
src/Language/PureScript/Ide.hs+1
@@ -90,6 +90,7 @@ handleCommand (Rebuild file) =
9090 rebuildFile file
9191handleCommand Cwd =
9292 TextResult . T.pack <$> liftIO getCurrentDirectory
93+handleCommand Reset = resetPscIdeState *> pure (TextResult "State has been reset.")
9394handleCommand Quit = liftIO exitSuccess
9495
9596findCompletions :: (PscIde m, MonadLogger m) =>
src/Language/PureScript/Ide/Command.hs+2
@@ -63,6 +63,7 @@ data Command
6363 | List { listType :: ListType }
6464 | Rebuild FilePath -- ^ Rebuild the specified file using the loaded externs
6565 | Cwd
66+ | Reset
6667 | Quit
6768
6869data ImportCommand
@@ -104,6 +105,7 @@ instance FromJSON Command where
104105 return $ List (fromMaybe LoadedModules listType')
105106 "cwd" -> return Cwd
106107 "quit" -> return Quit
108+ "reset" -> pure Reset
107109 "load" ->
108110 maybe (pure (Load [] [])) (\params -> do
109111 mods <- params .:? "modules"
src/Language/PureScript/Ide/State.hs+5
@@ -35,6 +35,11 @@ import Language.PureScript.Ide.Reexports
3535import Language.PureScript.Ide.Types
3636import Language.PureScript.Names
3737
38+resetPscIdeState :: PscIde m => m ()
39+resetPscIdeState = do
40+ stateVar <- envStateVar <$> ask
41+ liftIO $ atomically (writeTVar stateVar emptyPscIdeState)
42+
3843getPscIdeState :: (PscIde m) =>
3944 m (M.Map ModuleIdent [ExternDecl])
4045getPscIdeState = do
tests/Language/PureScript/Ide/Imports/IntegrationSpec.hs+3−10
@@ -1,7 +1,7 @@
11{-# LANGUAGE OverloadedStrings #-}
22module Language.PureScript.Ide.Imports.IntegrationSpec where
33
4−import Control.Monad
4+import Control.Monad (void)
55import Data.Text (Text)
66import qualified Data.Text as T
77import qualified Data.Text.IO as TIO
@@ -13,16 +13,9 @@ import System.FilePath
1313
1414setup :: IO ()
1515setup = do
16− Integration.deleteOutputFolder
17− s <- Integration.compileTestProject
18− unless s $ fail "Failed to compile .purs sources"
19− -- Integration.quitServer -- kill a eventually running psc-ide-server instance
20− _ <- Integration.startServer
16+ Integration.reset
2117 mapM_ Integration.loadModuleWithDeps ["ImportsSpec", "ImportsSpec1"]
2218
23−teardown :: IO ()
24−teardown = Integration.quitServer
25−
2619withSupportFiles :: (FilePath -> FilePath -> IO a) -> IO ()
2720withSupportFiles test = do
2821 pdir <- Integration.projectDirectory
@@ -38,7 +31,7 @@ outputFileShouldBe expectation = do
3831 shouldBe (T.lines outRes) expectation
3932
4033spec :: Spec
41−spec = beforeAll_ setup $ afterAll_ teardown $ describe "Adding imports" $ do
34+spec = beforeAll_ setup . describe "Adding imports" $ do
4235 let
4336 sourceFileSkeleton :: [Text] -> [Text]
4437 sourceFileSkeleton importSection =
tests/Language/PureScript/Ide/Integration.hs+19−10
@@ -27,29 +27,30 @@ module Language.PureScript.Ide.Integration
2727 , projectDirectory
2828 , deleteFileIfExists
2929 -- sending commands
30+ , addImport
31+ , addImplicitImport
3032 , loadModule
3133 , loadModuleWithDeps
3234 , getFlexCompletions
3335 , getType
34− , addImport
35− , addImplicitImport
3636 , rebuildModule
37+ , reset
3738 -- checking results
3839 , resultIsSuccess
3940 , parseCompletions
4041 , parseTextResult
4142 ) where
4243
43−import Control.Concurrent (threadDelay)
44+import Control.Concurrent (threadDelay)
4445import Control.Exception
45−import Control.Monad (join, when)
46+import Control.Monad (join, when)
4647import Data.Aeson
4748import Data.Aeson.Types
48−import qualified Data.ByteString.Lazy.UTF8 as BSL
49−import Data.Either (isRight)
50−import Data.Maybe (fromJust)
51−import qualified Data.Text as T
52−import qualified Data.Vector as V
49+import qualified Data.ByteString.Lazy.UTF8 as BSL
50+import Data.Either (isRight)
51+import Data.Maybe (fromJust)
52+import qualified Data.Text as T
53+import qualified Data.Vector as V
5354import Language.PureScript.Ide.Util
5455import System.Directory
5556import System.Exit
@@ -64,7 +65,9 @@ projectDirectory = do
6465startServer :: IO ProcessHandle
6566startServer = do
6667 pdir <- projectDirectory
67− (_, _, _, procHandle) <- createProcess $ (shell "psc-ide-server") {cwd=Just pdir}
68+ -- Turn off filewatching since it creates race condition in a testing environment
69+ (_, _, _, procHandle) <- createProcess $
70+ (shell "psc-ide-server --no-watch") {cwd = Just pdir}
6871 threadDelay 500000 -- give the server 500ms to start up
6972 return procHandle
7073
@@ -128,6 +131,12 @@ quitServer = do
128131 _ <- try $ sendCommand quitCommand :: IO (Either SomeException String)
129132 return ()
130133
134+reset :: IO ()
135+reset = do
136+ let resetCommand = object ["command" .= ("reset" :: String)]
137+ _ <- try $ sendCommand resetCommand :: IO (Either SomeException String)
138+ return ()
139+
131140loadModuleWithDeps :: String -> IO String
132141loadModuleWithDeps m = sendCommand $ load [] [m]
133142
tests/Language/PureScript/Ide/MatcherSpec.hs+8−16
@@ -2,21 +2,22 @@
22
33module Language.PureScript.Ide.MatcherSpec where
44
5+import Control.Monad (void)
56import Data.Text (Text)
7+import qualified Language.PureScript as P
68import Language.PureScript.Ide.Integration
79import Language.PureScript.Ide.Matcher
810import Language.PureScript.Ide.Types
9−import qualified Language.PureScript as P
1011import Test.Hspec
1112
1213value :: Text -> ExternDecl
1314value s = ValueDeclaration s $ P.TypeWildcard $ P.SourceSpan "" (P.SourcePos 0 0) (P.SourcePos 0 0)
1415
1516completions :: [Match]
16−completions = [
17− Match "" $ value "firstResult",
18− Match "" $ value "secondResult",
19− Match "" $ value "fiult"
17+completions =
18+ [ Match "" (value "firstResult")
19+ , Match "" (value "secondResult")
20+ , Match "" (value "fiult")
2021 ]
2122
2223mkResult :: [Int] -> [Match]
@@ -26,15 +27,7 @@ runFlex :: Text -> [Match]
2627runFlex s = runMatcher (flexMatcher s) completions
2728
2829setup :: IO ()
29−setup = do
30− deleteOutputFolder
31− _ <- compileTestProject
32− _ <- startServer
33− _ <- loadModuleWithDeps "Main"
34− return ()
35−
36−teardown :: IO ()
37−teardown = quitServer
30+setup = reset *> void (loadModuleWithDeps "Main")
3831
3932spec :: Spec
4033spec = do
@@ -46,8 +39,7 @@ spec = do
4639 it "scores short matches higher and sorts accordingly" $
4740 runFlex "filt" `shouldBe` mkResult [2, 0]
4841
49− beforeAll_ setup $ afterAll_ teardown $
50− describe "Integration Tests: Flex Matcher" $ do
42+ beforeAll_ setup . describe "Integration Tests: Flex Matcher" $ do
5143 it "doesn't match on an empty string" $ do
5244 cs <- getFlexCompletions ""
5345 cs `shouldBe` []
tests/Language/PureScript/Ide/RebuildSpec.hs+2−17
@@ -1,22 +1,8 @@
11module Language.PureScript.Ide.RebuildSpec where
22
3−import Control.Monad
43import qualified Language.PureScript.Ide.Integration as Integration
5−import Test.Hspec
6−
74import System.FilePath
8−
9−compile :: IO ()
10−compile = do
11− Integration.deleteOutputFolder
12− s <- Integration.compileTestProject
13− unless s $ fail "Failed to compile .purs sources"
14−
15−teardown :: IO ()
16−teardown = Integration.quitServer
17−
18−restart :: IO ()
19−restart = Integration.quitServer *> (void Integration.startServer)
5+import Test.Hspec
206
217shouldBeSuccess :: String -> IO ()
228shouldBeSuccess = shouldBe True . Integration.resultIsSuccess
@@ -25,8 +11,7 @@ shouldBeFailure :: String -> IO ()
2511shouldBeFailure = shouldBe False . Integration.resultIsSuccess
2612
2713spec :: Spec
28−spec = beforeAll_ compile $ afterAll_ teardown $ before_ restart $ do
29− describe "Rebuilding single modules" $ do
14+spec = before_ Integration.reset $ describe "Rebuilding single modules" $ do
3015 it "rebuilds a correct module without dependencies successfully" $ do
3116 _ <- Integration.loadModuleWithDeps "RebuildSpecSingleModule"
3217 pdir <- Integration.projectDirectory
tests/TestPscIde.hs+9−2
@@ -1,7 +1,14 @@
11module TestPscIde where
22
3+import Control.Monad (unless)
4+import Language.PureScript.Ide.Integration
35import qualified PscIdeSpec
4−import Test.Hspec
6+import Test.Hspec
57
68main :: IO ()
7−main = hspec PscIdeSpec.spec
9+main = do
10+ deleteOutputFolder
11+ s <- compileTestProject
12+ unless s $ fail "Failed to compile .purs sources"
13+
14+ withServer (hspec PscIdeSpec.spec)