[#90] Forbid verifying a single file

Problem: Verifying a single file using `-r` option missbehaves
when there are absolute links present in the file. Since `-r`
option expects a directory, we can just forbid verifying a single
file.

Solution: Fail with an error message when the user tries to
specify a file as the repository's root directory.
This commit is contained in:
Nurlan Alkuatov 2022-07-22 18:29:09 +06:00
parent 71a931b4eb
commit 96960f7ebf
5 changed files with 18 additions and 2 deletions

View File

@ -6,6 +6,8 @@
Unreleased
==========
* [#117](https://github.com/serokell/xrefcheck/pull/117)
+ Forbid verifying a single file using `--root` command line option.
* [#115](https://github.com/serokell/xrefcheck/pull/115)
+ Improved parsing of anchor html tags inside headers.
* [#109](https://github.com/serokell/xrefcheck/pull/109)

View File

@ -23,11 +23,13 @@ import Data.Aeson.TH (deriveFromJSON)
import Data.Foldable qualified as F
import Data.Map qualified as M
import GHC.Err (errorWithoutStackTrace)
import System.Directory (doesDirectoryExist)
import System.Directory.Tree qualified as Tree
import System.FilePath (dropTrailingPathSeparator, takeDirectory, takeExtension, (</>), equalFilePath)
import Xrefcheck.Core
import Xrefcheck.Progress
import Xrefcheck.System (readingSystem)
import Xrefcheck.Util (aesonConfigOption, normaliseWithNoTrailing)
-- | Config of repositry traversal.
@ -64,6 +66,10 @@ gatherRepoInfo
=> Rewrite -> FormatsSupport -> TraversalConfig -> FilePath -> m RepoInfo
gatherRepoInfo rw formatsSupport config root = do
putTextRewrite rw "Scanning repository..."
when (not $ isDirectory root) $
die $ "Repository's root does not seem to be a directory: " <> root
_ Tree.:/ repoTree <- liftIO $ Tree.readDirectoryWithL processFile root
let fileInfos = map (first normaliseWithNoTrailing)
$ filter (\(path, _) -> not $ isIgnored path)
@ -72,6 +78,8 @@ gatherRepoInfo rw formatsSupport config root = do
$ filterExcludedDirs root repoTree
return $ RepoInfo (M.fromList fileInfos)
where
isDirectory = readingSystem . doesDirectoryExist
processFile file = do
let ext = takeExtension file
let mscanner = formatsSupport ext

View File

@ -45,6 +45,4 @@ spec = do
[ "tests/markdowns/without-annotations"
, "tests/markdowns/without-annotations/"
, "tests/markdowns/without-annotations/./"
, "tests/markdowns/without-annotations/all_checked.md"
, "tests/markdowns/without-annotations/./all_checked.md"
]

View File

@ -77,3 +77,11 @@ load '../helpers'
rm /tmp/check-cli.test
}
@test "Single file as root" {
run xrefcheck \
--root single-file.md
assert_failure
assert_output --partial "Repository's root does not seem to be a directory: single-file.md"
}

View File