Skip to content

Commit 1c38e53

Browse files
committed
Add 'isolateTests' cabal flag to make plugin install cabal-fmt
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.
1 parent 81a11e8 commit 1c38e53

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ jobs:
250250
## version needs to be limited since the tests depend on cabal-fmt which only builds using specific ghc versions
251251
- if: matrix.test && matrix.ghc == '8.10.7'
252252
name: Test hls-cabal-fmt-plugin test suite
253-
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"
253+
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"
254254

255255
test_post_job:
256256
if: always()

plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ category: Development
1414
build-type: Simple
1515
extra-source-files: LICENSE
1616

17+
flag isolateTests
18+
description: Should tests search for 'cabal-fmt' on the $PATH or shall we install it via build-tool-depends?
19+
-- By default, search on the PATH
20+
default: False
21+
manual: True
22+
1723
common warnings
1824
ghc-options: -Wall
1925

@@ -44,8 +50,10 @@ test-suite tests
4450
ghc-options: -threaded -rtsopts -with-rtsopts=-N
4551
build-depends:
4652
, base
53+
, directory
4754
, filepath
4855
, hls-cabal-fmt-plugin
4956
, hls-test-utils ^>=1.3
5057

51-
build-tool-depends: cabal-fmt:cabal-fmt ==0.1.5.1
58+
if flag(isolateTests)
59+
build-tool-depends: cabal-fmt:cabal-fmt ==0.1.5.1

plugins/hls-cabal-fmt-plugin/test/Main.hs

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
11
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE CPP #-}
23
module Main
34
( main
45
) where
56

67
import qualified Ide.Plugin.CabalFmt as CabalFmt
8+
import System.Directory (findExecutable)
79
import System.FilePath
810
import Test.Hls
911

12+
data CabalFmtFound = Found | NotFound
13+
14+
isTestIsolated :: Bool
15+
#if isolateTests
16+
isTestIsolated = True
17+
#else
18+
isTestIsolated = False
19+
#endif
20+
21+
isCabalFmtFound :: IO CabalFmtFound
22+
isCabalFmtFound = case isTestIsolated of
23+
True -> pure Found
24+
False-> do
25+
cabalFmt <- findExecutable "cabal-fmt"
26+
pure $ maybe NotFound (const Found) cabalFmt
27+
1028
main :: IO ()
11-
main = defaultTestRunner tests
29+
main = do
30+
foundCabalFmt <- isCabalFmtFound
31+
defaultTestRunner (tests foundCabalFmt)
1232

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

16-
tests :: TestTree
17-
tests = testGroup "cabal-fmt"
18-
[ cabalFmtGolden "formats a simple document" "simple_testdata" "formatted_document" $ \doc -> do
36+
tests :: CabalFmtFound -> TestTree
37+
tests found = testGroup "cabal-fmt"
38+
[ cabalFmtGolden found "formats a simple document" "simple_testdata" "formatted_document" $ \doc -> do
1939
formatDoc doc (FormattingOptions 2 True Nothing Nothing Nothing)
2040

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

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

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

0 commit comments

Comments
 (0)