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 27, 2023
1 parent 2bc4a03 commit 1f8dd4a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support for test files (See [#45](https://github.com/mrcjkb/neotest-haskell/issues/45)).
Running `neotes.run.run(vim.api.nvim_buf_get_name)` will now run a single process for the top-level Hspec node.
- Support for simple cabal projects. These are projects with a single package and no `cabal.project` file.

## [0.2.2] - 2023-01-14
### Fixed
Expand Down
20 changes: 12 additions & 8 deletions lua/neotest-haskell/cabal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@ local function get_package_file(package_root)
end
end

--- @param pos table position from neotest
--- @param package_root string|nil package root directory; can be nil for projects without multiple modules
---@async
function cabal.build_command(package_root, pos)
function cabal.build_command(pos, package_root)
logger.debug('Building spec for Cabal project...')
local command = {
'cabal',
'test',
}
local package_file_path = get_package_file(package_root)
local package_file_name = vim.fn.fnamemodify(package_file_path, ':t')
local package_name = package_file_name:gsub('.cabal', '')
if lib.files.exists(package_file_path) then
table.insert(command, package_name)
else
table.insert(command, 'all')
if package_root then
local package_file_path = get_package_file(package_root)
local package_file_name = vim.fn.fnamemodify(package_file_path, ':t')
local package_name = package_file_name:gsub('.cabal', '')
if lib.files.exists(package_file_path) then
table.insert(command, package_name)
else
table.insert(command, 'all')
end
end
local test_opts = hspec.get_cabal_test_opts(pos)
return test_opts and vim.list_extend(command, test_opts) or command
Expand Down
8 changes: 5 additions & 3 deletions lua/neotest-haskell/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ 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)
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(cabal.build_command(pos, package_root))
elseif lib.files.exists(project_root .. '/stack.yaml') then
return mkCommand(stack.build_command(project_root, package_root, pos))
elseif package_root then
return mkCommand(cabal.build_command(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 1f8dd4a

Please sign in to comment.