Skip to content

Commit

Permalink
fixup! fixup! [#139] Ignore build-related files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sereja313 committed Oct 18, 2022
1 parent 9fcc9ec commit 81b8ab4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
13 changes: 9 additions & 4 deletions src/Xrefcheck/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ makeLenses ''FileInfo
instance Default FileInfo where
def = diffToFileInfo mempty

-- | Files from the repo with `FileInfo` attached to files that we can scan.
newtype RepoInfo = RepoInfo (Map FilePath (Maybe FileInfo))
deriving stock (Show)
-- | All tracked files and directories.
data RepoInfo = RepoInfo
{ riFiles :: Map FilePath (Maybe FileInfo)
-- ^ Files from the repo with `FileInfo` attached to files that we can scan.
, riDirectories :: Set FilePath
-- ^ Tracked directories.
} deriving stock (Show)

-----------------------------------------------------------
-- Instances
Expand Down Expand Up @@ -172,7 +176,8 @@ instance Given ColorMode => Buildable FileInfo where
]

instance Given ColorMode => Buildable RepoInfo where
build (RepoInfo m) = blockListF' "" buildFileReport (M.toList m)
build (RepoInfo m _) =
blockListF' "" buildFileReport (mapMaybe sequence $ M.toList m)
where
buildFileReport (name, info) = mconcat
[ colorIfNeeded Cyan $ fromString name <> ":\n"
Expand Down
11 changes: 9 additions & 2 deletions src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import Data.Map qualified as M
import Data.Reflection (Given)
import Fmt (Buildable (..), nameF, (+|), (|+))
import System.Directory (doesDirectoryExist)
import System.FilePath (dropTrailingPathSeparator, equalFilePath, takeExtension, (</>))
import System.FilePath
(dropTrailingPathSeparator, equalFilePath, splitDirectories, takeDirectory, takeExtension, (</>))
import System.Process (cwd, readCreateProcess, shell)

import Xrefcheck.Core
Expand Down Expand Up @@ -145,11 +146,17 @@ scanRepo rw formatsSupport config root = do
$ (gatherScanErrs &&& gatherFileInfos)
<$> readDirectoryWith config processFile root

return . ScanResult errs $ RepoInfo (M.fromList fileInfos)
let dirs = fromList $ foldMap (getDirs . fst) fileInfos

return . ScanResult errs $ RepoInfo (M.fromList fileInfos) dirs
where
isDirectory :: FilePath -> Bool
isDirectory = readingSystem . doesDirectoryExist

-- Get all directories from filepath.
getDirs :: FilePath -> [FilePath]
getDirs = scanl (</>) "" . splitDirectories . takeDirectory

gatherScanErrs
:: [(FilePath, Maybe (FileInfo, [ScanError]))]
-> [ScanError]
Expand Down
22 changes: 11 additions & 11 deletions src/Xrefcheck/Verify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ verifyRepo
config@VerifyConfig{..}
mode
root
repoInfo'@(RepoInfo repoInfo)
repoInfo'@(RepoInfo files _)
= do
let toScan = do
(file, fileInfo) <- M.toList repoInfo
(file, fileInfo) <- M.toList files
guard . not $ matchesGlobPatterns root vcNotScanned file
case fileInfo of
Just fi -> do
Expand Down Expand Up @@ -315,7 +315,7 @@ verifyReference
config@VerifyConfig{..}
mode
progressRef
(RepoInfo repoInfo)
(RepoInfo files dirs)
root
fileWithReference
ref@Reference{..}
Expand Down Expand Up @@ -420,7 +420,7 @@ verifyReference
unless (isVirtual referredFile) do
checkReferredFileIsInsideRepo referredFile
checkReferredFileExists referredFile
case lookupFilePath referredFile $ M.toList repoInfo of
case lookupFilePath referredFile $ M.toList files of
Nothing -> pass -- no support for such file, can do nothing
Just referredFileInfo -> whenJust mAnchor $
checkAnchor referredFile (_fiAnchors referredFileInfo)
Expand Down Expand Up @@ -461,17 +461,17 @@ verifyReference
nestingChange _ = 1

checkReferredFileExists file = do
unless checkExists $
unless (fileExists || dirExists) $
throwError (LocalFileDoesNotExist file)
where
checkExists :: Bool
checkExists = any (liftA2 (||) filePathExists dirExists) $ M.keys repoInfo
matchesFilePath :: FilePath -> Bool
matchesFilePath = equalFilePath $ expandIndirections file

filePathExists :: FilePath -> Bool
filePathExists = equalFilePath $ expandIndirections file
fileExists :: Bool
fileExists = any matchesFilePath $ M.keys files

dirExists :: FilePath -> Bool
dirExists = any filePathExists . scanl (</>) "" . splitDirectories
dirExists :: Bool
dirExists = any matchesFilePath dirs

checkAnchor file fileAnchors anchor = do
checkAnchorReferenceAmbiguity file fileAnchors anchor
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/Xrefcheck/TooManyRequestsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ test_tooManyRequests = testGroup "429 response tests"
verifyReferenceWithProgress reference progRef = do
fmap wrlItem <$> verifyReference
((cVerification $ defConfig GitHub) { vcIgnoreRefs = [] }) FullMode
progRef (RepoInfo M.empty) "." "" reference
progRef (RepoInfo M.empty mempty) "." "" reference

-- | When called for the first time, returns with a 429 and `Retry-After: @retryAfter@`.
-- Subsequent calls will respond with @status@.
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/Xrefcheck/TrailingSlashSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_slash = testGroup "Trailing forward slash detection" $
testCase ("All the files within the root \"" <>
root <>
"\" should exist") $ do
(ScanResult _ (RepoInfo repoInfo)) <- allowRewrite False $ \rw ->
(ScanResult _ (RepoInfo repoInfo _)) <- allowRewrite False $ \rw ->
scanRepo rw format TraversalConfig{ tcIgnored = [] } root
nonExistentFiles <- lefts <$> forM (keys repoInfo) (\filePath -> do
predicate <- doesFileExist filePath
Expand Down

0 comments on commit 81b8ab4

Please sign in to comment.