2018-04-11 22:56:31 +02:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2014-08-03 14:17:43 +02:00
|
|
|
module Main where
|
|
|
|
|
2018-04-11 22:56:31 +02:00
|
|
|
import Control.DeepSeq
|
|
|
|
import qualified Control.Exception as Exc
|
2018-04-05 00:32:21 +02:00
|
|
|
import Control.Monad
|
2018-04-11 22:56:31 +02:00
|
|
|
import Data.Fix
|
2018-04-07 21:09:49 +02:00
|
|
|
import Data.List (isInfixOf)
|
2018-04-14 07:34:24 +02:00
|
|
|
import Data.Maybe (isJust)
|
2018-04-11 22:56:31 +02:00
|
|
|
import Data.String.Interpolate.IsString
|
|
|
|
import Data.Text (unpack)
|
2015-12-21 06:55:53 +01:00
|
|
|
import qualified EvalTests
|
2018-04-11 22:56:31 +02:00
|
|
|
import qualified Nix
|
2018-04-14 04:29:18 +02:00
|
|
|
import Nix.Exec
|
2018-04-11 22:56:31 +02:00
|
|
|
import Nix.Expr.Types
|
2018-04-18 02:25:59 +02:00
|
|
|
import Nix.Options
|
2018-04-11 22:56:31 +02:00
|
|
|
import Nix.Parser
|
|
|
|
import Nix.Stack
|
|
|
|
import Nix.Value
|
2018-02-09 15:32:53 +01:00
|
|
|
import qualified NixLanguageTests
|
2018-03-28 06:59:27 +02:00
|
|
|
import qualified ParserTests
|
|
|
|
import qualified PrettyTests
|
2018-03-29 01:30:54 +02:00
|
|
|
import System.Environment
|
2018-04-11 22:56:31 +02:00
|
|
|
import System.FilePath.Glob
|
2018-04-07 21:02:33 +02:00
|
|
|
import System.Posix.Files
|
2018-04-07 21:09:49 +02:00
|
|
|
import System.Process
|
2018-03-28 06:59:27 +02:00
|
|
|
import Test.Tasty
|
2018-04-10 20:05:22 +02:00
|
|
|
import Test.Tasty.HUnit
|
2016-01-23 22:46:30 +01:00
|
|
|
|
2018-04-10 20:05:22 +02:00
|
|
|
cabalCorrectlyGenerated :: Assertion
|
|
|
|
cabalCorrectlyGenerated = do
|
|
|
|
output <- readCreateProcess (shell "hpack") ""
|
|
|
|
when ("modified manually" `isInfixOf` output) $
|
|
|
|
errorWithoutStackTrace
|
|
|
|
"Edit package.yaml and re-generate hnix.cabal by running \"hpack\""
|
2018-04-07 21:09:49 +02:00
|
|
|
|
2018-04-10 20:05:22 +02:00
|
|
|
ensureLangTestsPresent :: Assertion
|
|
|
|
ensureLangTestsPresent = do
|
2018-04-11 22:56:31 +02:00
|
|
|
exist <- fileExist "data/nix/tests/local.mk"
|
|
|
|
unless exist $
|
|
|
|
errorWithoutStackTrace $ unlines
|
|
|
|
[ "Directory data/nix does not have any files."
|
|
|
|
, "Did you forget to run"
|
|
|
|
++ " \"git submodule update --init --recursive\"?" ]
|
2018-04-07 21:09:49 +02:00
|
|
|
|
2018-04-10 20:05:22 +02:00
|
|
|
ensureNixpkgsCanParse :: Assertion
|
2018-04-11 22:56:31 +02:00
|
|
|
ensureNixpkgsCanParse =
|
|
|
|
consider "default.nix" (parseNixFile "default.nix") $ \case
|
|
|
|
Fix (NAbs (ParamSet params _ _) _) -> do
|
|
|
|
let rev = getString "rev" params
|
|
|
|
sha256 = getString "sha256" params
|
|
|
|
consider "fetchTarball expression" (pure $ parseNixTextLoc [i|
|
|
|
|
builtins.fetchTarball {
|
|
|
|
url = "https://github.com/NixOS/nixpkgs/archive/#{rev}.tar.gz";
|
|
|
|
sha256 = "#{sha256}";
|
|
|
|
}|]) $ \expr -> do
|
2018-04-18 02:25:59 +02:00
|
|
|
NVStr dir _ <- runLazyM defaultOptions $ Nix.evalLoc Nothing expr
|
2018-04-11 22:56:31 +02:00
|
|
|
files <- globDir1 (compile "**/*.nix") (unpack dir)
|
|
|
|
forM_ files $ \file ->
|
|
|
|
-- Parse and deepseq the resulting expression tree, to ensure the
|
|
|
|
-- parser is fully executed.
|
|
|
|
consider file (parseNixFileLoc file) $ Exc.evaluate . force
|
|
|
|
v -> error $ "Unexpected parse from default.nix: " ++ show v
|
|
|
|
where
|
|
|
|
getExpr k m = let Just (Just r) = lookup k m in r
|
|
|
|
getString k m =
|
|
|
|
let Fix (NStr (DoubleQuoted [Plain str])) = getExpr k m in str
|
|
|
|
|
|
|
|
consider path action k = action >>= \case
|
|
|
|
Failure err -> errorWithoutStackTrace $
|
|
|
|
"Parsing " ++ path ++ " failed: " ++ show err
|
|
|
|
Success expr -> Exc.catch (k expr) $ \case
|
|
|
|
NixEvalException msg -> errorWithoutStackTrace msg
|
2018-04-07 21:09:49 +02:00
|
|
|
|
2018-04-10 20:05:22 +02:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2018-04-11 22:56:31 +02:00
|
|
|
nixLanguageTests <- NixLanguageTests.genTests
|
2018-04-08 09:26:48 +02:00
|
|
|
evalComparisonTests <- EvalTests.genEvalCompareTests
|
2018-04-11 22:56:31 +02:00
|
|
|
langTestsEnv <- lookupEnv "LANGUAGE_TESTS"
|
|
|
|
nixpkgsTestsEnv <- lookupEnv "NIXPKGS_TESTS"
|
2018-04-14 07:34:24 +02:00
|
|
|
let runLangTests = isJust langTestsEnv
|
|
|
|
let runNixpkgsTests = isJust nixpkgsTestsEnv
|
2018-04-11 22:56:31 +02:00
|
|
|
|
2018-03-29 01:30:54 +02:00
|
|
|
defaultMain $ testGroup "hnix" $
|
2018-04-10 20:05:22 +02:00
|
|
|
[ testCase "hnix.cabal correctly generated" cabalCorrectlyGenerated ] ++
|
2018-02-09 15:32:53 +01:00
|
|
|
[ ParserTests.tests
|
|
|
|
, EvalTests.tests
|
|
|
|
, PrettyTests.tests
|
2018-04-11 22:56:31 +02:00
|
|
|
, evalComparisonTests ] ++
|
|
|
|
[ testCase "Nix languarge tests present" ensureLangTestsPresent
|
|
|
|
| runLangTests ] ++
|
2018-04-10 20:05:22 +02:00
|
|
|
[ nixLanguageTests | runLangTests ] ++
|
2018-04-11 22:56:31 +02:00
|
|
|
[ testCase "Nixpkgs parses without errors" ensureNixpkgsCanParse
|
|
|
|
| runNixpkgsTests ]
|