|
| 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 (dropWhileEnd) |
6 | 9 | import Data.String (fromString) |
7 | 10 | import Language.Nix.Identifier |
| 11 | +import System.Process (callProcess, readCreateProcess, proc) |
8 | 12 | import Test.Hspec |
9 | 13 | import Test.QuickCheck |
10 | 14 | import Text.Parsec.Class (parseM) |
@@ -51,10 +55,49 @@ main = hspec $ do |
51 | 55 | any isSpace s ==> needsQuoting s |
52 | 56 | it "if length is zero" $ shouldSatisfy "" needsQuoting |
53 | 57 |
|
| 58 | + describe "nix-instantiate" $ do |
| 59 | + nixInstantiate <- runIO $ do |
| 60 | + (callProcess nixInstantiateBin [ "--version" ] >> pure (Just nixInstantiateBin)) |
| 61 | + `catch` (\(_ :: SomeException) -> pure Nothing) |
| 62 | + let nix :: Example a => String -> (String -> a) -> SpecWith (Arg a) |
| 63 | + nix str spec = |
| 64 | + case nixInstantiate of |
| 65 | + Nothing -> it str $ \_ -> |
| 66 | + pendingWith (nixInstantiateBin ++ " could not be found or executed") |
| 67 | + Just exec -> it str $ spec exec |
| 68 | + |
| 69 | + nix "parses and produces result of quote" $ \exec -> stringIdentProperty $ \str -> ioProperty $ do |
| 70 | + let expAttr = quote str |
| 71 | + expr = "{" ++ expAttr ++ "=null;}" |
| 72 | + |
| 73 | + out <- readCreateProcess (proc exec ["--eval", "--strict", "-E", expr]) "" |
| 74 | + pure $ extractIdentSyntax out === expAttr |
| 75 | + |
| 76 | + nix "produces parseM-able identifiers" $ \exec -> identProperty $ \i -> ioProperty $ do |
| 77 | + let expr = "{" ++ prettyShow i ++ "=null;}" |
| 78 | + out <- readCreateProcess (proc exec ["--eval", "--strict", "-E", expr]) "" |
| 79 | + pure $ parseM "Identifier" (extractIdentSyntax out) == Just i |
| 80 | + |
| 81 | +nixInstantiateBin :: String |
| 82 | +nixInstantiateBin = "nix-instantiate" |
| 83 | + |
54 | 84 | stringIdentProperty :: Testable prop => (String -> prop) -> Property |
55 | 85 | stringIdentProperty p = property $ \s -> |
56 | 86 | '\0' `notElem` s ==> classify (needsQuoting s) "need quoting" $ p s |
57 | 87 |
|
58 | 88 | identProperty :: Testable prop => (Identifier -> prop) -> Property |
59 | 89 | identProperty p = property $ \i -> |
60 | 90 | classify (needsQuoting (from ident # i)) "need quoting" $ p i |
| 91 | + |
| 92 | +-- | Given the (pretty) printed representation of the Nix value produced by the |
| 93 | +-- expression @{ ${ident} = null; }@, for any value of @ident@, extract the |
| 94 | +-- part that represents the identifier. |
| 95 | +-- |
| 96 | +-- Note that pretty printing is buggy in some versions of Nix and the result |
| 97 | +-- may not actually be valid Nix syntax. |
| 98 | +extractIdentSyntax :: String -> String |
| 99 | +extractIdentSyntax = |
| 100 | + dropWhileEnd (`elem` "= \n\t") -- remove "… = " |
| 101 | + . dropWhileEnd (`elem` "null") -- remove "null" |
| 102 | + . dropWhileEnd (`elem` ";} \n\t") -- remove "…; }" |
| 103 | + . dropWhile (`elem` "{ \n\t") -- remove "{ …" |
0 commit comments