Skip to content

Commit

Permalink
Merge pull request #129 from phadej/trailing-whitespace
Browse files Browse the repository at this point in the history
Add --strip-trailing-whitespace
  • Loading branch information
phadej authored May 17, 2023
2 parents 916bd3c + 22e5f84 commit 7040a52
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cabal-bundler/fixtures/derivation.nix
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ stdenv.mkDerivation {
hsdeps = builtins.attrValues {
Cabal = hackageTarball "Cabal" "3.0.0.0" "11yjd0cmqngi1yr7v0dr55n59rq78kk6121sr44abha0swkfqhsi" { rev = 1; sha256 = "0dgyim2sm867sd365xp6vhmfalx98ykm5i4jlpkacvghbv52x1ph"; };
ansi-terminal = hackageTarball "ansi-terminal" "0.10.1" "1b168z688b1lg4d2bmbxmm2mj51mwm2wv0smfb1vcwjp2vzpqf9i" {};
ansi-wl-pprint = hackageTarball "ansi-wl-pprint" "0.6.9" "1b2fg8px98dzbaqyns10kvs8kn6cl1hdq5wb9saz40izrpkyicm7" { rev = 3; sha256 = "1km10sx7ldyv1vfyljik1gqnrwl7bnq2s5m40w41gc930vm48891"; };
ansi-wl-pprint = hackageTarball "ansi-wl-pprint" "0.6.9" "1b2fg8px98dzbaqyns10kvs8kn6cl1hdq5wb9saz40izrpkyicm7" { rev = 4; sha256 = "04ljsk64bzwsczh0zc5w3r98qr1zlzbwwm2xb6nk9vradv4pnwzv"; };
cabal-fmt = hackageTarball "cabal-fmt" "0.1.1" "07hx1s7l5zmh7vs2zmmm56msz2m7wnzn919mvnpypvrsswmmnnd9" {};
colour = hackageTarball "colour" "2.3.5" "1rq4l46jx4lpdppy71wf7m1n7pw2jwy788rm35ycwzb1g4clg39v" {};
optparse-applicative = hackageTarball "optparse-applicative" "0.15.1.0" "1ws6y3b3f6hsgv0ff0yp6lw4hba1rps4dnvry3yllng0s5gngcsd" { rev = 1; sha256 = "0zmhqkd96v2z1ilhqdkd9z4jgsnsxb8yi2479ind8m5zm9363zr9"; };
Expand Down
4 changes: 2 additions & 2 deletions cabal-bundler/fixtures/fetch-with-curl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cat <<EOF > SHA256SUMS
f0862eca5ef06da6e6a592c452a747a953e52adce6f66246d3c7a0aa458dfe35 Cabal-3.0.0.0.cabal
5143ec26d740c1a508c93a8860e64407e7546c29b9817db20ff1595c1968d287 Cabal-3.0.0.0.tar.gz
31397cff165772b6c3725583cd45e535145945ad7dd5251a79342c84cc4726ac ansi-terminal-0.10.1.tar.gz
212144ea0623b1170807a4162db05d87f26cf10b334aeadd0edb377aba06a1ce ansi-wl-pprint-0.6.9.cabal
fb737bc96e2aef34ad595d54ced7a73f648c521ebcb00fe0679aff45ccd49212 ansi-wl-pprint-0.6.9.cabal
a7b2e8e7cd3f02f2954e8b17dc60a0ccd889f49e2068ebb15abfa1d42f7a4eac ansi-wl-pprint-0.6.9.tar.gz
a9595b2bd73aefebafdd358564bfe5a78aafab29b5d62ff43eb0fe428f0e1d1e cabal-fmt-0.1.1.tar.gz
3b8d471979617dce7c193523743c9782df63433d8e87e3ef6d97922e0da104e7 colour-2.3.5.tar.gz
Expand All @@ -19,7 +19,7 @@ EOF
curl --silent --location --output Cabal-3.0.0.0.cabal 'http://hackage.haskell.org/package/Cabal-3.0.0.0/revision/1.cabal'
curl --silent --location --output Cabal-3.0.0.0.tar.gz 'http://hackage.haskell.org/package/Cabal-3.0.0.0/Cabal-3.0.0.0.tar.gz'
curl --silent --location --output ansi-terminal-0.10.1.tar.gz 'http://hackage.haskell.org/package/ansi-terminal-0.10.1/ansi-terminal-0.10.1.tar.gz'
curl --silent --location --output ansi-wl-pprint-0.6.9.cabal 'http://hackage.haskell.org/package/ansi-wl-pprint-0.6.9/revision/3.cabal'
curl --silent --location --output ansi-wl-pprint-0.6.9.cabal 'http://hackage.haskell.org/package/ansi-wl-pprint-0.6.9/revision/4.cabal'
curl --silent --location --output ansi-wl-pprint-0.6.9.tar.gz 'http://hackage.haskell.org/package/ansi-wl-pprint-0.6.9/ansi-wl-pprint-0.6.9.tar.gz'
curl --silent --location --output cabal-fmt-0.1.1.tar.gz 'http://hackage.haskell.org/package/cabal-fmt-0.1.1/cabal-fmt-0.1.1.tar.gz'
curl --silent --location --output colour-2.3.5.tar.gz 'http://hackage.haskell.org/package/colour-2.3.5/colour-2.3.5.tar.gz'
Expand Down
2 changes: 1 addition & 1 deletion cabal-bundler/fixtures/openbsd-ports.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ MODCABAL_VERSION = 0.1.1
MODCABAL_MANIFEST = \
Cabal 3.0.0.0 1 \
ansi-terminal 0.10.1 0 \
ansi-wl-pprint 0.6.9 3 \
ansi-wl-pprint 0.6.9 4 \
colour 2.3.5 0 \
optparse-applicative 0.15.1.0 1 \
transformers-compat 0.6.5 0 \
5 changes: 5 additions & 0 deletions cabal-docspec/MANUAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ However, in this list we mostly only list and show the --option version of them.
Especially outputs are assumed to be Haskell-like.
Default **\--no-strip-comments**.

**-Z, \--ignore-trailing-space**

: Strip trailing whitespace from the produced outputs.
Default **\--no-ignore-trailing-space**.

**\--setup** *expr*

: An additional expression to execute as setup for all examples.
Expand Down
4 changes: 2 additions & 2 deletions cabal-docspec/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
VERSION=0.0.0.20230406
VERSION=0.0.0.20230517
EXETARGET=cabal-docspec

cabal-docspec.1 : MANUAL.md
echo '.TH CABAL-DOCSPEC 1 "April 6th, 2023" "cabal-docspec $(VERSION)" "Cabal Extras"' > cabal-docspec.1
echo '.TH CABAL-DOCSPEC 1 "May 17th, 2023" "cabal-docspec $(VERSION)" "Cabal Extras"' > cabal-docspec.1
pandoc -f markdown -t man MANUAL.md >> cabal-docspec.1

man : cabal-docspec.1
Expand Down
6 changes: 5 additions & 1 deletion cabal-docspec/cabal-docspec.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH CABAL-DOCSPEC 1 "April 6th, 2023" "cabal-docspec 0.0.0.20230406" "Cabal Extras"
.TH CABAL-DOCSPEC 1 "May 17th, 2023" "cabal-docspec 0.0.0.20230517" "Cabal Extras"
.SH NAME
.PP
cabal-docspec - another doctest for Haskell
Expand Down Expand Up @@ -72,6 +72,10 @@ Strip Haskell comments from examples and the outputs.
Especially outputs are assumed to be Haskell-like.
Default \f[B]--no-strip-comments\f[R].
.TP
\f[B]-Z, --ignore-trailing-space\f[R]
Strip trailing whitespace from the produced outputs.
Default \f[B]--no-ignore-trailing-space\f[R].
.TP
\f[B]--setup\f[R] \f[I]expr\f[R]
An additional expression to execute as setup for all examples.
Can be specified multiple times.
Expand Down
2 changes: 1 addition & 1 deletion cabal-docspec/cabal-docspec.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: cabal-docspec
version: 0.0.0.20211114
version: 0.0.0.20230517
synopsis: Run examples in your docs
category: Development
description:
Expand Down
20 changes: 18 additions & 2 deletions cabal-docspec/src/CabalDocspec/Opts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data DynOpts = DynOpts
{ optPhase :: Phase
, optPreserveIt :: PreserveIt
, optStripComs :: StripComments
, optStripEOL :: StripEOL
, optExts :: [String]
, optTimeout :: Double
, optTimeoutMsg :: String -- ^ timeout response
Expand All @@ -45,6 +46,7 @@ defaultDynOpts = DynOpts
{ optPhase = Phase2
, optPreserveIt = DontPreserveIt
, optStripComs = DontStripComments
, optStripEOL = DontStripEOL
, optExts = []
, optTimeout = 3
, optTimeoutMsg = "* Hangs forever *"
Expand All @@ -62,6 +64,7 @@ newtype Verbosity = Verbosity Int deriving (Eq, Ord, Show)
data PreserveIt = PreserveIt | DontPreserveIt deriving (Eq, Show)
data CabalPlan = CabalPlan | NoCabalPlan deriving (Eq, Show)
data StripComments = StripComments | DontStripComments deriving (Eq, Show)
data StripEOL = StripEOL | DontStripEOL deriving (Eq, Show)

data Properties
= SkipProperties
Expand Down Expand Up @@ -145,6 +148,7 @@ dynOptsP = pure combine
<*> phaseP
<*> preserveItP
<*> stripComsP
<*> stripEolP
<*> listP extP
<*> timeoutP
<*> timeoutMsgP
Expand All @@ -166,8 +170,8 @@ dynOptsP = pure combine
monoidP :: Monoid a => O.Parser a -> O.Parser (a -> a)
monoidP p = (\xs ys -> mconcat (ys : xs)) <$> many p

combine f1 f2 f3 f4 f5 f6 f7 f8 f9 fA fB fC fD fE (DynOpts x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE) =
DynOpts (f1 x1) (f2 x2) (f3 x3) (f4 x4) (f5 x5) (f6 x6) (f7 x7) (f8 x8) (f9 x9) (fA xA) (fB xB) (fC xC) (fD xD) (fE xE)
combine f1 f2 f3 f4 f5 f6 f7 f8 f9 fA fB fC fD fE fG (DynOpts x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xG) =
DynOpts (f1 x1) (f2 x2) (f3 x3) (f4 x4) (f5 x5) (f6 x6) (f7 x7) (f8 x8) (f9 x9) (fA xA) (fB xB) (fC xC) (fD xD) (fE xE) (fG xG)

lastOpt :: [a] -> a -> a
lastOpt xs initial = foldl' (\_ x -> x) initial xs
Expand Down Expand Up @@ -195,6 +199,18 @@ stripComsP = lastOpt <$> many (on <|> off) where
on = O.flag' StripComments $ O.long "strip-comments" <> O.help "Strip comments in examples"
off = O.flag' DontStripComments $ O.long "no-strip-comments" <> O.help "Don't strip comments in examples"

-- $ diff --help
-- <...>
-- -E, --ignore-tab-expansion ignore changes due to tab expansion
-- -Z, --ignore-trailing-space ignore white space at line end
-- -b, --ignore-space-change ignore changes in the amount of white space
-- -w, --ignore-all-space ignore all white space
-- -B, --ignore-blank-lines ignore changes where lines are all blank
stripEolP :: O.Parser (StripEOL -> StripEOL)
stripEolP = lastOpt <$> many (on <|> off) where
on = O.flag' StripEOL $ O.short 'Z' <> O.long "ignore-trailing-space" <> O.help "Ignore white space at line end"
off = O.flag' DontStripEOL $ O.long "no-ignore-trailing-space" <> O.help "Don't ingore white space at line end"

propertiesP :: O.Parser (Properties -> Properties)
propertiesP = lastOpt <$> many (skip <|> simple) where
skip = O.flag' SkipProperties $ O.long "no-check-properties" <> O.help "Skip properties (default)"
Expand Down
10 changes: 8 additions & 2 deletions cabal-docspec/src/CabalDocspec/Phase2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Text.PrettyPrint.Annotated (Doc, ($$))
import qualified Cabal.Config as Cabal
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified System.Console.ANSI as ANSI

import CabalDocspec.Doctest.Example
import CabalDocspec.Doctest.Extract
Expand Down Expand Up @@ -180,7 +179,14 @@ phase2 tracer dynOpts unitIds ghcInfo mbuildDir cabalCfg cwd extraEnv parsed = d

Example expr expected -> do
result <- eval tracer ghci preserveIt timeout timeoutMsg expr
case mkResult expected (lines result) of

let ls = lines result
-- --strip-trailing-whitespace
let ls' = case optStripEOL dynOpts of
StripEOL -> map (dropWhileEnd isSpace) ls
DontStripEOL -> ls

case mkResult expected ls' of
Equal -> do
return (Left (acc <> mempty { sExamples = ssSuccess }))
NotEqual diff -> do
Expand Down

0 comments on commit 7040a52

Please sign in to comment.