Checks for duplicate FileDraft _dstPath during generation (#733)

This commit is contained in:
InfiniteVerma 2022-10-17 20:29:24 +05:30 committed by GitHub
parent e2fe5b2402
commit c6b61ce573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -3,6 +3,7 @@
module Wasp.Generator.WriteFileDrafts
( synchronizeFileDraftsWithDisk,
fileDraftsToWriteAndFilesToDelete, -- Exported for testing.
assertDstPathsAreUnique, -- Exported for testing.
removeFromChecksumFile,
)
where
@ -15,7 +16,7 @@ import qualified Data.ByteString.Lazy as BSL
import Data.Either (lefts, rights)
import qualified Data.HashMap.Strict as Map
import qualified Data.HashSet as Set
import Data.List (sortBy)
import Data.List (group, sort, sortBy)
import StrongPath (Abs, Dir, File', Path', Rel, relfile, (</>))
import qualified StrongPath as SP
import System.Directory (removeDirectoryRecursive, removeFile)
@ -31,6 +32,8 @@ import Wasp.Util (Checksum)
-- It is smart when writing, so it doesn't write file drafts that are already written on the disk from before.
synchronizeFileDraftsWithDisk :: Path' Abs (Dir ProjectRootDir) -> [FileDraft] -> IO ()
synchronizeFileDraftsWithDisk dstDir fileDrafts = do
return $! assertDstPathsAreUnique fileDrafts
maybePathsToChecksums <- readChecksumFile dstDir
case maybePathsToChecksums of
-- If checksums file is missing/corrupted, we delete all of the generated code
@ -60,6 +63,14 @@ type RelPathsToChecksums = [(FileOrDirPathRelativeTo ProjectRootDir, Checksum)]
type RelPathsToChecksumsMap = Map.HashMap (FileOrDirPathRelativeTo ProjectRootDir) Checksum
-- | Takes file drafts and verifies if the destination paths are unique.
assertDstPathsAreUnique :: [FileDraft] -> ()
assertDstPathsAreUnique fileDrafts =
let fdDstPaths = map getDstPath fileDrafts
duplicateFdDstPaths = map head $ filter ((> 1) . length) (group . sort $ fdDstPaths)
errMessage = unlines $ "FileDraft destination paths are not unique! Duplicates include: " : map show duplicateFdDstPaths
in if null duplicateFdDstPaths then () else error errMessage
-- | This file stores all checksums for files and directories that were written to disk
-- on the last project generation.
checksumFileInProjectRoot :: Path' (Rel ProjectRootDir) File'

View File

@ -4,11 +4,11 @@ import Data.Bifunctor (Bifunctor (first))
import Data.Maybe (fromJust)
import Data.Text (pack)
import qualified StrongPath as SP
import Test.Tasty.Hspec (Spec, describe, it, shouldBe, shouldMatchList)
import Test.Tasty.Hspec (Spec, anyErrorCall, describe, it, shouldBe, shouldMatchList, shouldReturn, shouldThrow)
import Wasp.Generator.FileDraft (FileDraft (FileDraftTextFd), Writeable (getDstPath))
import Wasp.Generator.FileDraft.TextFileDraft (TextFileDraft)
import qualified Wasp.Generator.FileDraft.TextFileDraft as TextFD
import Wasp.Generator.WriteFileDrafts (fileDraftsToWriteAndFilesToDelete)
import Wasp.Generator.WriteFileDrafts (assertDstPathsAreUnique, fileDraftsToWriteAndFilesToDelete)
import Wasp.Util (Checksum, checksumFromString, checksumFromText)
genMockTextFileDrafts :: Int -> [TextFileDraft]
@ -24,6 +24,16 @@ genFdsWithChecksums :: Int -> [(FileDraft, Checksum)]
genFdsWithChecksums n =
map (\fd -> (FileDraftTextFd fd, checksumFromText $ TextFD._content fd)) (genMockTextFileDrafts n)
spec_WriteDuplicatedDstFileDrafts :: Spec
spec_WriteDuplicatedDstFileDrafts =
describe "fileDraftsWithDuplicatedDstPaths" $ do
it "should throw error since there are duplicated destination paths" $
let fileDrafts = replicate 2 $ FileDraftTextFd $ head (genMockTextFileDrafts 1)
in (return $! assertDstPathsAreUnique fileDrafts) `shouldThrow` anyErrorCall
it "should not throw error because unique destination paths" $
let fileDrafts = map FileDraftTextFd (genMockTextFileDrafts 2)
in (return $! assertDstPathsAreUnique fileDrafts) `shouldReturn` ()
spec_WriteFileDrafts :: Spec
spec_WriteFileDrafts =
describe "fileDraftsToWriteAndFilesToDelete" $ do