Skip to content

Commit bb91eb7

Browse files
authored
Small test tweaks
* Use `withTestDirTree` in StylishSpec * Move StylishSpec module
1 parent 7b39c56 commit bb91eb7

File tree

6 files changed

+88
-48
lines changed

6 files changed

+88
-48
lines changed

stylish-haskell.cabal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ Test-suite stylish-haskell-tests
9797

9898
Other-modules:
9999
Language.Haskell.Stylish
100-
Language.Haskell.StylishSpec
101100
Language.Haskell.Stylish.Align
102101
Language.Haskell.Stylish.Block
103102
Language.Haskell.Stylish.Config
@@ -124,9 +123,11 @@ Test-suite stylish-haskell-tests
124123
Language.Haskell.Stylish.Step.TrailingWhitespace.Tests
125124
Language.Haskell.Stylish.Step.UnicodeSyntax
126125
Language.Haskell.Stylish.Step.UnicodeSyntax.Tests
126+
Language.Haskell.Stylish.Tests
127127
Language.Haskell.Stylish.Tests.Util
128128
Language.Haskell.Stylish.Util
129129
Language.Haskell.Stylish.Verbose
130+
Paths_stylish_haskell
130131

131132
Build-depends:
132133
HUnit >= 1.2 && < 1.7,

testdata/test-config.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

tests/Language/Haskell/Stylish/Config/Tests.hs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ module Language.Haskell.Stylish.Config.Tests
44

55

66
--------------------------------------------------------------------------------
7-
import Control.Exception hiding (assert)
87
import qualified Data.Set as Set
98
import System.Directory
10-
import System.FilePath ((</>))
11-
import System.IO.Error
12-
import System.Random
139
import Test.Framework (Test, testGroup)
1410
import Test.Framework.Providers.HUnit (testCase)
1511
import Test.HUnit (Assertion, assert)
12+
13+
1614
--------------------------------------------------------------------------------
1715
import Language.Haskell.Stylish.Config
16+
import Language.Haskell.Stylish.Tests.Util
17+
1818

1919
--------------------------------------------------------------------------------
2020
tests :: Test
@@ -32,33 +32,9 @@ tests = testGroup "Language.Haskell.Stylish.Config"
3232
, testCase "Correctly read .stylish-haskell.yaml file with no max column number"
3333
testNoColumns
3434
]
35-
--------------------------------------------------------------------------------
3635

37-
-- | Create a temporary directory with a randomised name built from the template provided
38-
createTempDirectory :: String -> IO FilePath
39-
createTempDirectory template = do
40-
tmpRootDir <- getTemporaryDirectory
41-
dirId <- randomIO :: IO Word
42-
findTempName tmpRootDir dirId
43-
where
44-
findTempName :: FilePath -> Word -> IO FilePath
45-
findTempName tmpRootDir x = do
46-
let dirpath = tmpRootDir </> template ++ show x
47-
r <- try $ createDirectory dirpath
48-
case r of
49-
Right _ -> return dirpath
50-
Left e | isAlreadyExistsError e -> findTempName tmpRootDir (x+1)
51-
| otherwise -> ioError e
52-
53-
-- | Perform an action inside a temporary directory tree and purge the tree afterwords
54-
withTestDirTree :: IO a -> IO a
55-
withTestDirTree action = bracket
56-
((,) <$> getCurrentDirectory <*> createTempDirectory "stylish_haskell")
57-
(\(current, temp) ->
58-
setCurrentDirectory current *>
59-
removeDirectoryRecursive temp)
60-
(\(_, temp) -> setCurrentDirectory temp *> action)
6136

37+
--------------------------------------------------------------------------------
6238
-- | Put an example config files (.cabal/.stylish-haskell.yaml/both)
6339
-- into the current directory and extract extensions from it.
6440
createFilesAndGetConfig :: [(FilePath, String)] -> IO Config
@@ -72,6 +48,7 @@ createFilesAndGetConfig files = withTestDirTree $ do
7248
config <- loadConfig (const (pure ())) Nothing
7349
pure config
7450

51+
7552
--------------------------------------------------------------------------------
7653
testExtensionsFromDotCabal :: Assertion
7754
testExtensionsFromDotCabal =
@@ -80,6 +57,7 @@ testExtensionsFromDotCabal =
8057
where
8158
expected = Set.fromList ["ScopedTypeVariables", "DataKinds"]
8259

60+
8361
--------------------------------------------------------------------------------
8462
testExtensionsFromDotStylish :: Assertion
8563
testExtensionsFromDotStylish =
@@ -88,6 +66,7 @@ testExtensionsFromDotStylish =
8866
where
8967
expected = Set.fromList ["TemplateHaskell", "QuasiQuotes"]
9068

69+
9170
--------------------------------------------------------------------------------
9271
testExtensionsFromBoth :: Assertion
9372
testExtensionsFromBoth =
@@ -98,6 +77,7 @@ testExtensionsFromBoth =
9877
expected = Set.fromList
9978
["ScopedTypeVariables", "DataKinds", "TemplateHaskell", "QuasiQuotes"]
10079

80+
10181
--------------------------------------------------------------------------------
10282
testSpecifiedColumns :: Assertion
10383
testSpecifiedColumns =
@@ -106,6 +86,7 @@ testSpecifiedColumns =
10686
where
10787
expected = Just 110
10888

89+
10990
--------------------------------------------------------------------------------
11091
testDefaultColumns :: Assertion
11192
testDefaultColumns =
@@ -114,6 +95,7 @@ testDefaultColumns =
11495
where
11596
expected = Just 80
11697

98+
11799
--------------------------------------------------------------------------------
118100
testNoColumns :: Assertion
119101
testNoColumns =
@@ -122,6 +104,7 @@ testNoColumns =
122104
where
123105
expected = Nothing
124106

107+
125108
-- | Example cabal file borrowed from
126109
-- https://www.haskell.org/cabal/users-guide/developing-packages.html
127110
-- with some default-extensions added

tests/Language/Haskell/StylishSpec.hs renamed to tests/Language/Haskell/Stylish/Tests.hs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
module Language.Haskell.StylishSpec where
1+
--------------------------------------------------------------------------------
2+
module Language.Haskell.Stylish.Tests
3+
( tests
4+
) where
5+
26

37
--------------------------------------------------------------------------------
4-
import Test.Framework (Test, testGroup)
5-
import Test.Framework.Providers.HUnit (testCase)
6-
import Test.HUnit (Assertion, (@?=))
8+
import Test.Framework (Test, testGroup)
9+
import Test.Framework.Providers.HUnit (testCase)
10+
import Test.HUnit (Assertion, (@?=))
11+
712

813
--------------------------------------------------------------------------------
914
import Language.Haskell.Stylish
15+
import Language.Haskell.Stylish.Tests.Util
16+
1017

11-
--------------------------------------------------------------------------------
12-
import System.IO.Unsafe
1318
--------------------------------------------------------------------------------
1419
tests :: Test
1520
tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests"
@@ -18,30 +23,40 @@ tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests"
1823
, testCase "case 03" case03
1924
]
2025

26+
2127
--------------------------------------------------------------------------------
2228
case01 :: Assertion
23-
case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input)
29+
case01 = (@?= result) =<< format Nothing Nothing input
2430
where
2531
input = "module Herp where\n data Foo = Bar | Baz"
2632
result = Right [ "module Herp where"
2733
, "data Foo = Bar"
2834
, " | Baz"
2935
]
3036

37+
38+
--------------------------------------------------------------------------------
3139
case02 :: Assertion
32-
case02 = (@?=) result (unsafePerformIO $ format (Just configLocation) Nothing input)
40+
case02 = withTestDirTree $ do
41+
writeFile "test-config.yaml" $ unlines
42+
[ "steps:"
43+
, " - records: {}"
44+
, "indent: 2"
45+
]
46+
47+
actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input
48+
actual @?= result
3349
where
34-
configLocation = ConfigPath "testdata/test-config.yaml"
3550
input = "module Herp where\n data Foo = Bar | Baz"
3651
result = Right [ "module Herp where"
3752
, "data Foo = Bar"
3853
, " | Baz"
3954
]
4055

56+
57+
--------------------------------------------------------------------------------
4158
case03 :: Assertion
42-
case03 = do
43-
actual <- format Nothing (Just fileLocation) input
44-
actual @?= result
59+
case03 = (@?= result) =<< format Nothing (Just fileLocation) input
4560
where
4661
fileLocation = "directory/File.hs"
4762
input = "module Herp"

tests/Language/Haskell/Stylish/Tests/Util.hs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
module Language.Haskell.Stylish.Tests.Util
22
( testStep
3+
, withTestDirTree
34
) where
45

56

7+
--------------------------------------------------------------------------------
8+
import Control.Exception (bracket, try)
9+
import System.Directory (createDirectory,
10+
getCurrentDirectory,
11+
getTemporaryDirectory,
12+
removeDirectoryRecursive,
13+
setCurrentDirectory)
14+
import System.FilePath ((</>))
15+
import System.IO.Error (isAlreadyExistsError)
16+
import System.Random (randomIO)
17+
18+
619
--------------------------------------------------------------------------------
720
import Language.Haskell.Stylish.Parse
821
import Language.Haskell.Stylish.Step
@@ -15,3 +28,34 @@ testStep step str = case parseModule [] Nothing str of
1528
Right module' -> unlines $ stepFilter step ls module'
1629
where
1730
ls = lines str
31+
32+
33+
--------------------------------------------------------------------------------
34+
-- | Create a temporary directory with a randomised name built from the template
35+
-- provided
36+
createTempDirectory :: String -> IO FilePath
37+
createTempDirectory template = do
38+
tmpRootDir <- getTemporaryDirectory
39+
dirId <- randomIO :: IO Word
40+
findTempName tmpRootDir dirId
41+
where
42+
findTempName :: FilePath -> Word -> IO FilePath
43+
findTempName tmpRootDir x = do
44+
let dirpath = tmpRootDir </> template ++ show x
45+
r <- try $ createDirectory dirpath
46+
case r of
47+
Right _ -> return dirpath
48+
Left e | isAlreadyExistsError e -> findTempName tmpRootDir (x+1)
49+
| otherwise -> ioError e
50+
51+
52+
--------------------------------------------------------------------------------
53+
-- | Perform an action inside a temporary directory tree and purge the tree
54+
-- afterwards
55+
withTestDirTree :: IO a -> IO a
56+
withTestDirTree action = bracket
57+
((,) <$> getCurrentDirectory <*> createTempDirectory "stylish_haskell")
58+
(\(current, temp) ->
59+
setCurrentDirectory current *>
60+
removeDirectoryRecursive temp)
61+
(\(_, temp) -> setCurrentDirectory temp *> action)

tests/TestSuite.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ import qualified Language.Haskell.Stylish.Step.Squash.Tests
1919
import qualified Language.Haskell.Stylish.Step.Tabs.Tests
2020
import qualified Language.Haskell.Stylish.Step.TrailingWhitespace.Tests
2121
import qualified Language.Haskell.Stylish.Step.UnicodeSyntax.Tests
22-
import qualified Language.Haskell.StylishSpec
22+
import qualified Language.Haskell.Stylish.Tests
2323

2424

2525
--------------------------------------------------------------------------------
2626
main :: IO ()
2727
main = defaultMain
28-
[ Language.Haskell.StylishSpec.tests
29-
, Language.Haskell.Stylish.Parse.Tests.tests
28+
[ Language.Haskell.Stylish.Parse.Tests.tests
3029
, Language.Haskell.Stylish.Config.Tests.tests
3130
, Language.Haskell.Stylish.Step.Data.Tests.tests
3231
, Language.Haskell.Stylish.Step.Imports.Tests.tests
@@ -36,4 +35,5 @@ main = defaultMain
3635
, Language.Haskell.Stylish.Step.Tabs.Tests.tests
3736
, Language.Haskell.Stylish.Step.TrailingWhitespace.Tests.tests
3837
, Language.Haskell.Stylish.Step.UnicodeSyntax.Tests.tests
38+
, Language.Haskell.Stylish.Tests.tests
3939
]

0 commit comments

Comments
 (0)