Skip to content

Commit 0f4dd67

Browse files
Copilotsoulomoon
andcommitted
Refactor test helpers to use VirtualFileTree instead of manual file copying
Co-authored-by: soulomoon <[email protected]>
1 parent f4a8e08 commit 0f4dd67

File tree

1 file changed

+16
-32
lines changed
  • plugins/hls-refactor-plugin/test

1 file changed

+16
-32
lines changed

plugins/hls-refactor-plugin/test/Main.hs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,14 @@ import Development.IDE.GHC.Util
2626
import Development.IDE.Plugin.Completions.Types (extendImportCommandId)
2727
import Development.IDE.Test
2828
import Development.IDE.Types.Location
29-
import Development.Shake (getDirectoryFilesIO)
3029
import qualified Language.LSP.Protocol.Lens as L
3130
import Language.LSP.Protocol.Message
3231
import Language.LSP.Protocol.Types hiding
3332
(SemanticTokensEdit (_start),
3433
mkRange)
3534
import Language.LSP.Test
36-
import System.Directory
3735
import System.FilePath
38-
import qualified System.IO.Extra
39-
import System.IO.Extra hiding (withTempDir)
36+
import System.IO.Extra (readFileUtf8)
4037
import System.Time.Extra
4138
import Test.Tasty
4239
import Test.Tasty.HUnit
@@ -45,6 +42,7 @@ import Text.Regex.TDFA ((=~))
4542

4643
import Development.IDE.Plugin.CodeAction (matchRegExMultipleImports)
4744
import Test.Hls
45+
import qualified Test.Hls.FileSystem as FS
4846

4947
import qualified Development.IDE.GHC.ExactPrint
5048
import Development.IDE.Plugin.CodeAction (NotInScope (..))
@@ -4031,40 +4029,26 @@ testSessionWithExtraFiles :: HasCallStack => FilePath -> TestName -> (FilePath -
40314029
testSessionWithExtraFiles prefix name = testCase name . runWithExtraFiles prefix
40324030

40334031
runWithExtraFiles :: HasCallStack => FilePath -> (FilePath -> Session a) -> IO a
4034-
runWithExtraFiles prefix s = withTempDir $ \dir -> do
4035-
copyTestDataFiles dir prefix
4036-
runInDir dir (s dir)
4037-
4038-
copyTestDataFiles :: HasCallStack => FilePath -> FilePath -> IO ()
4039-
copyTestDataFiles dir prefix = do
4040-
-- Copy all the test data files to the temporary workspace
4041-
testDataFiles <- getDirectoryFilesIO ("plugins/hls-refactor-plugin/test/data" </> prefix) ["//*"]
4042-
for_ testDataFiles $ \f -> do
4043-
createDirectoryIfMissing True $ dir </> takeDirectory f
4044-
copyFile ("plugins/hls-refactor-plugin/test/data" </> prefix </> f) (dir </> f)
4045-
4046-
run :: Session a -> IO a
4047-
run s = run' (const s)
4048-
4049-
run' :: (FilePath -> Session a) -> IO a
4050-
run' s = withTempDir $ \dir -> runInDir dir (s dir)
4051-
4052-
runInDir :: FilePath -> Session a -> IO a
4053-
runInDir dir act =
4032+
runWithExtraFiles prefix s =
40544033
runSessionWithTestConfig def
4055-
{ testDirLocation = Left dir
4034+
{ testDirLocation = Right $ mkVirtualFileTreeWithPrefix prefix
40564035
, testPluginDescriptor = refactorPlugin
40574036
, testConfigCaps = lspTestCaps }
4058-
$ const act
4037+
s
4038+
4039+
mkVirtualFileTreeWithPrefix :: FilePath -> FS.VirtualFileTree
4040+
mkVirtualFileTreeWithPrefix prefix =
4041+
FS.mkVirtualFileTree ("plugins/hls-refactor-plugin/test/data") [FS.copyDir prefix]
4042+
4043+
run :: Session a -> IO a
4044+
run s = runSessionWithTestConfig def
4045+
{ testDirLocation = Right $ FS.mkVirtualFileTree "" []
4046+
, testPluginDescriptor = refactorPlugin
4047+
, testConfigCaps = lspTestCaps }
4048+
(const s)
40594049

40604050
lspTestCaps :: ClientCapabilities
40614051
lspTestCaps = fullLatestClientCaps { _window = Just $ WindowClientCapabilities (Just True) Nothing Nothing }
40624052

40634053
pattern R :: UInt -> UInt -> UInt -> UInt -> Range
40644054
pattern R x y x' y' = Range (Position x y) (Position x' y')
4065-
4066-
-- | Version of 'System.IO.Extra.withTempDir' that canonicalizes the path
4067-
-- Which we need to do on macOS since the $TMPDIR can be in @/private/var@ or
4068-
-- @/var@
4069-
withTempDir :: (FilePath -> IO a) -> IO a
4070-
withTempDir f = System.IO.Extra.withTempDir $ (canonicalizePath >=> f)

0 commit comments

Comments
 (0)