mirror of
https://github.com/NixOS/nix
synced 2024-09-19 23:03:53 -04:00
coerceToPath(): Handle __toString, add tests
This commit is contained in:
parent
bacceaea91
commit
173abec0bc
|
@ -2335,29 +2335,32 @@ SourcePath EvalState::coerceToPath(const PosIdx pos, Value & v, NixStringContext
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
forceValue(v, pos);
|
forceValue(v, pos);
|
||||||
|
|
||||||
if (v.type() == nString) {
|
|
||||||
copyContext(v, context);
|
|
||||||
auto s = v.string_view();
|
|
||||||
if (!hasPrefix(s, "/"))
|
|
||||||
error("string '%s' doesn't represent an absolute path", s).atPos(pos).debugThrow<EvalError>();
|
|
||||||
return rootPath(CanonPath(s));
|
|
||||||
}
|
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
e.addTrace(positions[pos], errorCtx);
|
e.addTrace(positions[pos], errorCtx);
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Handle path values directly, without coercing to a string. */
|
||||||
if (v.type() == nPath)
|
if (v.type() == nPath)
|
||||||
return v.path();
|
return v.path();
|
||||||
|
|
||||||
|
/* Similarly, handle __toString where the result may be a path
|
||||||
|
value. */
|
||||||
if (v.type() == nAttrs) {
|
if (v.type() == nAttrs) {
|
||||||
auto i = v.attrs->find(sOutPath);
|
auto i = v.attrs->find(sToString);
|
||||||
if (i != v.attrs->end())
|
if (i != v.attrs->end()) {
|
||||||
return coerceToPath(pos, *i->value, context, errorCtx);
|
Value v1;
|
||||||
|
callFunction(*i->value, v, v1, pos);
|
||||||
|
return coerceToPath(pos, v1, context, errorCtx);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
error("cannot coerce %1% to a path", showType(v)).withTrace(pos, errorCtx).debugThrow<TypeError>();
|
/* Any other value should be coercable to a string, interpreted
|
||||||
|
relative to the root filesystem. */
|
||||||
|
auto path = coerceToString(pos, v, context, errorCtx, false, false, true).toOwned();
|
||||||
|
if (path == "" || path[0] != '/')
|
||||||
|
error("string '%1%' doesn't represent an absolute path", path).withTrace(pos, errorCtx).debugThrow<EvalError>();
|
||||||
|
return rootPath(CanonPath(path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -295,7 +295,7 @@ namespace nix {
|
||||||
TEST_F(ErrorTraceTest, toPath) {
|
TEST_F(ErrorTraceTest, toPath) {
|
||||||
ASSERT_TRACE2("toPath []",
|
ASSERT_TRACE2("toPath []",
|
||||||
TypeError,
|
TypeError,
|
||||||
hintfmt("cannot coerce %s to a path", "a list"),
|
hintfmt("cannot coerce %s to a string", "a list"),
|
||||||
hintfmt("while evaluating the first argument passed to builtins.toPath"));
|
hintfmt("while evaluating the first argument passed to builtins.toPath"));
|
||||||
|
|
||||||
ASSERT_TRACE2("toPath \"foo\"",
|
ASSERT_TRACE2("toPath \"foo\"",
|
||||||
|
@ -309,7 +309,7 @@ namespace nix {
|
||||||
TEST_F(ErrorTraceTest, storePath) {
|
TEST_F(ErrorTraceTest, storePath) {
|
||||||
ASSERT_TRACE2("storePath true",
|
ASSERT_TRACE2("storePath true",
|
||||||
TypeError,
|
TypeError,
|
||||||
hintfmt("cannot coerce %s to a path", "a Boolean"),
|
hintfmt("cannot coerce %s to a string", "a Boolean"),
|
||||||
hintfmt("while evaluating the first argument passed to 'builtins.storePath'"));
|
hintfmt("while evaluating the first argument passed to 'builtins.storePath'"));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -318,7 +318,7 @@ namespace nix {
|
||||||
TEST_F(ErrorTraceTest, pathExists) {
|
TEST_F(ErrorTraceTest, pathExists) {
|
||||||
ASSERT_TRACE2("pathExists []",
|
ASSERT_TRACE2("pathExists []",
|
||||||
TypeError,
|
TypeError,
|
||||||
hintfmt("cannot coerce %s to a path", "a list"),
|
hintfmt("cannot coerce %s to a string", "a list"),
|
||||||
hintfmt("while realising the context of a path"));
|
hintfmt("while realising the context of a path"));
|
||||||
|
|
||||||
ASSERT_TRACE2("pathExists \"zorglub\"",
|
ASSERT_TRACE2("pathExists \"zorglub\"",
|
||||||
|
@ -377,7 +377,7 @@ namespace nix {
|
||||||
TEST_F(ErrorTraceTest, filterSource) {
|
TEST_F(ErrorTraceTest, filterSource) {
|
||||||
ASSERT_TRACE2("filterSource [] []",
|
ASSERT_TRACE2("filterSource [] []",
|
||||||
TypeError,
|
TypeError,
|
||||||
hintfmt("cannot coerce %s to a path", "a list"),
|
hintfmt("cannot coerce %s to a string", "a list"),
|
||||||
hintfmt("while evaluating the second argument (the path to filter) passed to 'builtins.filterSource'"));
|
hintfmt("while evaluating the second argument (the path to filter) passed to 'builtins.filterSource'"));
|
||||||
|
|
||||||
ASSERT_TRACE2("filterSource [] \"foo\"",
|
ASSERT_TRACE2("filterSource [] \"foo\"",
|
||||||
|
|
|
@ -25,5 +25,7 @@ builtins.pathExists (./lib.nix)
|
||||||
&& builtins.pathExists (builtins.toString ./. + "/../lang/..//")
|
&& builtins.pathExists (builtins.toString ./. + "/../lang/..//")
|
||||||
&& builtins.pathExists (builtins.toPath (builtins.toString ./lib.nix))
|
&& builtins.pathExists (builtins.toPath (builtins.toString ./lib.nix))
|
||||||
&& !builtins.pathExists (builtins.toPath (builtins.toString ./bla.nix))
|
&& !builtins.pathExists (builtins.toPath (builtins.toString ./bla.nix))
|
||||||
|
&& builtins.pathExists (builtins.toPath { __toString = x: builtins.toString ./lib.nix; })
|
||||||
|
&& builtins.pathExists (builtins.toPath { outPath = builtins.toString ./lib.nix; })
|
||||||
&& builtins.pathExists ./lib.nix
|
&& builtins.pathExists ./lib.nix
|
||||||
&& !builtins.pathExists ./bla.nix
|
&& !builtins.pathExists ./bla.nix
|
||||||
|
|
Loading…
Reference in a new issue