-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include <nix/eval.hh> | ||
|
||
namespace nixt { | ||
|
||
bool isOption(nix::EvalState &State, nix::Value &V); | ||
|
||
bool isDerivation(nix::EvalState &State, nix::Value &V); | ||
|
||
std::string attrPathStr(nix::EvalState &State, nix::Value &V, | ||
const std::string &AttrPath); | ||
|
||
} // namespace nixt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "nixt/Value.h" | ||
|
||
#include <nix/attr-path.hh> | ||
|
||
using namespace nixt; | ||
|
||
bool nixt::isOption(nix::EvalState &State, nix::Value &V) { | ||
State.forceValue(V, nix::noPos); | ||
if (V.type() != nix::ValueType::nAttrs) | ||
return false; | ||
|
||
// https://github.com/NixOS/nixpkgs/blob/58ca986543b591a8269cbce3328293ca8d64480f/lib/options.nix#L89 | ||
try { | ||
auto S = attrPathStr(State, V, "_type"); | ||
return S == "option"; | ||
} catch (nix::AttrPathNotFound &Error) { | ||
return false; | ||
} | ||
}; | ||
|
||
bool nixt::isDerivation(nix::EvalState &State, nix::Value &V) { | ||
State.forceValue(V, nix::noPos); | ||
if (V.type() != nix::ValueType::nAttrs) | ||
return false; | ||
|
||
try { | ||
// Derivations has a special attribute "type" == "derivation" | ||
auto S = attrPathStr(State, V, "type"); | ||
return S == "derivation"; | ||
} catch (nix::AttrPathNotFound &Error) { | ||
return false; | ||
} | ||
} | ||
|
||
std::string nixt::attrPathStr(nix::EvalState &State, nix::Value &V, | ||
const std::string &AttrPath) { | ||
auto &AutoArgs = *State.allocBindings(0); | ||
auto [VPath, Pos] = nix::findAlongAttrPath(State, AttrPath, AutoArgs, V); | ||
State.forceValue(*VPath, Pos); | ||
return std::string(State.forceStringNoCtx(*VPath, nix::noPos, "")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <nix/eval.hh> | ||
#include <nix/store-api.hh> | ||
|
||
namespace nixt { | ||
|
||
struct StateTest : testing::Test { | ||
std::unique_ptr<nix::EvalState> State; | ||
StateTest() : State(new nix::EvalState{{}, nix::openStore("dummy://")}) {} | ||
}; | ||
|
||
} // namespace nixt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "StateTest.h" | ||
|
||
#include "nixt/Value.h" | ||
|
||
using namespace nixt; | ||
|
||
namespace { | ||
|
||
struct ValueTest : StateTest { | ||
nix::SourcePath cwd() { return State->rootPath(nix::CanonPath::fromCwd()); } | ||
}; | ||
|
||
TEST_F(ValueTest, IsOption_neg) { | ||
nix::Expr *AST = State->parseExprFromString("1", cwd()); | ||
nix::Value V; | ||
State->eval(AST, V); | ||
|
||
ASSERT_FALSE(isOption(*State, V)); | ||
} | ||
|
||
TEST_F(ValueTest, IsOption_pos) { | ||
nix::Expr *AST = | ||
State->parseExprFromString(R"({ _type = "option"; })", cwd()); | ||
nix::Value V; | ||
State->eval(AST, V); | ||
|
||
ASSERT_TRUE(isOption(*State, V)); | ||
} | ||
|
||
TEST_F(ValueTest, IsDerivation_neg) { | ||
nix::Expr *AST = | ||
State->parseExprFromString(R"({ _type = "option"; })", cwd()); | ||
nix::Value V; | ||
State->eval(AST, V); | ||
|
||
ASSERT_FALSE(isDerivation(*State, V)); | ||
} | ||
|
||
TEST_F(ValueTest, IsDerivation_pos) { | ||
nix::Expr *AST = | ||
State->parseExprFromString(R"({ type = "derivation"; })", cwd()); | ||
nix::Value V; | ||
State->eval(AST, V); | ||
|
||
ASSERT_TRUE(isDerivation(*State, V)); | ||
} | ||
|
||
} // namespace |