Skip to content

Commit

Permalink
Reduce duplication in GhciSpec.hs
Browse files Browse the repository at this point in the history
Deduplicate code that I'll have to change to fix compilation on
Windows (and maybe a few other things).
  • Loading branch information
Blaisorblade committed Aug 12, 2016
1 parent 26ffffc commit 81105cd
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 deletions src/test/Stack/GhciSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,107 @@ import Stack.Types.Version
import Test.Hspec
import NeatInterpolation
import Path
import Path.Extra (pathToText)

import Stack.Ghci
import Stack.Ghci.Script (scriptToLazyByteString)

textToLazy :: Text -> LBS.ByteString
textToLazy = LBS.fromStrict . T.encodeUtf8

projDirA, projDirB :: Path Abs Dir
projDirA = $(mkAbsDir "/Users/someone/src/project-a")
projDirB = $(mkAbsDir "/Users/someone/src/project-b")

relFile :: Path Rel File
relFile = $(mkRelFile "exe/Main.hs")

absFile :: Path Abs File
absFile = projDirA </> relFile

projDirAT, projDirBT, relFileT, absFileT :: Text
projDirAT = pathToText projDirA
projDirBT = pathToText projDirB
relFileT = pathToText relFile
absFileT = pathToText absFile

spec :: Spec
spec = do
describe "GHCi" $ do
describe "Script rendering" $ do
describe "should render GHCi scripts" $ do
it "with one library package" $ do
let res = scriptToLazyByteString $ renderScriptGhci packages_singlePackage Nothing
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 ghciScript_projectWithLib)
res `shouldBe` textToLazy ghciScript_projectWithLib

it "with one main package" $ do
let res = scriptToLazyByteString $ renderScriptGhci []
(Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 ghciScript_projectWithMain)
(Just absFile)
res `shouldBe` textToLazy ghciScript_projectWithMain

it "with one library and main package" $ do
let res = scriptToLazyByteString $ renderScriptGhci packages_singlePackage
(Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 ghciScript_projectWithLibAndMain)
(Just absFile)
res `shouldBe` textToLazy ghciScript_projectWithLibAndMain

it "with multiple library packages" $ do
let res = scriptToLazyByteString $ renderScriptGhci packages_multiplePackages Nothing
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 ghciScript_multipleProjectsWithLib)
res `shouldBe` textToLazy ghciScript_multipleProjectsWithLib

describe "should render intero scripts" $ do
it "with one library package" $ do
let res = scriptToLazyByteString $ renderScriptIntero packages_singlePackage Nothing
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 interoScript_projectWithLib)
res `shouldBe` textToLazy interoScript_projectWithLib

it "with one main package" $ do
let res = scriptToLazyByteString $ renderScriptIntero packages_singlePackage
(Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 interoScript_projectWithMain)
(Just absFile)
res `shouldBe` textToLazy interoScript_projectWithMain

it "with one library and main package" $ do
let res = scriptToLazyByteString $ renderScriptIntero packages_singlePackage
(Just $(mkAbsFile "/Users/someone/src/project-a/exe/Main.hs"))
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 interoScript_projectWithLibAndMain)
(Just absFile)
res `shouldBe` textToLazy interoScript_projectWithLibAndMain

it "with multiple library packages" $ do
let res = scriptToLazyByteString $ renderScriptIntero packages_multiplePackages Nothing
res `shouldBe` (LBS.fromStrict $ T.encodeUtf8 interoScript_multipleProjectsWithLib)
res `shouldBe` textToLazy interoScript_multipleProjectsWithLib

-- Exptected Intero scripts

interoScript_projectWithLib :: Text
interoScript_projectWithLib = [text|
:cd-ghc /Users/someone/src/project-a/
:cd-ghc $projDirAT
:add Lib.A
:module + Lib.A

|]

interoScript_projectWithMain :: Text
interoScript_projectWithMain = [text|
:cd-ghc /Users/someone/src/project-a/
:cd-ghc $projDirAT
:add Lib.A
:cd-ghc /Users/someone/src/project-a/
:add /Users/someone/src/project-a/exe/Main.hs
:cd-ghc $projDirAT
:add $absFileT
:module + Lib.A

|]

interoScript_projectWithLibAndMain :: Text
interoScript_projectWithLibAndMain = [text|
:cd-ghc /Users/someone/src/project-a/
:cd-ghc $projDirAT
:add Lib.A
:cd-ghc /Users/someone/src/project-a/
:add /Users/someone/src/project-a/exe/Main.hs
:cd-ghc $projDirAT
:add $absFileT
:module + Lib.A

|]

interoScript_multipleProjectsWithLib :: Text
interoScript_multipleProjectsWithLib = [text|
:cd-ghc /Users/someone/src/project-a/
:cd-ghc $projDirAT
:add Lib.A
:cd-ghc /Users/someone/src/project-b/
:cd-ghc $projDirBT
:add Lib.B
:module + Lib.A Lib.B

Expand All @@ -114,15 +134,15 @@ ghciScript_projectWithLib = [text|

ghciScript_projectWithMain :: Text
ghciScript_projectWithMain = [text|
:add /Users/someone/src/project-a/exe/Main.hs
:add $absFileT
:module +

|]

ghciScript_projectWithLibAndMain :: Text
ghciScript_projectWithLibAndMain = [text|
:add Lib.A
:add /Users/someone/src/project-a/exe/Main.hs
:add $absFileT
:module + Lib.A

|]
Expand All @@ -140,14 +160,14 @@ ghciScript_multipleProjectsWithLib = [text|
ghciLegacyScript_projectWithMain :: Text
ghciLegacyScript_projectWithMain = [text|
:add
:add /Users/someone/src/project-a/exe/Main.hs
:add $absFileT
:module +
|]

ghciLegacyScript_projectWithLibAndMain :: Text
ghciLegacyScript_projectWithLibAndMain = [text|
:add Lib.A
:add /Users/someone/src/project-a/exe/Main.hs
:add $absFileT
:module + Lib.A
|]

Expand All @@ -164,7 +184,7 @@ packages_singlePackage :: [GhciPkgInfo]
packages_singlePackage =
[ GhciPkgInfo
{ ghciPkgModules = S.fromList [fromString "Lib.A"]
, ghciPkgDir = $(mkAbsDir "/Users/someone/src/project-a")
, ghciPkgDir = projDirA
, ghciPkgName = $(mkPackageName "package-a")
, ghciPkgOpts = []
, ghciPkgModFiles = S.empty
Expand Down Expand Up @@ -196,7 +216,7 @@ packages_multiplePackages :: [GhciPkgInfo]
packages_multiplePackages =
[ GhciPkgInfo
{ ghciPkgModules = S.fromList [fromString "Lib.A"]
, ghciPkgDir = $(mkAbsDir "/Users/someone/src/project-a")
, ghciPkgDir = projDirA
, ghciPkgName = $(mkPackageName "package-a")
, ghciPkgOpts = []
, ghciPkgModFiles = S.empty
Expand Down Expand Up @@ -224,7 +244,7 @@ packages_multiplePackages =
}
, GhciPkgInfo
{ ghciPkgModules = S.fromList [fromString "Lib.B"]
, ghciPkgDir = $(mkAbsDir "/Users/someone/src/project-b")
, ghciPkgDir = projDirB
, ghciPkgName = $(mkPackageName "package-b")
, ghciPkgOpts = []
, ghciPkgModFiles = S.empty
Expand Down

0 comments on commit 81105cd

Please sign in to comment.