Skip to content

Commit

Permalink
Add 'isolateTests' cabal flag to make plugin install cabal-fmt
Browse files Browse the repository at this point in the history
For CI, we want to run the tests with a specific cabal-fmt version,
installed automatically by cabal.
However, locally, we might want to test with a locally installed
cabal-fmt version.
This flag allows developers to either let cabal install the
build-tool-depends or install a fitting version locally.
  • Loading branch information
fendor committed Oct 11, 2022
1 parent 06c349e commit 2ae9f02
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ jobs:
## version needs to be limited since the tests depend on cabal-fmt which only builds using specific ghc versions
- if: matrix.test && matrix.ghc == '8.10.7'
name: Test hls-cabal-fmt-plugin test suite
run: cabal test hls-cabal-fmt-plugin --test-options="$TEST_OPTS" || cabal test hls-cabal-fmt-plugin --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-cabal-fmt-plugin --test-options="$TEST_OPTS"
run: cabal test hls-cabal-fmt-plugin --flag=isolateTests --test-options="$TEST_OPTS" || cabal test hls-cabal-fmt-plugin --flag=isolateTests --test-options="$TEST_OPTS" || LSP_TEST_LOG_COLOR=0 LSP_TEST_LOG_MESSAGES=true LSP_TEST_LOG_STDERR=true cabal test hls-cabal-fmt-plugin --flag=isolateTests --test-options="$TEST_OPTS"

test_post_job:
if: always()
Expand Down
10 changes: 9 additions & 1 deletion plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ category: Development
build-type: Simple
extra-source-files: LICENSE

flag isolateTests
description: Should tests search for 'cabal-fmt' on the $PATH or shall we install it via build-tool-depends?
-- By default, search on the PATH
default: False
manual: True

common warnings
ghc-options: -Wall

Expand Down Expand Up @@ -44,8 +50,10 @@ test-suite tests
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
, base
, directory
, filepath
, hls-cabal-fmt-plugin
, hls-test-utils ^>=1.4

build-tool-depends: cabal-fmt:cabal-fmt ^>=0.1.6
if flag(isolateTests)
build-tool-depends: cabal-fmt:cabal-fmt ^>=0.1.6
40 changes: 32 additions & 8 deletions plugins/hls-cabal-fmt-plugin/test/Main.hs
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Main
( main
) where

import qualified Ide.Plugin.CabalFmt as CabalFmt
import System.Directory (findExecutable)
import System.FilePath
import Test.Hls

data CabalFmtFound = Found | NotFound

isTestIsolated :: Bool
#if isolateTests
isTestIsolated = True
#else
isTestIsolated = False
#endif

isCabalFmtFound :: IO CabalFmtFound
isCabalFmtFound = case isTestIsolated of
True -> pure Found
False-> do
cabalFmt <- findExecutable "cabal-fmt"
pure $ maybe NotFound (const Found) cabalFmt

main :: IO ()
main = defaultTestRunner tests
main = do
foundCabalFmt <- isCabalFmtFound
defaultTestRunner (tests foundCabalFmt)

cabalFmtPlugin :: PluginDescriptor IdeState
cabalFmtPlugin = CabalFmt.descriptor mempty "cabal-fmt"

tests :: TestTree
tests = testGroup "cabal-fmt"
[ cabalFmtGolden "formats a simple document" "simple_testdata" "formatted_document" $ \doc -> do
tests :: CabalFmtFound -> TestTree
tests found = testGroup "cabal-fmt"
[ cabalFmtGolden found "formats a simple document" "simple_testdata" "formatted_document" $ \doc -> do
formatDoc doc (FormattingOptions 2 True Nothing Nothing Nothing)

, cabalFmtGolden "formats a document with expand:src comment" "commented_testdata" "formatted_document" $ \doc -> do
, cabalFmtGolden found "formats a document with expand:src comment" "commented_testdata" "formatted_document" $ \doc -> do
formatDoc doc (FormattingOptions 2 True Nothing Nothing Nothing)

, cabalFmtGolden "formats a document with lib information" "lib_testdata" "formatted_document" $ \doc -> do
, cabalFmtGolden found "formats a document with lib information" "lib_testdata" "formatted_document" $ \doc -> do
formatDoc doc (FormattingOptions 10 True Nothing Nothing Nothing)
]

cabalFmtGolden :: TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
cabalFmtGolden title path desc = goldenWithCabalDocFormatter cabalFmtPlugin "cabal-fmt" conf title testDataDir path desc "cabal"
cabalFmtGolden :: CabalFmtFound -> TestName -> FilePath -> FilePath -> (TextDocumentIdentifier -> Session ()) -> TestTree
cabalFmtGolden NotFound title _ _ _ =
testCase title $
assertFailure $ "Couldn't find cabal-fmt on PATH or this is not an isolated run. "
<> "Use cabal flag 'isolateTests' to make it isolated or install cabal-fmt locally."
cabalFmtGolden Found title path desc act = goldenWithCabalDocFormatter cabalFmtPlugin "cabal-fmt" conf title testDataDir path desc "cabal" act
where
conf = def

Expand Down

0 comments on commit 2ae9f02

Please sign in to comment.