summaryrefslogtreecommitdiff
path: root/Command/FuzzTest.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2013-05-25 18:15:34 -0400
committerGravatar Joey Hess <joey@kitenet.net>2013-05-25 18:15:34 -0400
commit9449cb5a1a9d8e53863421cacdf9a908516bbd18 (patch)
treec7d252d2e39e29b7e2102ad8ec840a36ac58047c /Command/FuzzTest.hs
parent5574e612b21f9103d71cb51610710fbd8a6791d9 (diff)
fuzz tester: avoid deleting entire repository (had to happen eventually, right?)
Diffstat (limited to 'Command/FuzzTest.hs')
-rw-r--r--Command/FuzzTest.hs48
1 files changed, 27 insertions, 21 deletions
diff --git a/Command/FuzzTest.hs b/Command/FuzzTest.hs
index 29ee51ef2..d75bb5a04 100644
--- a/Command/FuzzTest.hs
+++ b/Command/FuzzTest.hs
@@ -211,31 +211,37 @@ genFuzzAction = do
existingFile :: Int -> FilePath -> IO (Maybe FuzzFile)
existingFile 0 _ = return Nothing
-existingFile n top = maybe (return Nothing) (go . toFilePath) =<< existingDir
- where
- go dir = do
- contents <- catchDefaultIO [] (getDirectoryContents dir)
- let files = filter isFuzzFile contents
- if null files
- then do
- let dirs = filter isFuzzDir contents
- if null dirs
- then return Nothing
- else do
- i <- getStdRandom $ randomR (0, length dirs - 1)
- existingFile (n - 1) (top </> dirs !! i)
- else do
- i <- getStdRandom $ randomR (0, length files - 1)
- return $ Just $ FuzzFile $ top </> dir </> files !! i
-
-existingDir :: IO (Maybe FuzzDir)
-existingDir = do
+existingFile n top = do
+ dir <- existingDirIncludingTop
+ contents <- catchDefaultIO [] (getDirectoryContents dir)
+ let files = filter isFuzzFile contents
+ if null files
+ then do
+ let dirs = filter isFuzzDir contents
+ if null dirs
+ then return Nothing
+ else do
+ i <- getStdRandom $ randomR (0, length dirs - 1)
+ existingFile (n - 1) (top </> dirs !! i)
+ else do
+ i <- getStdRandom $ randomR (0, length files - 1)
+ return $ Just $ FuzzFile $ top </> dir </> files !! i
+
+existingDirIncludingTop :: IO FilePath
+existingDirIncludingTop = do
dirs <- filter isFuzzDir <$> getDirectoryContents "."
if null dirs
- then return Nothing
+ then return "."
else do
n <- getStdRandom $ randomR (0, length dirs)
- return $ Just $ FuzzDir $ ("." : dirs) !! n
+ return $ ("." : dirs) !! n
+
+existingDir :: IO (Maybe FuzzDir)
+existingDir = do
+ d <- existingDirIncludingTop
+ return $ if isFuzzDir d
+ then Just $ FuzzDir d
+ else Nothing
newFile :: IO (Maybe FuzzFile)
newFile = go (100 :: Int)