Skip to content

Commit 03a9e53

Browse files
authored
Merge pull request haskell-servant/servant-quickcheck#88 from MangoIV/mangoiv/add-warnings
[chore] enable warnings in library and address them; address a couple of lints, remove travis.yml
2 parents cfd97ef + 90e8933 commit 03a9e53

File tree

5 files changed

+27
-196
lines changed

5 files changed

+27
-196
lines changed

servant-quickcheck/.travis.yml

Lines changed: 0 additions & 158 deletions
This file was deleted.

servant-quickcheck/servant-quickcheck.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ library
3333
Servant.QuickCheck.Internal.Predicates
3434
Servant.QuickCheck.Internal.QuickCheck
3535

36+
ghc-options: -Wall -Wcompat
3637
build-depends:
3738
aeson >=0.8 && <2.3
3839
, base >=4.9 && <4.20
@@ -82,7 +83,7 @@ library
8283

8384
test-suite spec
8485
type: exitcode-stdio-1.0
85-
ghc-options: -Wall -threaded
86+
ghc-options: -Wall -Wcompat -threaded
8687
default-language: Haskell2010
8788
hs-source-dirs: test
8889
main-is: Spec.hs
@@ -124,7 +125,7 @@ test-suite example
124125
type: exitcode-stdio-1.0
125126
main-is: Main.hs
126127
hs-source-dirs: example
127-
ghc-options: -Wall
128+
ghc-options: -Wall -Wcompat
128129
build-depends:
129130
base
130131
, hspec

servant-quickcheck/src/Servant/QuickCheck/Internal/Equality.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import Data.Aeson (Value, decode, decodeStrict)
44
import Data.ByteString (ByteString)
55
import qualified Data.ByteString.Lazy as LB
66
import Data.Function (on)
7-
import Data.Semigroup (Semigroup (..))
8-
import Network.HTTP.Client (Response (..), equivCookieJar,
9-
responseBody, responseClose)
7+
import Network.HTTP.Client (Response (..), equivCookieJar, responseBody)
108
import Prelude.Compat
119

1210
newtype ResponseEquality b = ResponseEquality {getResponseEquality :: Response b -> Response b -> Bool}

servant-quickcheck/src/Servant/QuickCheck/Internal/Predicates.hs

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Data.CaseInsensitive (foldCase, foldedCase, mk)
1111
import Data.Either (isRight)
1212
import Data.List.Split (wordsBy)
1313
import Data.Maybe (fromMaybe, isJust)
14-
import Data.Semigroup (Semigroup (..))
1514
import qualified Data.Text as T
1615
import Data.Time (UTCTime, defaultTimeLocale, parseTimeM,
1716
rfc822DateFormat)
@@ -155,7 +154,7 @@ createContainsValidLocation
155154
getsHaveLastModifiedHeader :: RequestPredicate
156155
getsHaveLastModifiedHeader
157156
= RequestPredicate $ \req mgr ->
158-
if (method req == methodGet)
157+
if method req == methodGet
159158
then do
160159
resp <- httpLbs req mgr
161160
unless (hasValidHeader "Last-Modified" isRFC822Date resp) $ do
@@ -189,15 +188,15 @@ notAllowedContainsAllowHeader
189188
= RequestPredicate $ \req mgr -> do
190189
let reqs = [ req { method = renderStdMethod m } | m <- [minBound .. maxBound]
191190
, renderStdMethod m /= method req ]
192-
resp <- mapM (flip httpLbs mgr) reqs
191+
resp <- mapM (`httpLbs` mgr) reqs
193192

194193
case filter pred' (zip reqs resp) of
195194
(x:_) -> throw $ PredicateFailure "notAllowedContainsAllowHeader" (Just $ fst x) (snd x)
196195
[] -> return resp
197196
where
198197
pred' (_, resp) = responseStatus resp == status405 && not (hasValidHeader "Allow" go resp)
199198
where
200-
go x = all (\y -> isRight $ parseMethod $ SBSC.pack y)
199+
go x = all (isRight . parseMethod . SBSC.pack)
201200
$ wordsBy (`elem` (", " :: [Char])) (SBSC.unpack x)
202201

203202

@@ -223,11 +222,9 @@ honoursAcceptHeader
223222
let scode = responseStatus resp
224223
sctype = lookup "Content-Type" $ responseHeaders resp
225224
sacc = fromMaybe "*/*" $ lookup "Accept" (requestHeaders req)
226-
if status100 < scode && scode < status300
227-
then if isJust $ sctype >>= \x -> matchAccept [x] sacc
228-
then throw $ PredicateFailure "honoursAcceptHeader" (Just req) resp
229-
else return [resp]
230-
else return [resp]
225+
(if (status100 < scode && scode < status300) && isJust (sctype >>= \x -> matchAccept [x] sacc)
226+
then throw $ PredicateFailure "honoursAcceptHeader" (Just req) resp
227+
else return [resp])
231228

232229

233230
-- | [__Best Practice__]
@@ -247,7 +244,7 @@ honoursAcceptHeader
247244
getsHaveCacheControlHeader :: RequestPredicate
248245
getsHaveCacheControlHeader
249246
= RequestPredicate $ \req mgr ->
250-
if (method req == methodGet)
247+
if method req == methodGet
251248
then do
252249
resp <- httpLbs req mgr
253250
unless (hasValidHeader "Cache-Control" (const True) resp) $ do
@@ -263,7 +260,7 @@ getsHaveCacheControlHeader
263260
headsHaveCacheControlHeader :: RequestPredicate
264261
headsHaveCacheControlHeader
265262
= RequestPredicate $ \req mgr ->
266-
if (method req == methodHead)
263+
if method req == methodHead
267264
then do
268265
resp <- httpLbs req mgr
269266
unless (hasValidHeader "Cache-Control" (const True) resp) $
@@ -334,10 +331,9 @@ linkHeadersAreValid
334331
unauthorizedContainsWWWAuthenticate :: ResponsePredicate
335332
unauthorizedContainsWWWAuthenticate
336333
= ResponsePredicate $ \resp ->
337-
if responseStatus resp == status401
338-
then unless (hasValidHeader "WWW-Authenticate" (const True) resp) $
334+
when (responseStatus resp == status401) $
335+
unless (hasValidHeader "WWW-Authenticate" (const True) resp) $
339336
throw $ PredicateFailure "unauthorizedContainsWWWAuthenticate" Nothing resp
340-
else return ()
341337

342338

343339
-- | [__RFC Compliance__]
@@ -354,12 +350,10 @@ unauthorizedContainsWWWAuthenticate
354350
htmlIncludesDoctype :: ResponsePredicate
355351
htmlIncludesDoctype
356352
= ResponsePredicate $ \resp ->
357-
if hasValidHeader "Content-Type" (SBS.isPrefixOf . foldCase $ "text/html") resp
358-
then do
359-
let htmlContent = foldCase . LBS.take 20 $ responseBody resp
360-
unless (LBS.isPrefixOf (foldCase "<!doctype html>") htmlContent) $
361-
throw $ PredicateFailure "htmlIncludesDoctype" Nothing resp
362-
else return ()
353+
when (hasValidHeader "Content-Type" (SBS.isPrefixOf . foldCase $ "text/html") resp) $ do
354+
let htmlContent = foldCase . LBS.take 20 $ responseBody resp
355+
unless (LBS.isPrefixOf (foldCase "<!doctype html>") htmlContent) $
356+
throw $ PredicateFailure "htmlIncludesDoctype" Nothing resp
363357

364358
-- * Predicate logic
365359

@@ -392,7 +386,7 @@ newtype RequestPredicate = RequestPredicate
392386

393387
-- TODO: This isn't actually a monoid
394388
instance Monoid RequestPredicate where
395-
mempty = RequestPredicate (\r m -> httpLbs r m >>= \x -> return ([x]))
389+
mempty = RequestPredicate (\r m -> httpLbs r m >>= \x -> return [x])
396390
mappend = (<>)
397391

398392
-- TODO: This isn't actually a monoid
@@ -417,10 +411,10 @@ instance Monoid Predicates where
417411
class JoinPreds a where
418412
joinPreds :: a -> Predicates -> Predicates
419413

420-
instance JoinPreds (RequestPredicate ) where
414+
instance JoinPreds RequestPredicate where
421415
joinPreds p (Predicates x y) = Predicates (p <> x) y
422416

423-
instance JoinPreds (ResponsePredicate ) where
417+
instance JoinPreds ResponsePredicate where
424418
joinPreds p (Predicates x y) = Predicates x (p <> y)
425419

426420
-- | Adds a new predicate (either `ResponsePredicate` or `RequestPredicate`) to
@@ -444,9 +438,7 @@ finishPredicates p req mgr = go `catch` \(e :: PredicateFailure) -> return $ Jus
444438
-- * helpers
445439

446440
hasValidHeader :: SBS.ByteString -> (SBS.ByteString -> Bool) -> Response b -> Bool
447-
hasValidHeader hdr p r = case lookup (mk hdr) (responseHeaders r) of
448-
Nothing -> False
449-
Just v -> p v
441+
hasValidHeader hdr p r = maybe False p (lookup (mk hdr) (responseHeaders r))
450442

451443
isRFC822Date :: SBS.ByteString -> Bool
452444
isRFC822Date s

servant-quickcheck/src/Servant/QuickCheck/Internal/QuickCheck.hs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE RecordWildCards #-}
21
{-# LANGUAGE CPP #-}
32
module Servant.QuickCheck.Internal.QuickCheck where
43

@@ -21,7 +20,6 @@ import Test.QuickCheck (Args (..), Result (..), quickCheckWi
2120
import Test.QuickCheck.Monadic (assert, forAllM, monadicIO, monitor,
2221
run)
2322
import Test.QuickCheck.Property (counterexample)
24-
2523
import Servant.QuickCheck.Internal.Equality
2624
import Servant.QuickCheck.Internal.ErrorTypes
2725
import Servant.QuickCheck.Internal.HasGenRequest
@@ -47,7 +45,7 @@ withServantServerAndContext :: HasServer a ctx
4745
#endif
4846
=> Proxy a -> Context ctx -> IO (Server a) -> (BaseUrl -> IO r) -> IO r
4947
withServantServerAndContext api ctx server t
50-
= withApplication (return . serveWithContext api ctx =<< server) $ \port ->
48+
= withApplication (serveWithContext api ctx <$> server) $ \port ->
5149
t (BaseUrl Http "localhost" port "")
5250

5351
-- | Check that the two servers running under the provided @BaseUrl@s behave
@@ -90,7 +88,7 @@ serversEqual api burl1 burl2 args req = do
9088
assert False
9189
case r of
9290
Success {} -> return ()
93-
Failure{..} -> do
91+
Failure {} -> do
9492
mx <- tryReadMVar deetsMVar
9593
case mx of
9694
Just x ->
@@ -146,15 +144,15 @@ serverSatisfiesMgr api manager burl args preds = do
146144
_ -> return ()
147145
case r of
148146
Success {} -> return ()
149-
Failure {..} -> do
147+
Failure {} -> do
150148
mx <- tryReadMVar deetsMVar
151149
case mx of
152150
Just x ->
153151
expectationFailure $ "Failed:\n" ++ show x
154152
Nothing ->
155153
expectationFailure $ "We failed to record a reason for failure: " <> show r
156154
GaveUp { numTests = n } -> expectationFailure $ "Gave up after " ++ show n ++ " tests"
157-
NoExpectedFailure {} -> expectationFailure $ "No expected failure"
155+
NoExpectedFailure {} -> expectationFailure "No expected failure"
158156
#if MIN_VERSION_QuickCheck(2,12,0)
159157
#else
160158
InsufficientCoverage {} -> expectationFailure "Insufficient coverage"
@@ -175,7 +173,7 @@ serverDoesntSatisfyMgr api manager burl args preds = do
175173
Success {} -> return ()
176174
GaveUp { numTests = n } -> expectationFailure $ "Gave up after " ++ show n ++ " tests"
177175
Failure { output = m } -> expectationFailure $ "Failed:\n" ++ show m
178-
NoExpectedFailure {} -> expectationFailure $ "No expected failure"
176+
NoExpectedFailure {} -> expectationFailure "No expected failure"
179177
#if MIN_VERSION_QuickCheck(2,12,0)
180178
#else
181179
InsufficientCoverage {} -> expectationFailure "Insufficient coverage"

0 commit comments

Comments
 (0)