phino-0.0.114: test/DepsSpec.hs
-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com
-- SPDX-License-Identifier: MIT
module DepsSpec where
import AST (Expression (ExRoot))
import Control.Exception (bracket)
import Control.Monad (when)
import Data.Time.Clock.POSIX (getPOSIXTime)
import Deps (dontSaveStep, saveStep)
import Logger (LogLevel (DEBUG, ERROR), setLogConfig)
import System.Directory
( doesDirectoryExist
, doesFileExist
, getTemporaryDirectory
, removeDirectoryRecursive
)
import System.FilePath ((</>))
import System.IO (stderr)
import System.IO.Silently (hSilence)
import Test.Hspec (Spec, describe, it, shouldBe)
withScratchDir :: (FilePath -> IO a) -> IO a
withScratchDir =
bracket
( do
tmp <- getTemporaryDirectory
stamp <- getPOSIXTime
pure (tmp </> ("phino-deps-spec-" ++ show (floor (stamp * 1000000) :: Integer)))
)
( \dir -> do
exists <- doesDirectoryExist dir
when exists (removeDirectoryRecursive dir)
)
spec :: Spec
spec = do
describe "dontSaveStep" $
it "is a no-op that never touches the filesystem" $
withScratchDir $ \dir -> do
dontSaveStep ExRoot
exists <- doesDirectoryExist dir
exists `shouldBe` False
describe "saveStep" $ do
it "creates the directory if missing, writes the rendered step and logs it" $ withScratchDir $ \dir -> do
setLogConfig DEBUG 25
hSilence [stderr] (saveStep (Just dir) "phi" (pure . show) 3 ExRoot)
setLogConfig ERROR 25
let path = dir </> "00003.phi"
exists <- doesFileExist path
exists `shouldBe` True
content <- readFile path
content `shouldBe` show ExRoot
it "numbers the file after the given step, zero padded to five digits" $ withScratchDir $ \dir -> do
saveStep (Just dir) "txt" (pure . show) 42 ExRoot
exists <- doesFileExist (dir </> "00042.txt")
exists `shouldBe` True