Skip to content

Use neat-interpolation instead of interpolate #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ let
name = "hnix";
root = pkgs.nix-gitignore.gitignoreSource [ ] ./.;

overrides = self: super: {
# 2020-06-26 Due to a behaviour change in neat-interpolation-0.4, we
# require n-i >= 0.4. dontCheck helps us avoid conflicts with
# neat-interpolation's test dependencies.
neat-interpolation = pkgs.haskell.lib.dontCheck super.neat-interpolation_0_5_1;
};

modifier = drv: pkgs.haskell.lib.overrideCabal drv (attrs: {
buildTools = (attrs.buildTools or []) ++ [
haskellPackages.cabal-install
Expand Down Expand Up @@ -249,4 +256,3 @@ let

in composedPackage


4 changes: 2 additions & 2 deletions hnix.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ library
, http-client >= 0.5.14 && < 0.6 || >= 0.6.4 && < 0.8
, http-client-tls >= 0.3.5 && < 0.4
, http-types >= 0.12.2 && < 0.13
, interpolate >= 0.2.0 && < 0.3
, lens-family >=1.2.2 && < 2.2
, lens-family-core >=1.2.2 && < 2.2
, lens-family-th >= 0.5.0 && < 0.6
Expand All @@ -895,6 +894,7 @@ library
, monad-control >= 1.0.2 && < 1.1
, monadlist >= 0.0.2 && < 0.1
, mtl >= 2.2.2 && < 2.3
, neat-interpolation >= 0.4 && < 0.6
, optparse-applicative >= 0.14.3 && < 0.15 || >= 0.15.0.0 && < 0.16
, parser-combinators >= 1.0.1 && < 1.3
, prettyprinter >= 1.2.1 && < 1.7
Expand Down Expand Up @@ -1004,9 +1004,9 @@ test-suite hnix-tests
, filepath
, hedgehog
, hnix
, interpolate
, megaparsec >=7.0 && <8.1
, mtl
, neat-interpolation
, optparse-applicative
, pretty-show
, prettyprinter
Expand Down
4 changes: 2 additions & 2 deletions src/Nix/Builtins.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import Data.Maybe
import Data.Scientific
import Data.Set ( Set )
import qualified Data.Set as S
import Data.String.Interpolate.IsString
import Data.Text ( Text )
import qualified Data.Text as Text
import Data.Text.Encoding
Expand All @@ -61,6 +60,7 @@ import Data.These ( fromThese )
import qualified Data.Time.Clock.POSIX as Time
import Data.Traversable ( for )
import qualified Data.Vector as V
import NeatInterpolation ( text )
import Nix.Atoms
import Nix.Convert
import Nix.Effects
Expand Down Expand Up @@ -170,7 +170,7 @@ builtinsList = sequence
-- This is compiled in so that we only parse and evaluate it once, at
-- compile-time.
, add0 TopLevel "derivation" $(do
let Success expr = parseNixText [i|
let Success expr = parseNixText [text|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this change will result in less leading whitespace in the interpolated text. I don't think it hurts in this case though.

drvAttrs @ { outputs ? [ "out" ], ... }:

let
Expand Down
69 changes: 37 additions & 32 deletions tests/EvalTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import Control.Monad.IO.Class
import Data.List ((\\))
import Data.Maybe (isJust)
import qualified Data.Set as S
import Data.String.Interpolate.IsString
import Data.Text (Text)
import Data.Time
import NeatInterpolation (text)
import Nix
import Nix.Standard
import Nix.TH
Expand Down Expand Up @@ -90,7 +90,7 @@ case_function_recursive_args =
constantEqualText "2" "({ x ? 1, y ? x * 3}: y - x) {}"

case_function_recursive_sets =
constantEqualText "[ [ 6 4 100 ] 4 ]" [i|
constantEqualText "[ [ 6 4 100 ] 4 ]" [text|
let x = rec {

y = 2;
Expand Down Expand Up @@ -152,15 +152,15 @@ case_lang_version =
constantEqualText "5" "builtins.langVersion"

case_rec_set_attr_path_simpl =
constantEqualText "123" [i|
constantEqualText "123" [text|
let x = rec {
foo.number = 123;
foo.function = y: foo.number;
}; in x.foo.function 1
|]

case_inherit_from_set_has_no_scope =
constantEqualText' "false" [i|
constantEqualText' "false" [text|
(builtins.tryEval (
let x = 1;
y = { z = 2; };
Expand All @@ -171,7 +171,7 @@ case_inherit_from_set_has_no_scope =
-- github/orblivion (2018-08-05): Adding these failing tests so we fix this feature

-- case_overrides =
-- constantEqualText' "2" [i|
-- constantEqualText' "2" [text|
-- let
--
-- overrides = { a = 2; };
Expand All @@ -184,7 +184,7 @@ case_inherit_from_set_has_no_scope =
-- |]

-- case_inherit_overrides =
-- constantEqualText' "2" [i|
-- constantEqualText' "2" [text|
-- let
--
-- __overrides = { a = 2; };
Expand All @@ -197,7 +197,7 @@ case_inherit_from_set_has_no_scope =
-- |]

case_unsafegetattrpos1 =
constantEqualText "[ 6 20 ]" [i|
constantEqualText "[ 5 14 ]" [text|
let e = 1;
f = 1;
t = {};
Expand All @@ -212,7 +212,7 @@ case_unsafegetattrpos1 =
|]

case_unsafegetattrpos2 =
constantEqualText "[ 6 20 ]" [i|
constantEqualText "[ 5 14 ]" [text|
let e = 1;
f = 1;
t = {};
Expand All @@ -227,7 +227,7 @@ case_unsafegetattrpos2 =
|]

case_unsafegetattrpos3 =
constantEqualText "[ 7 13 ]" [i|
constantEqualText "[ 6 7 ]" [text|
let e = 1;
f = 1;
t = {};
Expand All @@ -242,7 +242,7 @@ case_unsafegetattrpos3 =
|]

case_unsafegetattrpos4 =
constantEqualText "[ 8 13 ]" [i|
constantEqualText "[ 7 7 ]" [text|
let e = 1;
f = 1;
t = {};
Expand All @@ -259,7 +259,7 @@ case_unsafegetattrpos4 =
-- jww (2018-05-09): These two are failing but they shouldn't be

-- case_unsafegetattrpos5 =
-- constantEqualText "[ 7 13 ]" [i|
-- constantEqualText "[ 7 13 ]" [text|
-- let e = 1;
-- f = 1;
-- t = {};
Expand All @@ -274,7 +274,7 @@ case_unsafegetattrpos4 =
-- |]

-- case_unsafegetattrpos6 =
-- constantEqualText "[ 7 13 ]" [i|
-- constantEqualText "[ 7 13 ]" [text|
-- let e = 1;
-- f = 1;
-- t = {};
Expand All @@ -289,7 +289,7 @@ case_unsafegetattrpos4 =
-- |]

case_fixed_points =
constantEqualText [i|[
constantEqualText [text|[
{
foobar = "foobar";
foo = "foo";
Expand All @@ -300,7 +300,7 @@ case_fixed_points =
foo = "foo + ";
bar = "bar";
}
]|] [i|
]|] [text|
let
fix = f: let x = f x; in x;
extends = f: rattrs: self:
Expand All @@ -313,7 +313,7 @@ case_fixed_points =
|]

case_fixed_points_and_fold =
constantEqualText [i|[ {} {} ]|] [i|
constantEqualText [text|[ {} {} ]|] [text|
let
extends = f: rattrs: self:
let super = rattrs self; in super // f self super;
Expand All @@ -325,7 +325,7 @@ in [ (fix toFixFold) (fix toFix) ]
|]

case_fixed_points_attrsets =
constantEqualText "{ x = { y = { z = 100; }; z = { y = 100; }; }; }" [i|
constantEqualText "{ x = { y = { z = 100; }; z = { y = 100; }; }; }" [text|
let fix = f: let x = f x; in x;
f = self: { x.z.y = 100; x.y.z = self.x.z.y; };
in fix f
Expand Down Expand Up @@ -360,7 +360,7 @@ case_rec_path_attr =
"let src = 10; x = rec { passthru.src = src; }; in x.passthru.src"

case_mapattrs_builtin =
constantEqualText' "{ a = \"afoo\"; b = \"bbar\"; }" [i|
constantEqualText' "{ a = \"afoo\"; b = \"bbar\"; }" [text|
(builtins.mapAttrs (x: y: x + y) {
a = "foo";
b = "bar";
Expand Down Expand Up @@ -413,10 +413,10 @@ case_attrset_function_nested_bottom_equal =
-- Regression test for #527

case_add_string_thunk_left =
constantEqualText [i|"cygwin"|] [i|builtins.head ["cyg"] + "win"|]
constantEqualText [text|"cygwin"|] [text|builtins.head ["cyg"] + "win"|]

case_add_string_thunk_right =
constantEqualText [i|"cygwin"|] [i|"cyg" + builtins.head ["win"]|]
constantEqualText [text|"cygwin"|] [text|"cyg" + builtins.head ["win"]|]

case_add_int_thunk_left =
constantEqualText "3" "builtins.head [1] + 2"
Expand Down Expand Up @@ -446,28 +446,33 @@ genEvalCompareTests = do
mkTestCase td f = testCase f $ assertEvalFileMatchesNix (td </> f)

constantEqual :: NExprLoc -> NExprLoc -> Assertion
constantEqual a b = do
constantEqual expected actual = do
time <- getCurrentTime
let opts = defaultOptions time
-- putStrLn =<< lint (stripAnnotation a)
res <- runWithBasicEffectsIO opts $ do
a' <- normalForm =<< nixEvalExprLoc Nothing a
b' <- normalForm =<< nixEvalExprLoc Nothing b
valueEqM a' b'
assertBool "" res
(eq, expectedNF, actualNF) <- runWithBasicEffectsIO opts $ do
expectedNF <- normalForm =<< nixEvalExprLoc Nothing expected
actualNF <- normalForm =<< nixEvalExprLoc Nothing actual
eq <- valueEqM expectedNF actualNF
return (eq, expectedNF, actualNF)
let message =
"Inequal normal forms:\n"
<> "Expected: " <> printNix expectedNF <> "\n"
<> "Actual: " <> printNix actualNF
assertBool message eq

constantEqualText' :: Text -> Text -> Assertion
constantEqualText' a b = do
let Success a' = parseNixTextLoc a
Success b' = parseNixTextLoc b
constantEqual a' b'
constantEqualText' expected actual = do
let Success expected' = parseNixTextLoc expected
Success actual' = parseNixTextLoc actual
constantEqual expected' actual'

constantEqualText :: Text -> Text -> Assertion
constantEqualText a b = do
constantEqualText' a b
constantEqualText expected actual = do
constantEqualText' expected actual
mres <- liftIO $ lookupEnv "ALL_TESTS" <|> lookupEnv "MATCHING_TESTS"
when (isJust mres) $
assertEvalMatchesNix b
assertEvalMatchesNix actual

assertNixEvalThrows :: Text -> Assertion
assertNixEvalThrows a = do
Expand Down
10 changes: 5 additions & 5 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import Control.Monad
import Data.Fix
import Data.List (isSuffixOf)
import Data.Maybe
import Data.String.Interpolate.IsString
import Data.Text (unpack)
import Data.Time
import qualified EvalTests
import NeatInterpolation (text)
import qualified Nix
import Nix.Expr.Types
import Nix.String
Expand Down Expand Up @@ -50,10 +50,10 @@ ensureNixpkgsCanParse =
Fix (NAbs (ParamSet params _ _) _) -> do
let rev = getString "rev" params
sha256 = getString "sha256" params
consider "fetchTarball expression" (pure $ parseNixTextLoc [i|
consider "fetchTarball expression" (pure $ parseNixTextLoc [text|
builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/#{rev}.tar.gz";
sha256 = "#{sha256}";
url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
sha256 = "${sha256}";
}|]) $ \expr -> do
NVStr ns <- do
time <- getCurrentTime
Expand All @@ -65,7 +65,7 @@ ensureNixpkgsCanParse =
errorWithoutStackTrace $
"Directory " ++ show dir ++ " does not exist"
files <- globDir1 (compile "**/*.nix") (unpack dir)
when (length files == 0) $
when (null files) $
errorWithoutStackTrace $
"Directory " ++ show dir ++ " does not have any files"
forM_ files $ \file -> do
Expand Down
10 changes: 5 additions & 5 deletions tests/ParserTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module ParserTests (tests) where

import Data.Fix
import Data.List.NonEmpty (NonEmpty(..))
import Data.String.Interpolate.IsString
import Data.Text (Text, unpack)
import Data.Text.Prettyprint.Doc
import Data.Text.Prettyprint.Doc.Render.Text
import NeatInterpolation (text)
import Nix.Atoms
import Nix.Expr
import Nix.Parser
Expand Down Expand Up @@ -329,12 +329,12 @@ case_comments = do
assertParseFile "let-comments.nix" expected

case_select_or_precedence =
assertParsePrint [i|let
assertParsePrint [text|let
matchDef = def: matcher:
v: let
case = builtins.head (builtins.attrNames v);
in (matcher.case or def case) (v.case);
in null|] [i|let
in null|] [text|let
matchDef = def:
matcher:
v:
Expand All @@ -344,12 +344,12 @@ in null|] [i|let
in null|]

case_select_or_precedence2 =
assertParsePrint [i|let
assertParsePrint [text|let
matchDef = def: matcher:
v: let
case = builtins.head (builtins.attrNames v);
in (matcher.case or null.foo) (v.case);
in null|] [i|let
in null|] [text|let
matchDef = def:
matcher:
v:
Expand Down