Skip to content

Commit

Permalink
feat: Support simple cabal projects without a project.cabal file
Browse files Browse the repository at this point in the history
  • Loading branch information
saep committed Feb 26, 2023
1 parent 5809d97 commit 2615524
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lua/neotest-haskell/cabal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ local function get_package_file(package_root)
end
end

--- A 'simple' cabal project is a haskell project without project.cabal file
--- that contains a '*.cabal' file.
---@async
function cabal.build_command_simple(pos)
logger.debug('Building spec for simple Cabal project...')
local command = {
'cabal',
'test',
}
local test_opts = hspec.get_cabal_test_opts(pos)
return test_opts and vim.list_extend(command, test_opts) or command
end

---@async
function cabal.build_command(package_root, pos)
logger.debug('Building spec for Cabal project...')
Expand Down
6 changes: 5 additions & 1 deletion lua/neotest-haskell/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ function HaskellNeotestAdapter.build_spec(args)
end

local package_root = base.match_package_root_pattern(pos.path)
local project_root = base.match_project_root_pattern(pos.path)
-- If this isn't a stack project or a multi-package cabal project,
-- then the root is also where the package (i.e. '*.cabal' file) is.
local project_root = base.match_project_root_pattern(pos.path) or package_root

if lib.files.exists(project_root .. '/cabal.project') then
return mkCommand(cabal.build_command(package_root, pos))
elseif lib.files.exists(project_root .. '/stack.yaml') and vim.fn.executable('stack') == 1 then
return mkCommand(stack.build_command(project_root, package_root, pos))
elseif lib.files.match_root_pattern('*.cabal')(project_root) then
return mkCommand(cabal.build_command_simple(pos))
end

logger.error('Project is neither a Cabal nor a Stack project.')
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/hspec/cabal/simple/simple.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cabal-version: 3.0
name: simple
version: 0.1.0.0
build-type: Simple
common warnings
ghc-options: -Wall

test-suite simple-test
import: warnings
default-language: Haskell2010
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Spec.hs
build-depends: base >=4.9 && < 5
, hspec ==2.*
, hspec-discover
15 changes: 15 additions & 0 deletions tests/fixtures/hspec/cabal/simple/test/FirstSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module FirstSpec where

import Test.Hspec

spec :: Spec
spec = do
describe "section 1" $ do
it "is a tautology" $ do
True `shouldBe` True
it "assumes that 2 is 1" $ do
2 `shouldBe` (1 :: Integer)

describe "section 2" $ do
it "only contains one test" $ do
sum [1..17] `shouldBe` ((17 * 18) `div` 2 :: Integer)
1 change: 1 addition & 0 deletions tests/fixtures/hspec/cabal/simple/test/Spec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

0 comments on commit 2615524

Please sign in to comment.