diff --git a/README.md b/README.md index 410ef66..e1db955 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,48 @@ git add . && git commit -m rename - Run `bin/test` to run the test suite. - Run the application without installing: `nix run github:srid/haskell-template` (or `nix run .` from checkout) +## Common workflows + +### Adding tests + +1. Split any logic code out of `Main.hs` into, say, a `Lib.hs`. +1. Correspondingly, add `other-modules: Lib` to the "shared" section of your cabal file. +1. Add `tests/Spec.hs` (example below): + ```haskell + module Main where + + import Lib qualified + import Test.Hspec (describe, hspec, it, shouldContain) + + main :: IO () + main = hspec $ do + describe "Lib.hello" $ do + it "contains the world emoji" $ do + toString Lib.hello `shouldContain` "🌎" + ``` +1. Add the tests stanza to the cabal file: + ```cabal + test-suite tests + import: shared + main-is: Spec.hs + type: exitcode-stdio-1.0 + hs-source-dirs: tests + build-depends: hspec + ``` +1. Update `hie.yaml` accordingly; for example, by adding, + ```yaml + - path: "tests" + component: "test:tests" + ``` +1. Add `bin/test` and `chmod a+x` it: + ```sh + #!/usr/bin/env bash + set -xe + + exec nix develop -i -c ghcid -c "cabal repl test:tests" -T :main + ``` +1. Commit your changes to Git, and test it out by running `bin/test`. + ## Discussions Got questions? Ideas? Suggestions? Post them here: https://github.com/srid/haskell-template/discussions diff --git a/bin/test b/bin/test deleted file mode 100755 index 779f9c5..0000000 --- a/bin/test +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -set -xe - -exec nix develop -i -c ghcid -c "cabal repl test:tests" -T :main diff --git a/haskell-template.cabal b/haskell-template.cabal index ff1ac2a..974d017 100644 --- a/haskell-template.cabal +++ b/haskell-template.cabal @@ -92,16 +92,8 @@ common shared , with-utf8 hs-source-dirs: src - other-modules: Lib default-language: Haskell2010 executable haskell-template import: shared main-is: Main.hs - -test-suite tests - import: shared - main-is: Spec.hs - type: exitcode-stdio-1.0 - hs-source-dirs: tests - build-depends: hspec diff --git a/hie.yaml b/hie.yaml index 8991be5..cebc65d 100644 --- a/hie.yaml +++ b/hie.yaml @@ -2,5 +2,3 @@ cradle: cabal: - path: "src" component: "exe:haskell-template" - - path: "tests" - component: "test:tests" diff --git a/src/Lib.hs b/src/Lib.hs deleted file mode 100644 index 7c55ddf..0000000 --- a/src/Lib.hs +++ /dev/null @@ -1,4 +0,0 @@ -module Lib (hello) where - -hello :: Text -hello = "Hello 🌎" diff --git a/src/Main.hs b/src/Main.hs index 44a1d36..e4b7af5 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,6 +1,5 @@ module Main where -import Lib qualified import Main.Utf8 qualified as Utf8 {- | @@ -12,4 +11,4 @@ main :: IO () main = do -- For withUtf8, see https://serokell.io/blog/haskell-with-utf8 Utf8.withUtf8 $ do - putTextLn Lib.hello + putTextLn "Hello 🌎" diff --git a/tests/Spec.hs b/tests/Spec.hs deleted file mode 100644 index 17e0ee7..0000000 --- a/tests/Spec.hs +++ /dev/null @@ -1,10 +0,0 @@ -module Main where - -import Lib qualified -import Test.Hspec (describe, hspec, it, shouldContain) - -main :: IO () -main = hspec $ do - describe "Lib.hello" $ do - it "contains the world emoji" $ do - toString Lib.hello `shouldContain` "🌎"