Skip to content

Commit b5209af

Browse files
committed
test(nix): cover attrpath qualification, quoting, and interpolation
Four extraction cases and one pipeline case, each asserting a distinct claim so a regression names what it broke: nix_attrset_scope_disambiguates_leaf_names two attrsets each holding `dup` produce two definitions with distinct QNs. Unqualified they shared one QN and the second was discarded. nix_dotted_attrpath_qualifies_like_nested `wrap.deep.fn = …` and `wrap = { deep = { fn = …; }; }` produce the SAME QN. This is the equality that makes attrpath qualification a correctness fix rather than a preference — the two are the same expression. nix_quoted_attr_name_strips_quotes `"kebab-case"` is named kebab-case, and asserts the quoted form is absent rather than merely that the bare form is present. nix_interpolated_attr_mints_no_def `"${dynamic}" = …` mints nothing, and the sibling binding still does — so the test cannot pass by extracting nothing at all. pipeline_nix_scoped_binding_calls_resolve (tests/test_pipeline.c) a call inside a scoped binding reaches its target through the store, for both routes into a qualified name: an enclosing attrset and a dotted attrpath. The pipeline case has to live at that level. Definition QNs and call-scope QNs come from two separate functions, and if they disagree the edge is dropped at write with no error — every extraction-level assertion still passes while the graph quietly loses edges. Only the store shows it. Adds has_def_qn alongside the existing has_def. find_def_by_name returns the first match by NAME and so cannot tell two same-named definitions in different scopes apart, which is exactly what these tests exist to separate. Extraction and pipeline suites: 491 passed, 0 failed. Signed-off-by: Jason Bowman <jason@json64.dev>
1 parent bf8680c commit b5209af

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎tests/test_extraction.c‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ static int __attribute__((unused)) has_import(CBMFileResult *r, const char *path
5353
}
5454

5555
/* Count definitions with a given label. */
56+
/* Check for a definition with the given qualified name. Distinct from
57+
* find_def_by_name, which returns the first match by NAME and so cannot tell two
58+
* same-named definitions in different scopes apart — exactly the case that
59+
* attrpath qualification exists to separate. */
60+
static int has_def_qn(CBMFileResult *r, const char *qn) {
61+
for (int i = 0; i < r->defs.count; i++) {
62+
if (r->defs.items[i].qualified_name && strcmp(r->defs.items[i].qualified_name, qn) == 0)
63+
return 1;
64+
}
65+
return 0;
66+
}
67+
5668
static int count_defs_with_label(CBMFileResult *r, const char *label) {
5769
int count = 0;
5870
for (int i = 0; i < r->defs.count; i++) {
@@ -1222,6 +1234,77 @@ TEST(nix_defs_survive_curried_header) {
12221234
PASS();
12231235
}
12241236

1237+
/* A Nix binding's name is a PATH, and the extractor previously took only its first
1238+
* segment. Convention here matches C++ namespaces — `ns::serialize` is name
1239+
* `serialize`, QN `proj.file.ns.serialize` — so a Nix binding is name = leaf
1240+
* segment, QN = enclosing scope + leaf. */
1241+
TEST(nix_attrset_scope_disambiguates_leaf_names) {
1242+
CBMFileResult *r =
1243+
extract("{\n setA = { dup = x: x + 1; };\n setB = { dup = y: y + 2; };\n}\n",
1244+
CBM_LANG_NIX, "t", "collide.nix");
1245+
ASSERT_NOT_NULL(r);
1246+
ASSERT_FALSE(r->has_error);
1247+
/* Both survive. Unqualified, these shared one QN, so the second definition —
1248+
* and every CALLS edge sourced from it — was silently discarded at write. */
1249+
ASSERT(count_defs_with_label(r, "Function") == 2);
1250+
ASSERT(has_def_qn(r, "t.collide.setA.dup"));
1251+
ASSERT(has_def_qn(r, "t.collide.setB.dup"));
1252+
cbm_free_result(r);
1253+
PASS();
1254+
}
1255+
1256+
/* `a.b.fn = …` is sugar for `a = { b = { fn = …; }; }`. Both spellings must yield
1257+
* the same name and the same QN; the leading segments are scope, not name. */
1258+
TEST(nix_dotted_attrpath_qualifies_like_nested) {
1259+
CBMFileResult *r = extract("{\n wrap.deep.fn = z: z + 1;\n}\n", CBM_LANG_NIX, "t", "d.nix");
1260+
ASSERT_NOT_NULL(r);
1261+
ASSERT_FALSE(r->has_error);
1262+
ASSERT(has_def(r, "Function", "fn"));
1263+
ASSERT(has_def_qn(r, "t.d.wrap.deep.fn"));
1264+
1265+
CBMFileResult *n =
1266+
extract("{\n wrap = { deep = { fn = z: z + 1; }; };\n}\n", CBM_LANG_NIX, "t", "d.nix");
1267+
ASSERT_NOT_NULL(n);
1268+
ASSERT_FALSE(n->has_error);
1269+
/* The equality that makes this a correctness fix rather than a preference. */
1270+
ASSERT(has_def_qn(n, "t.d.wrap.deep.fn"));
1271+
cbm_free_result(n);
1272+
cbm_free_result(r);
1273+
PASS();
1274+
}
1275+
1276+
/* A quoted segment is an ordinary name that merely needs quoting in source. The
1277+
* delimiters are not part of it, and leaving them in means every consumer keying
1278+
* on the name has to know to re-quote. */
1279+
TEST(nix_quoted_attr_name_strips_quotes) {
1280+
CBMFileResult *r = extract("{\n \"kebab-case\" = a: a;\n svc.\"my.name\" = b: b;\n}\n",
1281+
CBM_LANG_NIX, "t", "q.nix");
1282+
ASSERT_NOT_NULL(r);
1283+
ASSERT_FALSE(r->has_error);
1284+
ASSERT(has_def(r, "Function", "kebab-case"));
1285+
ASSERT_FALSE(has_def_any(r, "\"kebab-case\""));
1286+
ASSERT(has_def_qn(r, "t.q.kebab-case"));
1287+
/* A quoted segment may itself contain dots; they are part of the name, not
1288+
* path separators, but the QN is a dotted string either way. */
1289+
ASSERT(has_def(r, "Function", "my.name"));
1290+
cbm_free_result(r);
1291+
PASS();
1292+
}
1293+
1294+
/* `"${x}" = …` has no statically knowable name. Minting it produces a def named
1295+
* `"${x}"` that nothing can look up or resolve a call against, so mint nothing —
1296+
* the same call the Makefile dot-prefix guard makes. */
1297+
TEST(nix_interpolated_attr_mints_no_def) {
1298+
CBMFileResult *r =
1299+
extract("{\n \"${dynamic}\" = a: a;\n fixed = b: b;\n}\n", CBM_LANG_NIX, "t", "i.nix");
1300+
ASSERT_NOT_NULL(r);
1301+
ASSERT_FALSE(r->has_error);
1302+
ASSERT(has_def(r, "Function", "fixed"));
1303+
ASSERT(count_defs_with_label(r, "Function") == 1);
1304+
cbm_free_result(r);
1305+
PASS();
1306+
}
1307+
12251308
/* Descending past the header must not mint a second def from a curried lambda's inner
12261309
* arm: `iota = a: b: ...` is one named function, not two. The inner `b:` has a
12271310
* function_expression parent, resolves no name, and must stay out. */
@@ -4986,6 +5069,10 @@ SUITE(extraction) {
49865069
RUN_TEST(nix_defs_survive_function_header_let);
49875070
RUN_TEST(nix_defs_survive_function_header_attrset);
49885071
RUN_TEST(nix_defs_survive_curried_header);
5072+
RUN_TEST(nix_attrset_scope_disambiguates_leaf_names);
5073+
RUN_TEST(nix_dotted_attrpath_qualifies_like_nested);
5074+
RUN_TEST(nix_quoted_attr_name_strips_quotes);
5075+
RUN_TEST(nix_interpolated_attr_mints_no_def);
49895076
RUN_TEST(nix_curried_lambda_mints_one_def);
49905077
RUN_TEST(fortran_function);
49915078

‎tests/test_pipeline.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,61 @@ static bool cross_file_call_exists(cbm_store_t *s, const char *project, const ch
851851
return found;
852852
}
853853

854+
/* Nix attrpath qualification, end to end. A call inside a scoped binding must
855+
* source to the QUALIFIED definition.
856+
*
857+
* This has to be a pipeline test rather than an extraction one. Definition QNs
858+
* (extract_defs.c) and call-scope QNs (extract_unified.c) are computed by two
859+
* separate functions. If they disagree by even one segment, the CALLS edge names
860+
* a source node that was never minted and is dropped at write — with no error,
861+
* and with every extraction-level assertion still passing. Only the store sees it.
862+
*
863+
* Both callers below are scoped, one by an enclosing attrset and one by a dotted
864+
* attrpath, so this covers both routes into the qualified name. */
865+
TEST(pipeline_nix_scoped_binding_calls_resolve) {
866+
if (setup_test_repo() != 0) {
867+
FAIL("failed to create temp dir");
868+
}
869+
870+
char nix_path[512];
871+
snprintf(nix_path, sizeof(nix_path), "%s/lib.nix", g_tmpdir);
872+
FILE *nf = fopen(nix_path, "w");
873+
if (!nf) {
874+
teardown_test_repo();
875+
FAIL("failed to write nix fixture");
876+
}
877+
fprintf(nf, "{ prelude }:\n"
878+
"let\n"
879+
" nixTarget = t: t + 1;\n"
880+
"in\n"
881+
"{\n"
882+
" setA = { nixCaller = x: nixTarget x; };\n"
883+
" outer.inner.nixDeepCaller = y: nixTarget y;\n"
884+
"}\n");
885+
fclose(nf);
886+
887+
char nix_db[512];
888+
snprintf(nix_db, sizeof(nix_db), "%s/test_nix_calls.db", g_tmpdir);
889+
890+
cbm_pipeline_t *np = cbm_pipeline_new(g_tmpdir, nix_db, CBM_MODE_FULL);
891+
ASSERT_NOT_NULL(np);
892+
ASSERT_EQ(cbm_pipeline_run(np), 0);
893+
894+
cbm_store_t *ns = cbm_store_open_path(nix_db);
895+
ASSERT_NOT_NULL(ns);
896+
const char *nix_project = cbm_pipeline_project_name(np);
897+
898+
/* Scoped by an enclosing attrset: the def QN is proj.lib.setA.nixCaller. */
899+
ASSERT(cross_file_call_exists(ns, nix_project, "nixCaller", "nixTarget"));
900+
/* Scoped by a dotted attrpath: proj.lib.outer.inner.nixDeepCaller. */
901+
ASSERT(cross_file_call_exists(ns, nix_project, "nixDeepCaller", "nixTarget"));
902+
903+
cbm_store_close(ns);
904+
cbm_pipeline_free(np);
905+
teardown_test_repo();
906+
PASS();
907+
}
908+
854909
/* Regression: incremental re-index of an edited file must NOT drop inbound
855910
* cross-file CALLS edges whose source lives in an UNCHANGED file.
856911
*
@@ -7363,6 +7418,7 @@ SUITE(pipeline) {
73637418
RUN_TEST(pipeline_complexity_transitive_loop_depth);
73647419
/* Calls pass */
73657420
RUN_TEST(pipeline_calls_resolution);
7421+
RUN_TEST(pipeline_nix_scoped_binding_calls_resolve);
73667422
RUN_TEST(pipeline_incremental_preserves_cross_file_calls);
73677423
RUN_TEST(pipeline_tsjs_receiver_suppresses_weak_method_edge);
73687424
RUN_TEST(pipeline_tsjs_receiver_parallel_keeps_service_edges);

0 commit comments

Comments
 (0)