Skip to content

Commit 7f9feb6

Browse files
committed
test(nix): cover Variable extraction, scope bound, and the Function/Variable split
nix_module_level_bindings_mint_variables a `let` binding and an attrset binding both mint Variables, and the QN carries the attrpath (services.nginx.enable). nix_nested_bindings_are_not_module_level a binding two attrsets deep mints nothing, and neither `deep` nor `nested` becomes a Variable since both are scopes. nix_lambda_binding_is_function_not_variable a lambda-valued binding is a Function and NOT a Variable; a scalar binding is the reverse. Every absence assertion is paired with a positive one on the same predicate in the same result — `topLevel` in the nesting test, `val` in the split test — so none of them can pass by extracting nothing at all. Several false "clean" results in this area came from predicates that could not have matched. Signed-off-by: Jason Bowman <jason@json64.dev>
1 parent 0bd65aa commit 7f9feb6

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

‎tests/test_extraction.c‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,75 @@ TEST(nix_interpolated_attr_mints_no_def) {
13051305
PASS();
13061306
}
13071307

1308+
/* Nix Variables. `nix_var_types` has always declared `binding`, but no Nix name
1309+
* resolver existed, so the count was unconditionally zero.
1310+
*
1311+
* Scope follows the rule every other language uses: extract_variables mints FILE
1312+
* scope and never locals — a C++ declaration inside a function body is not a
1313+
* Variable. For Nix, file scope is the `let` bindings and the returned attrset;
1314+
* anything in a deeper attrset is not. Without that bound a NixOS module's
1315+
* settings tree would mint a node per `enable = true`. */
1316+
TEST(nix_module_level_bindings_mint_variables) {
1317+
CBMFileResult *r = extract("{ pkgs, lib, ... }:\n"
1318+
"let\n"
1319+
" privateConst = 42;\n"
1320+
"in\n"
1321+
"{\n"
1322+
" exported = \"value\";\n"
1323+
" services.nginx.enable = true;\n"
1324+
"}\n",
1325+
CBM_LANG_NIX, "t", "mod.nix");
1326+
ASSERT_NOT_NULL(r);
1327+
ASSERT_FALSE(r->has_error);
1328+
/* A let binding is file scope in the same sense a C++ file-static is. */
1329+
ASSERT(has_def(r, "Variable", "privateConst"));
1330+
ASSERT(has_def(r, "Variable", "exported"));
1331+
/* The QN carries the attrpath, exactly as it does for functions. */
1332+
ASSERT(has_def(r, "Variable", "enable"));
1333+
ASSERT(has_def_qn(r, "t.mod.services.nginx.enable"));
1334+
cbm_free_result(r);
1335+
PASS();
1336+
}
1337+
1338+
/* The flood guard. Each absence assertion is paired with a positive one on the
1339+
* same predicate in the same result, so none can pass by extracting nothing. */
1340+
TEST(nix_nested_bindings_are_not_module_level) {
1341+
CBMFileResult *r = extract("{ pkgs }:\n"
1342+
"{\n"
1343+
" topLevel = 1;\n"
1344+
" deep = {\n"
1345+
" nested = {\n"
1346+
" shouldNotAppear = 2;\n"
1347+
" };\n"
1348+
" };\n"
1349+
"}\n",
1350+
CBM_LANG_NIX, "t", "deep.nix");
1351+
ASSERT_NOT_NULL(r);
1352+
ASSERT_FALSE(r->has_error);
1353+
ASSERT(has_def(r, "Variable", "topLevel")); /* positive control */
1354+
ASSERT_FALSE(has_def_any(r, "shouldNotAppear"));
1355+
/* `deep` is an attrset — a scope, not a value — so not a Variable either. */
1356+
ASSERT_FALSE(has_def(r, "Variable", "deep"));
1357+
ASSERT_FALSE(has_def(r, "Variable", "nested"));
1358+
cbm_free_result(r);
1359+
PASS();
1360+
}
1361+
1362+
/* A lambda-valued binding is already minted as a Function by the def walk. Minting
1363+
* it again here would double-count every helper in the ecosystem. */
1364+
TEST(nix_lambda_binding_is_function_not_variable) {
1365+
CBMFileResult *r =
1366+
extract("{\n fn = x: x + 1;\n val = 7;\n}\n", CBM_LANG_NIX, "t", "mix.nix");
1367+
ASSERT_NOT_NULL(r);
1368+
ASSERT_FALSE(r->has_error);
1369+
ASSERT(has_def(r, "Function", "fn"));
1370+
ASSERT_FALSE(has_def(r, "Variable", "fn"));
1371+
ASSERT(has_def(r, "Variable", "val")); /* positive control */
1372+
ASSERT_FALSE(has_def(r, "Function", "val"));
1373+
cbm_free_result(r);
1374+
PASS();
1375+
}
1376+
13081377
/* Descending past the header must not mint a second def from a curried lambda's inner
13091378
* arm: `iota = a: b: ...` is one named function, not two. The inner `b:` has a
13101379
* function_expression parent, resolves no name, and must stay out. */
@@ -5073,6 +5142,9 @@ SUITE(extraction) {
50735142
RUN_TEST(nix_dotted_attrpath_qualifies_like_nested);
50745143
RUN_TEST(nix_quoted_attr_name_strips_quotes);
50755144
RUN_TEST(nix_interpolated_attr_mints_no_def);
5145+
RUN_TEST(nix_module_level_bindings_mint_variables);
5146+
RUN_TEST(nix_nested_bindings_are_not_module_level);
5147+
RUN_TEST(nix_lambda_binding_is_function_not_variable);
50765148
RUN_TEST(nix_curried_lambda_mints_one_def);
50775149
RUN_TEST(fortran_function);
50785150

0 commit comments

Comments
 (0)