|
| 1 | +{-# LANGUAGE ScopedTypeVariables #-} |
1 | 2 | module Main (main) where |
2 | 3 |
|
| 4 | +import Control.Exception |
3 | 5 | import Control.Lens |
4 | 6 | import Control.Monad (forM_) |
5 | 7 | import Data.Char (isAscii, isSpace) |
| 8 | +import Data.List (dropWhile, dropWhileEnd) |
6 | 9 | import Data.String (fromString) |
7 | 10 | import Language.Nix.Identifier |
| 11 | +import System.Exit (ExitCode (..)) |
| 12 | +import System.Process (callProcess, readCreateProcess, proc) |
8 | 13 | import Test.Hspec |
9 | 14 | import Test.QuickCheck |
10 | 15 | import Text.Parsec.Class (parseM) |
11 | 16 | import Text.PrettyPrint.HughesPJClass (prettyShow) |
12 | 17 |
|
13 | 18 | main :: IO () |
14 | | -main = hspec $ do |
| 19 | +main = do |
| 20 | + let nixInstantiate = "nix-instantiate" |
| 21 | + nixInstantiateWorks <- catch |
| 22 | + (callProcess nixInstantiate [ "--version" ] >> pure True) |
| 23 | + (\(e :: SomeException) -> pure False) |
| 24 | + |
| 25 | + spec $ if nixInstantiateWorks then Just nixInstantiate else Nothing |
| 26 | + |
| 27 | +spec :: Maybe FilePath -> IO () |
| 28 | +spec nixInstantiate = hspec $ do |
15 | 29 | describe "Language.Nix.Identifier" $ do |
16 | 30 | describe "ident" $ do |
17 | 31 | it "is equivalent to fromString" $ |
@@ -39,6 +53,26 @@ main = hspec $ do |
39 | 53 | any isSpace s ==> needsQuoting s |
40 | 54 | it "if length is zero" $ shouldSatisfy "" needsQuoting |
41 | 55 |
|
| 56 | + describe "nix-instantiate" $ do |
| 57 | + let nit :: Example a => String -> (String -> a) -> SpecWith (Arg a) |
| 58 | + nit str spec = |
| 59 | + case nixInstantiate of |
| 60 | + Nothing -> xit str $ spec undefined |
| 61 | + Just exec -> it str $ spec exec |
| 62 | + |
| 63 | +-- TODO: parseM (nix-instantiate (prettyShow i)) |
| 64 | + nit "test" $ \exec -> stringIdentProperty $ \str -> ioProperty $ do |
| 65 | + let expAttr = quote str |
| 66 | + extractAttr = |
| 67 | + dropWhileEnd (`elem` "= \n\t") -- remove "… = " |
| 68 | + . dropWhileEnd (`elem` "null") -- remove "null" |
| 69 | + . dropWhileEnd (`elem` ";} \n\t") -- remove "…; }" |
| 70 | + . dropWhile (`elem` "{ \n\t") -- remove "{ …" |
| 71 | + expr = "{" ++ expAttr ++ "=null;}" |
| 72 | + |
| 73 | + out <- readCreateProcess (proc exec ["--eval", "--strict", "-E", expr]) "" |
| 74 | + pure $ extractAttr out === expAttr |
| 75 | + |
42 | 76 | stringIdentProperty :: Testable prop => (String -> prop) -> Property |
43 | 77 | stringIdentProperty p = property $ \s -> |
44 | 78 | '\0' `notElem` s ==> classify (needsQuoting s) "need quoting" $ p s |
|
0 commit comments