Skip to content

Commit 063f731

Browse files
committed
Check based on globs only
Check only globs, not the filesystem.
1 parent 1da3835 commit 063f731

File tree

12 files changed

+89
-65
lines changed

12 files changed

+89
-65
lines changed

Cabal/src/Distribution/PackageDescription/Check.hs

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import Data.Foldable (foldrM)
4040
import Distribution.Compat.Prelude
4141
import Prelude ()
4242

43-
import Data.List (delete, group)
43+
import Data.List (group)
4444
import Distribution.CabalSpecVersion
4545
import Distribution.Compat.Lens
4646
import Distribution.Compiler
@@ -784,12 +784,12 @@ ppExplanation MissingSourceControl =
784784
++ "control information in the .cabal file using one or more "
785785
++ "'source-repository' sections. See the Cabal user guide for "
786786
++ "details."
787-
ppExplanation (MissingExpectedExtraDocFiles paths) =
788-
"Please consider including the " ++ quotes paths
787+
ppExplanation (MissingExpectedExtraDocFiles files) =
788+
"Please consider including " ++ gather files
789789
++ " in the 'extra-doc-files' section of the .cabal file "
790790
++ "if it contains useful information for users of the package."
791-
where quotes [p] = "file " ++ quote p
792-
quotes ps = "files " ++ intercalate ", " (map quote ps)
791+
where gather [p] = p ++ " file"
792+
gather ps = intercalate " and " ps ++ " files"
793793
ppExplanation (WrongFieldExpectedExtraDocFiles field paths) =
794794
"Please consider moving the " ++ quotes paths
795795
++ " from the '" ++ field ++ "' section of the .cabal file "
@@ -2411,23 +2411,16 @@ checkGlobFiles :: Verbosity
24112411
-> FilePath
24122412
-> IO [PackageCheck]
24132413
checkGlobFiles verbosity pkg root = do
2414-
-- Get the desirable doc files from package’s directory
2415-
rootContents <- System.Directory.getDirectoryContents root
2416-
desirableDocFiles0 <- filterM System.doesFileExist
2417-
[ root </> file
2418-
| file <- rootContents
2419-
, isDesirableExtraDocFile file
2420-
]
2421-
24222414
-- Check the globs
2423-
(warnings, unlisted) <- foldrM checkGlob ([], desirableDocFiles0) allGlobs
2415+
(warnings, missingChangeLog) <- foldrM checkGlob ([], True) allGlobs
24242416

2425-
return $ if null unlisted
2426-
-- No missing desirable file
2427-
then warnings
2417+
return $ if missingChangeLog
24282418
-- Some missing desirable files
2429-
else warnings ++
2430-
[PackageDistSuspiciousWarn (MissingExpectedExtraDocFiles unlisted)]
2419+
then let unlisted = [ "a changelog" | missingChangeLog]
2420+
in warnings ++
2421+
[PackageDistSuspiciousWarn (MissingExpectedExtraDocFiles unlisted)]
2422+
-- No missing desirable file
2423+
else warnings
24312424
where
24322425
adjustedDataDir = if null (dataDir pkg) then root else root </> dataDir pkg
24332426
-- Cabal fields with globs
@@ -2440,92 +2433,101 @@ checkGlobFiles verbosity pkg root = do
24402433

24412434
-- For each field with globs (see allGlobs), look for:
24422435
-- • errors (missing directory, no match)
2443-
-- • omitted documentation files (readme, changelog)
2436+
-- • omitted documentation files (changelog)
24442437
checkGlob :: (String, Bool, FilePath, FilePath)
2445-
-> ([PackageCheck], [FilePath])
2446-
-> IO ([PackageCheck], [FilePath])
2447-
checkGlob (field, isDocField, dir, glob) acc@(warnings, desirableDocs) =
2438+
-> ([PackageCheck], Bool)
2439+
-> IO ([PackageCheck], Bool)
2440+
checkGlob (field, isDocField, dir, glob) acc@(warnings, changelog) =
24482441
-- Note: we just skip over parse errors here; they're reported elsewhere.
24492442
case parseFileGlob (specVersion pkg) glob of
24502443
Left _ -> return acc
24512444
Right parsedGlob -> do
24522445
results <- runDirFileGlob verbosity (root </> dir) parsedGlob
2453-
let acc0 = (warnings, True, [], desirableDocs)
2446+
let acc0 = (warnings, True, changelog, [])
24542447
return $ case foldr checkGlobResult acc0 results of
2455-
(individualWarnings, noMatchesWarn, wrongPaths, desirableDocs') ->
2448+
(individualWarn, noMatchesWarn, changelog', wrongPaths) ->
24562449
let wrongFieldWarnings = [ PackageDistSuspiciousWarn
24572450
(WrongFieldExpectedExtraDocFiles
24582451
field wrongPaths)
24592452
| not (null wrongPaths) ]
24602453
in
24612454
( if noMatchesWarn
24622455
then [PackageDistSuspiciousWarn (GlobNoMatch field glob)] ++
2463-
individualWarnings ++
2456+
individualWarn ++
24642457
wrongFieldWarnings
2465-
else individualWarnings ++ wrongFieldWarnings
2466-
, desirableDocs'
2458+
else individualWarn ++ wrongFieldWarnings
2459+
, changelog'
24672460
)
24682461
where
24692462
checkGlobResult :: GlobResult FilePath
2470-
-> ([PackageCheck], Bool, [FilePath], [FilePath])
2471-
-> ([PackageCheck], Bool, [FilePath], [FilePath])
2472-
checkGlobResult result (ws, noMatchesWarn, wrongPaths, docFiles) =
2463+
-> ([PackageCheck], Bool, Bool, [FilePath])
2464+
-> ([PackageCheck], Bool, Bool, [FilePath])
2465+
checkGlobResult result (ws, noMatchesWarn, changelog1, wrongPaths) =
24732466
let noMatchesWarn' = noMatchesWarn &&
24742467
not (suppressesNoMatchesWarning result)
24752468
in case getWarning field glob result of
24762469
-- No match: add warning and do no further check
24772470
Left w ->
24782471
( w : ws
24792472
, noMatchesWarn'
2473+
, changelog1
24802474
, wrongPaths
2481-
, docFiles
24822475
)
24832476
-- Match: check doc files
24842477
Right path ->
2485-
let path' = "." </> path -- HACK? match getDirectoryContents result
2486-
(wrongPaths', docFiles') = checkDoc isDocField path'
2487-
wrongPaths docFiles
2478+
let (changelog1', wrongPaths') = checkDoc isDocField
2479+
path
2480+
changelog1
2481+
wrongPaths
24882482
in
24892483
( ws
24902484
, noMatchesWarn'
2485+
, changelog1'
24912486
, wrongPaths'
2492-
, docFiles'
24932487
)
24942488

24952489
-- Check whether a path is a desirable doc: if so, check if it is in the
24962490
-- field "extra-doc-files" and remove it from the list of paths to check.
24972491
checkDoc :: Bool -- Is it "extra-doc-files" ?
24982492
-> FilePath -- Path to test
2493+
-> Bool -- Look for changelog?
24992494
-> [FilePath] -- Previous wrong paths
2500-
-> [FilePath] -- Remaining paths to check
2501-
-> ([FilePath], [FilePath]) -- Updated paths
2502-
checkDoc isDocField path wrongFieldPaths docFiles =
2503-
if path `elem` docFiles
2504-
-- Found desirable doc file
2505-
then
2506-
( if isDocField then wrongFieldPaths else path : wrongFieldPaths
2507-
, delete path docFiles
2508-
)
2509-
-- Not a desirable doc file
2510-
else
2511-
( wrongFieldPaths
2512-
, docFiles
2513-
)
2495+
-> (Bool, [FilePath]) -- Updated paths
2496+
checkDoc isDocField path changelog wrongFieldPaths
2497+
-- Found desirable changelog file
2498+
| changelog && isDesirableExtraDocFile desirableChangeLog path =
2499+
( False
2500+
, if isDocField
2501+
then wrongFieldPaths
2502+
else (root </> path) : wrongFieldPaths
2503+
)
2504+
-- Not a desirable doc file
2505+
| otherwise =
2506+
( changelog
2507+
, wrongFieldPaths
2508+
)
25142509

25152510
-- Test whether a file is a desirable documentation for Hackage server
2516-
isDesirableExtraDocFile :: FilePath -> Bool
2517-
isDesirableExtraDocFile fp = map toLower basename `elem` desirable
2511+
isDesirableExtraDocFile :: [FilePath] -> FilePath -> Bool
2512+
isDesirableExtraDocFile paths fp = map toLower basename `elem` paths
25182513
where
25192514
(basename, _ext) = splitExtension fp
2520-
desirable =
2521-
[ -- Source: hackage-server/src/Distribution/Server/Packages/Readme.hs
2522-
"readme"
2523-
-- Source: hackage-server/src/Distribution/Server/Packages/ChangeLog.hs
2524-
, "news"
2525-
, "changelog"
2526-
, "change_log"
2527-
, "changes"
2528-
]
2515+
2516+
-- Source: hackage-server/src/Distribution/Server/Packages/ChangeLog.hs
2517+
desirableChangeLog =
2518+
[ "news"
2519+
, "changelog"
2520+
, "change_log"
2521+
, "changes"
2522+
]
2523+
-- [TODO] Check readme. Observations:
2524+
-- • Readme is not necessary if package description is good.
2525+
-- • Some readmes exists only for repository browsing.
2526+
-- • There is currently no reliable way to check what a good
2527+
-- description is; there will be complains if the criterion is
2528+
-- based on the length or number of words (can of worms).
2529+
-- -- Source: hackage-server/src/Distribution/Server/Packages/Readme.hs
2530+
-- desirableReadme = ["readme"]
25292531

25302532
-- If there's a missing directory in play, since our globs don't
25312533
-- (currently) support disjunction, that will always mean there are no
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cabal check
2+
Warning: These warnings may cause trouble when distributing the package:
3+
Warning: Please consider including a changelog file in the 'extra-doc-files' section of the .cabal file if it contains useful information for users of the package.

cabal-testsuite/PackageTests/Check/PackageFiles/MissingExpectedExtraDocFiles/cabal.test.hs renamed to cabal-testsuite/PackageTests/Check/PackageFiles/MissingExpectedExtraDocFiles/ChangeLog/cabal.test.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import Test.Cabal.Prelude
22

33
import System.Directory (createDirectoryIfMissing)
44

5-
-- Omitting README but not ChangeLog.md in extra-doc-files
5+
-- Omitting ChangeLog.md but not README in extra-doc-files
66
main = cabalTest $ do
77
cabal "check" []
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cabal-version: 3.0
2+
name: pkg
3+
synopsis: synopsis
4+
description: description
5+
version: 0
6+
category: example
7+
maintainer: [email protected]
8+
license: GPL-3.0-or-later
9+
extra-doc-files: README
10+
11+
library
12+
exposed-modules: Foo
13+
default-language: Haskell2010

cabal-testsuite/PackageTests/Check/PackageFiles/MissingExpectedExtraDocFiles/Readme/ChangeLog.md

Whitespace-only changes.

cabal-testsuite/PackageTests/Check/PackageFiles/MissingExpectedExtraDocFiles/Readme/README

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# cabal check
2+
No errors or warnings could be found in the package.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Test.Cabal.Prelude
2+
3+
import System.Directory (createDirectoryIfMissing)
4+
5+
-- Omitting README in extra-doc-files
6+
main = cabalTest $ do
7+
cabal "check" []

cabal-testsuite/PackageTests/Check/PackageFiles/MissingExpectedExtraDocFiles/cabal.out

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

0 commit comments

Comments
 (0)