diff options
author | Jasper Van der Jeugt <m@jaspervdj.be> | 2020-01-26 13:57:22 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-26 13:57:22 +0000 |
commit | bb91eb75602153a36927768ccd2f915dc43922f5 (patch) | |
tree | 260eebaa4498da708eda22b5f7a2f814211acca5 | |
parent | 7b39c565e5e9c7493c256504e5a0a8c436ee97d6 (diff) | |
download | stylish-haskell-bb91eb75602153a36927768ccd2f915dc43922f5.tar.gz |
Small test tweaks
* Use `withTestDirTree` in StylishSpec
* Move StylishSpec module
-rw-r--r-- | stylish-haskell.cabal | 3 | ||||
-rw-r--r-- | testdata/test-config.yaml | 3 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Config/Tests.hs | 41 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Tests.hs (renamed from tests/Language/Haskell/StylishSpec.hs) | 39 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Tests/Util.hs | 44 | ||||
-rw-r--r-- | tests/TestSuite.hs | 6 |
6 files changed, 88 insertions, 48 deletions
diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal index 495e3b4..0ec6bc3 100644 --- a/stylish-haskell.cabal +++ b/stylish-haskell.cabal @@ -97,7 +97,6 @@ Test-suite stylish-haskell-tests Other-modules: Language.Haskell.Stylish - Language.Haskell.StylishSpec Language.Haskell.Stylish.Align Language.Haskell.Stylish.Block Language.Haskell.Stylish.Config @@ -124,9 +123,11 @@ Test-suite stylish-haskell-tests Language.Haskell.Stylish.Step.TrailingWhitespace.Tests Language.Haskell.Stylish.Step.UnicodeSyntax Language.Haskell.Stylish.Step.UnicodeSyntax.Tests + Language.Haskell.Stylish.Tests Language.Haskell.Stylish.Tests.Util Language.Haskell.Stylish.Util Language.Haskell.Stylish.Verbose + Paths_stylish_haskell Build-depends: HUnit >= 1.2 && < 1.7, diff --git a/testdata/test-config.yaml b/testdata/test-config.yaml deleted file mode 100644 index b81fdc2..0000000 --- a/testdata/test-config.yaml +++ /dev/null @@ -1,3 +0,0 @@ -steps: - - records: {} -indent: 2 diff --git a/tests/Language/Haskell/Stylish/Config/Tests.hs b/tests/Language/Haskell/Stylish/Config/Tests.hs index f8869ce..464ebb7 100644 --- a/tests/Language/Haskell/Stylish/Config/Tests.hs +++ b/tests/Language/Haskell/Stylish/Config/Tests.hs @@ -4,17 +4,17 @@ module Language.Haskell.Stylish.Config.Tests -------------------------------------------------------------------------------- -import Control.Exception hiding (assert) import qualified Data.Set as Set import System.Directory -import System.FilePath ((</>)) -import System.IO.Error -import System.Random import Test.Framework (Test, testGroup) import Test.Framework.Providers.HUnit (testCase) import Test.HUnit (Assertion, assert) + + -------------------------------------------------------------------------------- import Language.Haskell.Stylish.Config +import Language.Haskell.Stylish.Tests.Util + -------------------------------------------------------------------------------- tests :: Test @@ -32,33 +32,9 @@ tests = testGroup "Language.Haskell.Stylish.Config" , testCase "Correctly read .stylish-haskell.yaml file with no max column number" testNoColumns ] --------------------------------------------------------------------------------- --- | Create a temporary directory with a randomised name built from the template provided -createTempDirectory :: String -> IO FilePath -createTempDirectory template = do - tmpRootDir <- getTemporaryDirectory - dirId <- randomIO :: IO Word - findTempName tmpRootDir dirId - where - findTempName :: FilePath -> Word -> IO FilePath - findTempName tmpRootDir x = do - let dirpath = tmpRootDir </> template ++ show x - r <- try $ createDirectory dirpath - case r of - Right _ -> return dirpath - Left e | isAlreadyExistsError e -> findTempName tmpRootDir (x+1) - | otherwise -> ioError e - --- | Perform an action inside a temporary directory tree and purge the tree afterwords -withTestDirTree :: IO a -> IO a -withTestDirTree action = bracket - ((,) <$> getCurrentDirectory <*> createTempDirectory "stylish_haskell") - (\(current, temp) -> - setCurrentDirectory current *> - removeDirectoryRecursive temp) - (\(_, temp) -> setCurrentDirectory temp *> action) +-------------------------------------------------------------------------------- -- | Put an example config files (.cabal/.stylish-haskell.yaml/both) -- into the current directory and extract extensions from it. createFilesAndGetConfig :: [(FilePath, String)] -> IO Config @@ -72,6 +48,7 @@ createFilesAndGetConfig files = withTestDirTree $ do config <- loadConfig (const (pure ())) Nothing pure config + -------------------------------------------------------------------------------- testExtensionsFromDotCabal :: Assertion testExtensionsFromDotCabal = @@ -80,6 +57,7 @@ testExtensionsFromDotCabal = where expected = Set.fromList ["ScopedTypeVariables", "DataKinds"] + -------------------------------------------------------------------------------- testExtensionsFromDotStylish :: Assertion testExtensionsFromDotStylish = @@ -88,6 +66,7 @@ testExtensionsFromDotStylish = where expected = Set.fromList ["TemplateHaskell", "QuasiQuotes"] + -------------------------------------------------------------------------------- testExtensionsFromBoth :: Assertion testExtensionsFromBoth = @@ -98,6 +77,7 @@ testExtensionsFromBoth = expected = Set.fromList ["ScopedTypeVariables", "DataKinds", "TemplateHaskell", "QuasiQuotes"] + -------------------------------------------------------------------------------- testSpecifiedColumns :: Assertion testSpecifiedColumns = @@ -106,6 +86,7 @@ testSpecifiedColumns = where expected = Just 110 + -------------------------------------------------------------------------------- testDefaultColumns :: Assertion testDefaultColumns = @@ -114,6 +95,7 @@ testDefaultColumns = where expected = Just 80 + -------------------------------------------------------------------------------- testNoColumns :: Assertion testNoColumns = @@ -122,6 +104,7 @@ testNoColumns = where expected = Nothing + -- | Example cabal file borrowed from -- https://www.haskell.org/cabal/users-guide/developing-packages.html -- with some default-extensions added diff --git a/tests/Language/Haskell/StylishSpec.hs b/tests/Language/Haskell/Stylish/Tests.hs index 77a23b1..3a27ce7 100644 --- a/tests/Language/Haskell/StylishSpec.hs +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -1,15 +1,20 @@ -module Language.Haskell.StylishSpec where +-------------------------------------------------------------------------------- +module Language.Haskell.Stylish.Tests + ( tests + ) where + -------------------------------------------------------------------------------- -import Test.Framework (Test, testGroup) -import Test.Framework.Providers.HUnit (testCase) -import Test.HUnit (Assertion, (@?=)) +import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) +import Test.HUnit (Assertion, (@?=)) + -------------------------------------------------------------------------------- import Language.Haskell.Stylish +import Language.Haskell.Stylish.Tests.Util + --------------------------------------------------------------------------------- -import System.IO.Unsafe -------------------------------------------------------------------------------- tests :: Test tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" @@ -18,9 +23,10 @@ tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" , testCase "case 03" case03 ] + -------------------------------------------------------------------------------- case01 :: Assertion -case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input) +case01 = (@?= result) =<< format Nothing Nothing input where input = "module Herp where\n data Foo = Bar | Baz" result = Right [ "module Herp where" @@ -28,20 +34,29 @@ case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input) , " | Baz" ] + +-------------------------------------------------------------------------------- case02 :: Assertion -case02 = (@?=) result (unsafePerformIO $ format (Just configLocation) Nothing input) +case02 = withTestDirTree $ do + writeFile "test-config.yaml" $ unlines + [ "steps:" + , " - records: {}" + , "indent: 2" + ] + + actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input + actual @?= result where - configLocation = ConfigPath "testdata/test-config.yaml" input = "module Herp where\n data Foo = Bar | Baz" result = Right [ "module Herp where" , "data Foo = Bar" , " | Baz" ] + +-------------------------------------------------------------------------------- case03 :: Assertion -case03 = do - actual <- format Nothing (Just fileLocation) input - actual @?= result +case03 = (@?= result) =<< format Nothing (Just fileLocation) input where fileLocation = "directory/File.hs" input = "module Herp" diff --git a/tests/Language/Haskell/Stylish/Tests/Util.hs b/tests/Language/Haskell/Stylish/Tests/Util.hs index 40b5629..f43b6b5 100644 --- a/tests/Language/Haskell/Stylish/Tests/Util.hs +++ b/tests/Language/Haskell/Stylish/Tests/Util.hs @@ -1,9 +1,22 @@ module Language.Haskell.Stylish.Tests.Util ( testStep + , withTestDirTree ) where -------------------------------------------------------------------------------- +import Control.Exception (bracket, try) +import System.Directory (createDirectory, + getCurrentDirectory, + getTemporaryDirectory, + removeDirectoryRecursive, + setCurrentDirectory) +import System.FilePath ((</>)) +import System.IO.Error (isAlreadyExistsError) +import System.Random (randomIO) + + +-------------------------------------------------------------------------------- import Language.Haskell.Stylish.Parse import Language.Haskell.Stylish.Step @@ -15,3 +28,34 @@ testStep step str = case parseModule [] Nothing str of Right module' -> unlines $ stepFilter step ls module' where ls = lines str + + +-------------------------------------------------------------------------------- +-- | Create a temporary directory with a randomised name built from the template +-- provided +createTempDirectory :: String -> IO FilePath +createTempDirectory template = do + tmpRootDir <- getTemporaryDirectory + dirId <- randomIO :: IO Word + findTempName tmpRootDir dirId + where + findTempName :: FilePath -> Word -> IO FilePath + findTempName tmpRootDir x = do + let dirpath = tmpRootDir </> template ++ show x + r <- try $ createDirectory dirpath + case r of + Right _ -> return dirpath + Left e | isAlreadyExistsError e -> findTempName tmpRootDir (x+1) + | otherwise -> ioError e + + +-------------------------------------------------------------------------------- +-- | Perform an action inside a temporary directory tree and purge the tree +-- afterwards +withTestDirTree :: IO a -> IO a +withTestDirTree action = bracket + ((,) <$> getCurrentDirectory <*> createTempDirectory "stylish_haskell") + (\(current, temp) -> + setCurrentDirectory current *> + removeDirectoryRecursive temp) + (\(_, temp) -> setCurrentDirectory temp *> action) diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs index 1138323..d2023ed 100644 --- a/tests/TestSuite.hs +++ b/tests/TestSuite.hs @@ -19,14 +19,13 @@ import qualified Language.Haskell.Stylish.Step.Squash.Tests import qualified Language.Haskell.Stylish.Step.Tabs.Tests import qualified Language.Haskell.Stylish.Step.TrailingWhitespace.Tests import qualified Language.Haskell.Stylish.Step.UnicodeSyntax.Tests -import qualified Language.Haskell.StylishSpec +import qualified Language.Haskell.Stylish.Tests -------------------------------------------------------------------------------- main :: IO () main = defaultMain - [ Language.Haskell.StylishSpec.tests - , Language.Haskell.Stylish.Parse.Tests.tests + [ Language.Haskell.Stylish.Parse.Tests.tests , Language.Haskell.Stylish.Config.Tests.tests , Language.Haskell.Stylish.Step.Data.Tests.tests , Language.Haskell.Stylish.Step.Imports.Tests.tests @@ -36,4 +35,5 @@ main = defaultMain , Language.Haskell.Stylish.Step.Tabs.Tests.tests , Language.Haskell.Stylish.Step.TrailingWhitespace.Tests.tests , Language.Haskell.Stylish.Step.UnicodeSyntax.Tests.tests + , Language.Haskell.Stylish.Tests.tests ] |