From e25fcf4e3b47e32bf2dc6de8355bb71bd4984087 Mon Sep 17 00:00:00 2001 From: Fendor Date: Sat, 9 Jul 2022 10:47:12 +0200 Subject: [PATCH] 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. --- .github/workflows/test.yml | 2 +- .../hls-cabal-fmt-plugin.cabal | 10 ++++- plugins/hls-cabal-fmt-plugin/test/Main.hs | 40 +++++++++++++++---- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f0721518d19..f858973e766 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -267,7 +267,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() diff --git a/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal b/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal index 5d245e8b494..ad8a8d8ad58 100644 --- a/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal +++ b/plugins/hls-cabal-fmt-plugin/hls-cabal-fmt-plugin.cabal @@ -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 @@ -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 diff --git a/plugins/hls-cabal-fmt-plugin/test/Main.hs b/plugins/hls-cabal-fmt-plugin/test/Main.hs index 21a90898935..04c42726c06 100644 --- a/plugins/hls-cabal-fmt-plugin/test/Main.hs +++ b/plugins/hls-cabal-fmt-plugin/test/Main.hs @@ -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