1

I am trying to run hspec but getting error as "Failed to load interface for Spec file". I tried similar example from github, got same error. Please suggest where I am going wrong...(PS: I am able to run this with stack).

Example from github (https://github.com/FranklinChen/twenty-four-days2015-of-hackage) I have following files:

-- src.HintExample.hs
module HintExample where
import SortWrapper (Sort)
import qualified Language.Haskell.Interpreter as I
import Language.Haskell.Interpreter (OptionVal((:=)))

-- | Dynamically load a 'Sort' implementation from a file.
-- src is needed to pick up our SortWrapper.
-- sort-plugins is a sample user plugins directory
loadSort :: I.MonadInterpreter m =>
        String  -- ^ module name
     -> String  -- ^ function name
     -> m Sort
loadSort moduleName functionName = do
I.set [I.searchPath := ["src", "sort-plugins"]]
I.loadModules [moduleName]
I.setImports [moduleName, "SortWrapper"]
I.interpret (moduleName ++ "." ++ functionName) (I.as :: Sort)

And test file as

-- test.HintExampleSpec.hs
module HintExampleSpec where
import SortWrapper (Sort(Sort))
import HintExample (loadSort)
import qualified Language.Haskell.Interpreter as I
import Test.Hspec (Spec, hspec, describe, it, shouldBe)
import Test.Hspec.QuickCheck (prop)

-- | Required for auto-discovery.
spec :: Spec
spec =
describe "hint" $ do
it "dynamically loads a correct polymorphic sort function" $ do
  Right (Sort ourSort) <-
    I.runInterpreter (loadSort "OurSorter" "ourSort")
  ourSort "ebcad" `shouldBe` "abcde"
  ourSort [1 :: Int, 5, 4, 3, 7] `shouldBe` [1, 3, 4, 5, 7]
it "dynamically loads a wrong (only head) sort function" $ do
  Right (Sort onlyHead) <-
    I.runInterpreter (loadSort "OurSorter" "onlyHead")
  onlyHead "ebcad" `shouldBe` "e"
  onlyHead [True, False] `shouldBe` [True]

main :: IO ()
main = hspec spec

Spec file (Spec.hs) in test directory as:

{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

running as: runhaskell test/Spec.hs

Manvi
  • 1,136
  • 2
  • 18
  • 41
  • 1
    You're able to run this with `stack test`, but not with `runhaskell`? Well, there are two things that come immediately to mind: 1) you might be running `Spec.hs` from the wrong directory and 2) `runhaskell` won't know about `HintExample`. – Zeta Oct 03 '16 at 07:29
  • Spec.hs is in test directory with HintExampleSpec.hs and I am running it from main project directory as " runhaskell test/Spec.hs". And HintExample is in src directory. Kindly let me know if my project structure is not correct.. – Manvi Oct 03 '16 at 14:00
  • I am also trying to run simple project with file and spec file and I am getting this error (no cabal or stack files in folder). I expect global installation of hspec by cabal should do it and allow me to import Test.Hspec but it does not – kirhgoff Nov 11 '19 at 09:47

0 Answers0