Skip to content

Commit 7aaaa82

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 7c15374 commit 7aaaa82

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

Diff for: .github/workflows/test.yml

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

258258
test_post_job:
259259
if: always()

Diff for: 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.4
5057

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

Diff for: plugins/hls-cabal-fmt-plugin/test/Main.hs

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

78
import qualified Ide.Plugin.CabalFmt as CabalFmt
9+
import System.Directory (findExecutable)
810
import System.FilePath
911
import Test.Hls
1012

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

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

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

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

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

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

0 commit comments

Comments
 (0)