From bb91eb75602153a36927768ccd2f915dc43922f5 Mon Sep 17 00:00:00 2001 From: Jasper Van der Jeugt Date: Sun, 26 Jan 2020 13:57:22 +0000 Subject: Small test tweaks * Use `withTestDirTree` in StylishSpec * Move StylishSpec module --- tests/Language/Haskell/Stylish/Config/Tests.hs | 41 +++++----------- tests/Language/Haskell/Stylish/Tests.hs | 66 ++++++++++++++++++++++++++ tests/Language/Haskell/Stylish/Tests/Util.hs | 44 +++++++++++++++++ tests/Language/Haskell/StylishSpec.hs | 51 -------------------- tests/TestSuite.hs | 6 +-- 5 files changed, 125 insertions(+), 83 deletions(-) create mode 100644 tests/Language/Haskell/Stylish/Tests.hs delete mode 100644 tests/Language/Haskell/StylishSpec.hs (limited to 'tests') 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/Stylish/Tests.hs b/tests/Language/Haskell/Stylish/Tests.hs new file mode 100644 index 0000000..3a27ce7 --- /dev/null +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -0,0 +1,66 @@ +-------------------------------------------------------------------------------- +module Language.Haskell.Stylish.Tests + ( tests + ) where + + +-------------------------------------------------------------------------------- +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 + + +-------------------------------------------------------------------------------- +tests :: Test +tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" + [ testCase "case 01" case01 + , testCase "case 02" case02 + , testCase "case 03" case03 + ] + + +-------------------------------------------------------------------------------- +case01 :: Assertion +case01 = (@?= result) =<< format Nothing Nothing input + where + input = "module Herp where\n data Foo = Bar | Baz" + result = Right [ "module Herp where" + , "data Foo = Bar" + , " | Baz" + ] + + +-------------------------------------------------------------------------------- +case02 :: Assertion +case02 = withTestDirTree $ do + writeFile "test-config.yaml" $ unlines + [ "steps:" + , " - records: {}" + , "indent: 2" + ] + + actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input + actual @?= result + where + input = "module Herp where\n data Foo = Bar | Baz" + result = Right [ "module Herp where" + , "data Foo = Bar" + , " | Baz" + ] + + +-------------------------------------------------------------------------------- +case03 :: Assertion +case03 = (@?= result) =<< format Nothing (Just fileLocation) input + where + fileLocation = "directory/File.hs" + input = "module Herp" + result = Left $ + "Language.Haskell.Stylish.Parse.parseModule: could not parse " <> + fileLocation <> + ": ParseFailed (SrcLoc \".hs\" 2 1) \"Parse error: EOF\"" 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,8 +1,21 @@ 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/Language/Haskell/StylishSpec.hs b/tests/Language/Haskell/StylishSpec.hs deleted file mode 100644 index 77a23b1..0000000 --- a/tests/Language/Haskell/StylishSpec.hs +++ /dev/null @@ -1,51 +0,0 @@ -module Language.Haskell.StylishSpec where - --------------------------------------------------------------------------------- -import Test.Framework (Test, testGroup) -import Test.Framework.Providers.HUnit (testCase) -import Test.HUnit (Assertion, (@?=)) - --------------------------------------------------------------------------------- -import Language.Haskell.Stylish - --------------------------------------------------------------------------------- -import System.IO.Unsafe --------------------------------------------------------------------------------- -tests :: Test -tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" - [ testCase "case 01" case01 - , testCase "case 02" case02 - , testCase "case 03" case03 - ] - --------------------------------------------------------------------------------- -case01 :: Assertion -case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input) - where - input = "module Herp where\n data Foo = Bar | Baz" - result = Right [ "module Herp where" - , "data Foo = Bar" - , " | Baz" - ] - -case02 :: Assertion -case02 = (@?=) result (unsafePerformIO $ format (Just configLocation) Nothing input) - 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 - where - fileLocation = "directory/File.hs" - input = "module Herp" - result = Left $ - "Language.Haskell.Stylish.Parse.parseModule: could not parse " <> - fileLocation <> - ": ParseFailed (SrcLoc \".hs\" 2 1) \"Parse error: EOF\"" 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 ] -- cgit v1.2.3