Unit test main function: imports itself error

461 views Asked by At

I'm trying to write a (small) executable, setup using Cabal, unit tested using HSpec. Almost all of my code is in a separate module, Library, including the body of main, which I import into my Main module as the run function

-- In src/Hecho.hs
module Main where
import Library(run)

main :: IO ()
main = run

Although the main function is now as short as I think it can be, is there a way I can write a test for it, say to check that it equals the run function, or maybe to test it some other way? The issue is that my spec file defines another Main module, and I don't seem to be able (or at least I can't figure out how) to import anything into it from the other Main module.

For example, if I try the following

-- In test/HechoSpec.hs
module Main where

import Library
import Main
import Test.Hspec

main :: IO ()
main = hspec $ do
  -- Test definitions

Then I get the error:

Module imports form a cycle:
  module `Main' (test/HechoSpec.hs) imports itself

Is there a way to test the main function?

1

There are 1 answers

10
ErikR On

Updated answer: Apparently the question is how to make sure Library.run is the same as main.

The answer is that it's not possible to do that. main is a value of type IO () and there is no Eq defined for IO actions. For instance, this program does not type check:

main = print "Hello"

foo = main

fooEqualsMain = foo == main