From 517c2ec16855c978461fcc845a641db6593d0845 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Thu, 18 Jul 2024 18:24:14 -0700 Subject: [PATCH 1/3] support for running weeder --- cabal.haskell-ci | 5 +++++ src/HaskellCI/Auxiliary.hs | 3 +++ src/HaskellCI/Config/Grammar.hs | 2 ++ src/HaskellCI/Config/Initial.hs | 1 + src/HaskellCI/Config/Type.hs | 1 + src/HaskellCI/GitHub.hs | 19 +++++++++++++++++++ src/HaskellCI/ShVersionRange.hs | 26 ++++++++++++++++++++------ 7 files changed, 51 insertions(+), 6 deletions(-) diff --git a/cabal.haskell-ci b/cabal.haskell-ci index 8a9a71fc..df92dc8b 100644 --- a/cabal.haskell-ci +++ b/cabal.haskell-ci @@ -36,6 +36,11 @@ benchmarks: True -- Build haddocks. Accepts booleans or version range. haddock: True +-- Run weeder. Accepts booleans or version range. +-- Expects 'weeder.toml' to live at the repository root, +-- and the '.hie/' directory to be generated in the repository root. +weeder: True + -- Run cabal check -- cabal-check: True diff --git a/src/HaskellCI/Auxiliary.hs b/src/HaskellCI/Auxiliary.hs index 181297c7..241a97dd 100644 --- a/src/HaskellCI/Auxiliary.hs +++ b/src/HaskellCI/Auxiliary.hs @@ -49,6 +49,7 @@ data Auxiliary = Auxiliary , anyJobUsesPreviewGHC :: Bool , runHaddock :: Bool , haddockFlags :: String + , runWeeder :: Bool } auxiliary :: Config -> Project URI Void Package -> JobVersions -> Auxiliary @@ -84,6 +85,8 @@ auxiliary Config {..} prj JobVersions {..} = Auxiliary {..} ComponentsAll -> " --haddock-all" ComponentsLibs -> "" + runWeeder = not (equivVersionRanges C.noVersion cfgWeeder) + extraCabalProjectFields :: FilePath -> [C.PrettyField ()] extraCabalProjectFields rootdir = buildList $ do -- generate package fields for URI packages. diff --git a/src/HaskellCI/Config/Grammar.hs b/src/HaskellCI/Config/Grammar.hs index c97d03b5..7762593b 100644 --- a/src/HaskellCI/Config/Grammar.hs +++ b/src/HaskellCI/Config/Grammar.hs @@ -90,6 +90,8 @@ configGrammar = Config ^^^ metahelp "RANGE" "Haddock step" <*> optionalFieldDef "haddock-components" (field @"cfgHaddockComponents") defaultConfig ^^^ metahelp "all|libs" "Haddock components" + <*> rangeField "weeder" (field @"cfgWeeder") defaultConfig + ^^^ metahelp "RANGE" "Weeder step" <*> rangeField "no-tests-no-benchmarks" (field @"cfgNoTestsNoBench") defaultConfig ^^^ metahelp "RANGE" "Build without tests and benchmarks" <*> rangeField "unconstrained" (field @"cfgUnconstrainted") defaultConfig diff --git a/src/HaskellCI/Config/Initial.hs b/src/HaskellCI/Config/Initial.hs index 943fcfe8..b809e211 100644 --- a/src/HaskellCI/Config/Initial.hs +++ b/src/HaskellCI/Config/Initial.hs @@ -37,6 +37,7 @@ initialConfig = Config , cfgBenchmarks = anyVersion , cfgHaddock = anyVersion , cfgHaddockComponents = ComponentsAll + , cfgWeeder = noVersion , cfgNoTestsNoBench = anyVersion , cfgUnconstrainted = anyVersion , cfgHeadHackage = defaultHeadHackage diff --git a/src/HaskellCI/Config/Type.hs b/src/HaskellCI/Config/Type.hs index 1875a345..ae508545 100644 --- a/src/HaskellCI/Config/Type.hs +++ b/src/HaskellCI/Config/Type.hs @@ -42,6 +42,7 @@ data Config = Config , cfgBenchmarks :: !VersionRange , cfgHaddock :: !VersionRange , cfgHaddockComponents :: !Components + , cfgWeeder :: !VersionRange , cfgNoTestsNoBench :: !VersionRange , cfgUnconstrainted :: !VersionRange , cfgHeadHackage :: !VersionRange diff --git a/src/HaskellCI/GitHub.hs b/src/HaskellCI/GitHub.hs index cd3fe70b..52913c42 100644 --- a/src/HaskellCI/GitHub.hs +++ b/src/HaskellCI/GitHub.hs @@ -536,6 +536,15 @@ makeGitHub _argv config@Config {..} gitconfig prj jobs@JobVersions {..} = do -- disable-documentation disables docs in deps: https://github.com/haskell/cabal/issues/7462 sh_if range $ "$CABAL v2-haddock --disable-documentation" ++ haddockFlags ++ " $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all" + when runWeeder $ + let range = RangeGHC /\ Range cfgWeeder in + let ifCond = ghCompilerVersionArithPredicate allVersions range in + for_ pkgs $ \Pkg{pkgName} -> do + githubUsesIf "weeder" "freckle/weeder-action@v2" ifCond $ buildList $ do + item ("ghc-version", ghWrapExpr "matrix.compilerVersion") + item ("weeder-arguments", "--config $GITHUB_WORKSPACE/source/weeder.toml") + item ("working-directory", ghWrapExpr $ ghEnvContext $ pkgNameDirVariable' pkgName) + -- unconstrained build unless (equivVersionRanges C.noVersion cfgUnconstrainted) $ githubRun "unconstrained build" $ do let range = Range cfgUnconstrainted @@ -855,3 +864,13 @@ parseGitHubRepo t = -- runners support. ghcRunsOnVer :: String ghcRunsOnVer = "ubuntu-20.04" + +ghWrapExpr :: String -> String +ghWrapExpr expr = "${{ " ++ expr ++ " }}" + +ghEnvContext :: String -> String +ghEnvContext = ("env." ++) + +ghCompilerVersionArithPredicate :: Set CompilerVersion -> CompilerRange -> String +ghCompilerVersionArithPredicate = compilerVersionPredicateImpl $ + freeToArith $ ExprConfig ghWrapExpr ghEnvContext diff --git a/src/HaskellCI/ShVersionRange.hs b/src/HaskellCI/ShVersionRange.hs index 3a32e5dd..7913062a 100644 --- a/src/HaskellCI/ShVersionRange.hs +++ b/src/HaskellCI/ShVersionRange.hs @@ -1,6 +1,9 @@ module HaskellCI.ShVersionRange ( compilerVersionPredicate, compilerVersionArithPredicate, + compilerVersionPredicateImpl, + freeToArith, + ExprConfig (..), roundDown, ) where @@ -21,11 +24,11 @@ import HaskellCI.Compiler -- >>> import qualified Distribution.Version as C compilerVersionPredicate :: Set CompilerVersion -> CompilerRange -> String -compilerVersionPredicate = compilerVersionPredicateImpl (toTest . freeToArith) where +compilerVersionPredicate = compilerVersionPredicateImpl (toTest . shFreeToArith) where toTest expr = "[ " ++ expr ++ " -ne 0 ]" compilerVersionArithPredicate :: Set CompilerVersion -> CompilerRange -> String -compilerVersionArithPredicate = compilerVersionPredicateImpl freeToArith +compilerVersionArithPredicate = compilerVersionPredicateImpl shFreeToArith compilerVersionPredicateImpl :: (Free String -> String) @@ -197,14 +200,25 @@ roundDown = go S.empty . S.toList where -- Arithmetic expression ------------------------------------------------------------------------------- -freeToArith :: Free String -> String -freeToArith z +shWrapExpr :: String -> String +shWrapExpr expr = "$((" ++ expr ++ "))" + +shFreeToArith :: Free String -> String +shFreeToArith = freeToArith $ ExprConfig shWrapExpr id + +data ExprConfig = ExprConfig { + _exprWrap :: String -> String + , _varWrap :: String -> String + } + +freeToArith :: ExprConfig -> Free String -> String +freeToArith (ExprConfig exprWrap varWrap) z | z == top = "1" | z == bottom = "0" - | otherwise = "$((" ++ go 0 z ++ "))" + | otherwise = exprWrap $ go 0 z where go :: Int -> Free String -> String - go _ (Var x) = x + go _ (Var x) = varWrap x go _ F.Bottom = "1" go _ F.Top = "0" From 9dd0dd751fcfdf3b78b9c9a95487e812f5bc56e7 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Fri, 19 Jul 2024 13:33:14 -0700 Subject: [PATCH 2/3] golden test for weeder --- fixtures/weeder.args | 0 fixtures/weeder.github | 343 ++++++++++++++++++++++++++++++++++++++++ fixtures/weeder.project | 4 + src/HaskellCI/GitHub.hs | 2 +- test/Tests.hs | 81 ++++++---- 5 files changed, 401 insertions(+), 29 deletions(-) create mode 100644 fixtures/weeder.args create mode 100644 fixtures/weeder.github create mode 100644 fixtures/weeder.project diff --git a/fixtures/weeder.args b/fixtures/weeder.args new file mode 100644 index 00000000..e69de29b diff --git a/fixtures/weeder.github b/fixtures/weeder.github new file mode 100644 index 00000000..e1251721 --- /dev/null +++ b/fixtures/weeder.github @@ -0,0 +1,343 @@ +# SUCCESS +# *INFO* Generating GitHub config for testing for GHC versions: 8.0.1 8.0.2 8.2.1 8.2.2 8.4.1 8.4.2 8.4.3 8.4.4 8.6.1 8.6.2 8.6.3 8.6.4 8.6.5 8.8.1 8.8.2 8.8.3 8.8.4 8.10.1 8.10.2 8.10.3 8.10.4 8.10.5 8.10.6 8.10.7 +# This GitHub workflow config has been generated by a script via +# +# haskell-ci 'github' 'weeder.project' +# +# To regenerate the script (for example after adjusting tested-with) run +# +# haskell-ci regenerate +# +# For more information, see https://github.com/haskell-CI/haskell-ci +# +# REGENDATA ["github","weeder.project"] +# +name: Haskell-CI +on: + - push + - pull_request +jobs: + linux: + name: Haskell-CI - Linux - ${{ matrix.compiler }} + runs-on: ubuntu-20.04 + timeout-minutes: + 60 + container: + image: buildpack-deps:jammy + continue-on-error: ${{ matrix.allow-failure }} + strategy: + matrix: + include: + - compiler: ghc-8.10.7 + compilerKind: ghc + compilerVersion: 8.10.7 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.6 + compilerKind: ghc + compilerVersion: 8.10.6 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.5 + compilerKind: ghc + compilerVersion: 8.10.5 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.4 + compilerKind: ghc + compilerVersion: 8.10.4 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.3 + compilerKind: ghc + compilerVersion: 8.10.3 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.2 + compilerKind: ghc + compilerVersion: 8.10.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.1 + compilerKind: ghc + compilerVersion: 8.10.1 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.8.4 + compilerKind: ghc + compilerVersion: 8.8.4 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.8.3 + compilerKind: ghc + compilerVersion: 8.8.3 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.8.2 + compilerKind: ghc + compilerVersion: 8.8.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.8.1 + compilerKind: ghc + compilerVersion: 8.8.1 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.6.5 + compilerKind: ghc + compilerVersion: 8.6.5 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.6.4 + compilerKind: ghc + compilerVersion: 8.6.4 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.6.3 + compilerKind: ghc + compilerVersion: 8.6.3 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.6.2 + compilerKind: ghc + compilerVersion: 8.6.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.6.1 + compilerKind: ghc + compilerVersion: 8.6.1 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.4.4 + compilerKind: ghc + compilerVersion: 8.4.4 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.4.3 + compilerKind: ghc + compilerVersion: 8.4.3 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.4.2 + compilerKind: ghc + compilerVersion: 8.4.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.4.1 + compilerKind: ghc + compilerVersion: 8.4.1 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.2.2 + compilerKind: ghc + compilerVersion: 8.2.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.2.1 + compilerKind: ghc + compilerVersion: 8.2.1 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.0.2 + compilerKind: ghc + compilerVersion: 8.0.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.0.1 + compilerKind: ghc + compilerVersion: 8.0.1 + setup-method: ghcup + allow-failure: false + fail-fast: false + steps: + - name: apt + run: | + apt-get update + apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5 libnuma-dev + mkdir -p "$HOME/.ghcup/bin" + curl -sL https://downloads.haskell.org/ghcup/0.1.30.0/x86_64-linux-ghcup-0.1.30.0 > "$HOME/.ghcup/bin/ghcup" + chmod a+x "$HOME/.ghcup/bin/ghcup" + "$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false) + "$HOME/.ghcup/bin/ghcup" install cabal 3.12.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false) + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: Set PATH and environment variables + run: | + echo "$HOME/.cabal/bin" >> $GITHUB_PATH + echo "LANG=C.UTF-8" >> "$GITHUB_ENV" + echo "CABAL_DIR=$HOME/.cabal" >> "$GITHUB_ENV" + echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV" + HCDIR=/opt/$HCKIND/$HCVER + HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER") + HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#') + HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#') + echo "HC=$HC" >> "$GITHUB_ENV" + echo "HCPKG=$HCPKG" >> "$GITHUB_ENV" + echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV" + echo "CABAL=$HOME/.ghcup/bin/cabal-3.12.1.0 -vnormal+nowrap" >> "$GITHUB_ENV" + HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))') + echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV" + echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV" + echo "ARG_BENCH=--enable-benchmarks" >> "$GITHUB_ENV" + echo "HEADHACKAGE=false" >> "$GITHUB_ENV" + echo "ARG_COMPILER=--$HCKIND --with-compiler=$HC" >> "$GITHUB_ENV" + echo "GHCJSARITH=0" >> "$GITHUB_ENV" + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: env + run: | + env + - name: write cabal config + run: | + mkdir -p $CABAL_DIR + cat >> $CABAL_CONFIG <> $CABAL_CONFIG < cabal-plan.xz + echo 'f62ccb2971567a5f638f2005ad3173dba14693a45154c1508645c52289714cb2 cabal-plan.xz' | sha256sum -c - + xz -d < cabal-plan.xz > $HOME/.cabal/bin/cabal-plan + rm -f cabal-plan.xz + chmod a+x $HOME/.cabal/bin/cabal-plan + cabal-plan --version + - name: checkout + uses: actions/checkout@v4 + with: + path: source + - name: initial cabal.project for sdist + run: | + touch cabal.project + echo "packages: $GITHUB_WORKSPACE/source/servant" >> cabal.project + echo "packages: $GITHUB_WORKSPACE/source/servant-client" >> cabal.project + echo "packages: $GITHUB_WORKSPACE/source/servant-docs" >> cabal.project + cat cabal.project + - name: sdist + run: | + mkdir -p sdist + $CABAL sdist all --output-dir $GITHUB_WORKSPACE/sdist + - name: unpack + run: | + mkdir -p unpacked + find sdist -maxdepth 1 -type f -name '*.tar.gz' -exec tar -C $GITHUB_WORKSPACE/unpacked -xzvf {} \; + - name: generate cabal.project + run: | + PKGDIR_servant="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/servant-[0-9.]*')" + echo "PKGDIR_servant=${PKGDIR_servant}" >> "$GITHUB_ENV" + PKGDIR_servant_client="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/servant-client-[0-9.]*')" + echo "PKGDIR_servant_client=${PKGDIR_servant_client}" >> "$GITHUB_ENV" + PKGDIR_servant_docs="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/servant-docs-[0-9.]*')" + echo "PKGDIR_servant_docs=${PKGDIR_servant_docs}" >> "$GITHUB_ENV" + rm -f cabal.project cabal.project.local + touch cabal.project + touch cabal.project.local + echo "packages: ${PKGDIR_servant}" >> cabal.project + echo "packages: ${PKGDIR_servant_client}" >> cabal.project + echo "packages: ${PKGDIR_servant_docs}" >> cabal.project + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo "package servant" >> cabal.project ; fi + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo " ghc-options: -Werror=missing-methods" >> cabal.project ; fi + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo "package servant-client" >> cabal.project ; fi + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo " ghc-options: -Werror=missing-methods" >> cabal.project ; fi + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo "package servant-docs" >> cabal.project ; fi + if [ $((HCNUMVER >= 80200)) -ne 0 ] ; then echo " ghc-options: -Werror=missing-methods" >> cabal.project ; fi + cat >> cabal.project <> cabal.project.local + cat cabal.project + cat cabal.project.local + - name: dump install plan + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dry-run all + cabal-plan + - name: restore cache + uses: actions/cache/restore@v4 + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store + restore-keys: ${{ runner.os }}-${{ matrix.compiler }}- + - name: install dependencies + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --dependencies-only -j2 all + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dependencies-only -j2 all + - name: build w/o tests + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: build + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --write-ghc-environment-files=always + - name: tests + run: | + $CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct + - name: cabal check + run: | + cd ${PKGDIR_servant} || false + ${CABAL} -vnormal check + cd ${PKGDIR_servant_client} || false + ${CABAL} -vnormal check + cd ${PKGDIR_servant_docs} || false + ${CABAL} -vnormal check + - name: haddock + run: | + $CABAL v2-haddock --disable-documentation --haddock-all $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all + - name: "weeder: servant" + uses: freckle/weeder-action@v2 + if: ${{ env.HCNUMVER < 81000 }} + with: + ghc-version: ${{ matrix.compilerVersion }} + weeder-arguments: --config $GITHUB_WORKSPACE/source/weeder.toml + working-directory: ${{ env.PKGDIR_servant }} + - name: "weeder: servant-client" + uses: freckle/weeder-action@v2 + if: ${{ env.HCNUMVER < 81000 }} + with: + ghc-version: ${{ matrix.compilerVersion }} + weeder-arguments: --config $GITHUB_WORKSPACE/source/weeder.toml + working-directory: ${{ env.PKGDIR_servant_client }} + - name: "weeder: servant-docs" + uses: freckle/weeder-action@v2 + if: ${{ env.HCNUMVER < 81000 }} + with: + ghc-version: ${{ matrix.compilerVersion }} + weeder-arguments: --config $GITHUB_WORKSPACE/source/weeder.toml + working-directory: ${{ env.PKGDIR_servant_docs }} + - name: unconstrained build + run: | + rm -f cabal.project.local + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: save cache + uses: actions/cache/save@v4 + if: always() + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store diff --git a/fixtures/weeder.project b/fixtures/weeder.project new file mode 100644 index 00000000..946b4e0b --- /dev/null +++ b/fixtures/weeder.project @@ -0,0 +1,4 @@ +packages: + servant, + servant-client, + servant-docs, diff --git a/src/HaskellCI/GitHub.hs b/src/HaskellCI/GitHub.hs index 52913c42..27159f90 100644 --- a/src/HaskellCI/GitHub.hs +++ b/src/HaskellCI/GitHub.hs @@ -540,7 +540,7 @@ makeGitHub _argv config@Config {..} gitconfig prj jobs@JobVersions {..} = do let range = RangeGHC /\ Range cfgWeeder in let ifCond = ghCompilerVersionArithPredicate allVersions range in for_ pkgs $ \Pkg{pkgName} -> do - githubUsesIf "weeder" "freckle/weeder-action@v2" ifCond $ buildList $ do + githubUsesIf (unwords ["weeder:", pkgName]) "freckle/weeder-action@v2" ifCond $ buildList $ do item ("ghc-version", ghWrapExpr "matrix.compilerVersion") item ("weeder-arguments", "--config $GITHUB_WORKSPACE/source/weeder.toml") item ("working-directory", ghWrapExpr $ ghEnvContext $ pkgNameDirVariable' pkgName) diff --git a/test/Tests.hs b/test/Tests.hs index add7f169..76ac357d 100644 --- a/test/Tests.hs +++ b/test/Tests.hs @@ -5,6 +5,7 @@ import Prelude () import Prelude.Compat import HaskellCI hiding (main) +import HaskellCI.Diagnostics ( DiagnosticsT ) import Control.Arrow (first) import Data.Algorithm.Diff (PolyDiff (..), getGroupedDiff) @@ -14,6 +15,7 @@ import System.FilePath (addExtension) import Test.Tasty (TestName, TestTree, defaultMain, testGroup) import Test.Tasty.Golden.Advanced (goldenTest) +import qualified Distribution.Parsec as C import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BS8 import qualified System.Console.ANSI as ANSI @@ -37,8 +39,25 @@ main = do , fixtureGoldenTest "copy-fields-some" , fixtureGoldenTest "copy-fields-none" ] + , testGroup "github-specific" + [ + fixtureWeederGoldenTest "<= 8.10" "weeder" + ] ] +-- | Weeder invocation only applies to GitHub. +fixtureWeederGoldenTest :: String -> FilePath -> TestTree +fixtureWeederGoldenTest versionRangeString fp = + fixtureGoldenTestConfigVariant mkWeederVersions fp "github" githubFromConfigFile + where + weederVersionRange = case C.eitherParsec versionRangeString of + Left e -> error $ unlines ["Supplied an unparsable version range:", e] + Right x -> x + mkWeederVersions cfg = cfg { + cfgInsertVersion = False + , cfgWeeder = weederVersionRange + } + -- | -- @ -- travisFromConfigFile :: @@ -46,35 +65,41 @@ main = do -- @ fixtureGoldenTest :: FilePath -> TestTree fixtureGoldenTest fp = testGroup fp - [ fixtureGoldenTest' "travis" travisFromConfigFile - , fixtureGoldenTest' "github" githubFromConfigFile - , fixtureGoldenTest' "bash" bashFromConfigFile + [ fixture "travis" travisFromConfigFile + , fixture "github" githubFromConfigFile + , fixture "bash" bashFromConfigFile ] - where - -- name acts as extension also - fixtureGoldenTest' name generate = cabalGoldenTest name outputRef $ do - (argv, opts') <- makeFlags - let opts = opts' - { optInputType = Just InputTypeProject - , optConfigMorphism = (\cfg -> cfg { cfgInsertVersion = False}) . optConfigMorphism opts' - } - let genConfig = generate argv opts projectfp - first (fmap (lines . fromUTF8BS)) <$> runDiagnosticsT genConfig - where - outputRef = addExtension fp name - projectfp = fp ++ ".project" - - readArgv :: IO [String] - readArgv = do - contents <- readFile $ addExtension fp "args" - return $ filter (not . null)$ lines contents - - makeFlags :: IO ([String], Options) - makeFlags = do - argv <- readArgv - let argv' = argv ++ [name, projectfp] - (_fp, opts) <- parseOptions argv' - return (argv', opts) + where + fixture = fixtureGoldenTest' fp + +fixtureGoldenTest' :: FilePath -> String -> ([String] -> Options -> String -> DiagnosticsT IO BS8.ByteString) -> TestTree +fixtureGoldenTest' = fixtureGoldenTestConfigVariant (\cfg -> cfg { cfgInsertVersion = False}) + +-- name acts as extension also +fixtureGoldenTestConfigVariant :: (Config -> Config) -> FilePath -> String -> ([String] -> Options -> String -> DiagnosticsT IO BS8.ByteString) -> TestTree +fixtureGoldenTestConfigVariant modifyConfig fp name generate = cabalGoldenTest name outputRef $ do + (argv, opts') <- makeFlags + let opts = opts' + { optInputType = Just InputTypeProject + , optConfigMorphism = modifyConfig . optConfigMorphism opts' + } + let genConfig = generate argv opts projectfp + first (fmap (lines . fromUTF8BS)) <$> runDiagnosticsT genConfig + where + outputRef = addExtension fp name + projectfp = fp ++ ".project" + + readArgv :: IO [String] + readArgv = do + contents <- readFile $ addExtension fp "args" + return $ filter (not . null)$ lines contents + + makeFlags :: IO ([String], Options) + makeFlags = do + argv <- readArgv + let argv' = argv ++ [name, projectfp] + (_fp, opts) <- parseOptions argv' + return (argv', opts) cabalGoldenTest :: TestName From 6a4444dda7cf34d16cbb3298779ce6d1b22ad161 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Fri, 19 Jul 2024 15:15:54 -0700 Subject: [PATCH 3/3] remove superfluous testGroup --- test/Tests.hs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/Tests.hs b/test/Tests.hs index 76ac357d..a20263db 100644 --- a/test/Tests.hs +++ b/test/Tests.hs @@ -39,10 +39,7 @@ main = do , fixtureGoldenTest "copy-fields-some" , fixtureGoldenTest "copy-fields-none" ] - , testGroup "github-specific" - [ - fixtureWeederGoldenTest "<= 8.10" "weeder" - ] + , fixtureWeederGoldenTest "<= 8.10" "weeder" ] -- | Weeder invocation only applies to GitHub.