|
| 1 | +{-# LANGUAGE OverloadedStrings #-} |
| 2 | + |
| 3 | +module System.AtomicWriteSpec (spec) where |
| 4 | + |
| 5 | +import Test.Hspec (it, describe, shouldBe, Spec) |
| 6 | + |
| 7 | +import System.AtomicWrite (atomicWriteFile) |
| 8 | + |
| 9 | +import System.IO.Temp (withSystemTempDirectory) |
| 10 | +import System.FilePath.Posix (joinPath) |
| 11 | +import System.Posix.Files |
| 12 | + (setFileMode, setFileCreationMask, getFileStatus, fileMode) |
| 13 | + |
| 14 | + |
| 15 | +spec :: Spec |
| 16 | +spec = describe "atomicWriteFile" $ do |
| 17 | + it "writes contents to a file" $ |
| 18 | + withSystemTempDirectory "atomicFileTest" $ \tmpDir -> do |
| 19 | + |
| 20 | + let path = joinPath [ tmpDir, "writeTest.tmp" ] |
| 21 | + |
| 22 | + atomicWriteFile path "just testing" |
| 23 | + |
| 24 | + contents <- readFile path |
| 25 | + |
| 26 | + contents `shouldBe` "just testing" |
| 27 | + |
| 28 | + |
| 29 | + it "preserves the permissions of original file, regardless of umask" $ |
| 30 | + withSystemTempDirectory "atomicFileTest" $ \tmpDir -> do |
| 31 | + let |
| 32 | + filePath = joinPath [tmpDir, "testFile"] |
| 33 | + |
| 34 | + writeFile filePath "initial contents" |
| 35 | + setFileMode filePath 0o100644 |
| 36 | + |
| 37 | + newStat <- getFileStatus filePath |
| 38 | + fileMode newStat `shouldBe` 0o100644 |
| 39 | + |
| 40 | + -- New files are created with 100600 perms. |
| 41 | + _ <- setFileCreationMask 0o100066 |
| 42 | + |
| 43 | + -- Create a new file once different mask is set and make sure that mask |
| 44 | + -- is applied. |
| 45 | + writeFile (joinPath [tmpDir, "sanityCheck"]) "with sanity check mask" |
| 46 | + sanityCheckStat <- getFileStatus $ joinPath [tmpDir, "sanityCheck"] |
| 47 | + fileMode sanityCheckStat `shouldBe` 0o100600 |
| 48 | + |
| 49 | + -- Since we move, this makes the new file assume the filemask of 0600 |
| 50 | + atomicWriteFile filePath "new contents" |
| 51 | + |
| 52 | + resultStat <- getFileStatus filePath |
| 53 | + |
| 54 | + -- Fails when using atomic mv command unless apply perms on initial file |
| 55 | + fileMode resultStat `shouldBe` 0o100644 |
| 56 | + |
| 57 | + |
| 58 | + it "creates a new file with permissions based on active umask" $ |
| 59 | + withSystemTempDirectory "atomicFileTest" $ \tmpDir -> do |
| 60 | + let |
| 61 | + filePath = joinPath [tmpDir, "testFile"] |
| 62 | + sampleFilePath = joinPath [tmpDir, "sampleFile"] |
| 63 | + |
| 64 | + -- Set somewhat distinctive defaults for test |
| 65 | + _ <- setFileCreationMask 0o100171 |
| 66 | + |
| 67 | + -- We don't know what the default file permissions are, so create a |
| 68 | + -- file to sample them. |
| 69 | + writeFile sampleFilePath "I'm being written to sample permissions" |
| 70 | + |
| 71 | + newStat <- getFileStatus sampleFilePath |
| 72 | + fileMode newStat `shouldBe` 0o100606 |
| 73 | + |
| 74 | + atomicWriteFile filePath "new contents" |
| 75 | + |
| 76 | + resultStat <- getFileStatus filePath |
| 77 | + |
| 78 | + -- The default tempfile permissions are 0600, so this fails unless we |
| 79 | + -- make sure that the default umask is relied on for creation of the |
| 80 | + -- tempfile. |
| 81 | + fileMode resultStat `shouldBe` 0o100606 |
0 commit comments