/* * test_discover.c — Tests for directory skip logic, suffix filters, and file walk. * * RED phase: Tests define expected filtering behavior for the discover module. */ #include "test_framework.h" #include "test_helpers.h" #include "discover/discover.h" #include "foundation/platform.h" typedef struct { char *home; char *xdg_config_home; } git_env_snapshot_t; static git_env_snapshot_t save_git_env(void) { git_env_snapshot_t snapshot = {0}; const char *home = getenv("HOME"); const char *xdg = getenv("XDG_CONFIG_HOME"); snapshot.home = home ? cbm_strdup(home) : NULL; snapshot.xdg_config_home = xdg ? cbm_strdup(xdg) : NULL; return snapshot; } static void restore_git_env(git_env_snapshot_t *snapshot) { if (snapshot->home) { cbm_setenv("HOME", snapshot->home, 1); free(snapshot->home); } else { cbm_unsetenv("HOME"); } if (snapshot->xdg_config_home) { cbm_setenv("XDG_CONFIG_HOME", snapshot->xdg_config_home, 1); free(snapshot->xdg_config_home); } else { cbm_unsetenv("XDG_CONFIG_HOME"); } } static bool discover_has_rel_path(const cbm_file_info_t *files, int count, const char *rel_path) { for (int i = 0; i < count; i++) { if (strcmp(files[i].rel_path, rel_path) == 0) { return true; } } return false; } /* ── Directory skip (always skipped) ───────────────────────────── */ TEST(skip_git) { ASSERT_TRUE(cbm_should_skip_dir(".git", CBM_MODE_FULL)); PASS(); } TEST(skip_node_modules) { ASSERT_TRUE(cbm_should_skip_dir("node_modules", CBM_MODE_FULL)); PASS(); } TEST(skip_pycache) { ASSERT_TRUE(cbm_should_skip_dir("__pycache__", CBM_MODE_FULL)); PASS(); } TEST(skip_venv) { ASSERT_TRUE(cbm_should_skip_dir("venv", CBM_MODE_FULL)); PASS(); } TEST(skip_dist) { ASSERT_TRUE(cbm_should_skip_dir("dist", CBM_MODE_FULL)); PASS(); } TEST(skip_target) { ASSERT_TRUE(cbm_should_skip_dir("target", CBM_MODE_FULL)); PASS(); } TEST(skip_vendor) { ASSERT_TRUE(cbm_should_skip_dir("vendor", CBM_MODE_FULL)); PASS(); } TEST(skip_vendored) { ASSERT_TRUE(cbm_should_skip_dir("vendored", CBM_MODE_FULL)); PASS(); } TEST(skip_terraform) { ASSERT_TRUE(cbm_should_skip_dir(".terraform", CBM_MODE_FULL)); PASS(); } TEST(skip_coverage) { ASSERT_TRUE(cbm_should_skip_dir("coverage", CBM_MODE_FULL)); PASS(); } TEST(skip_idea) { ASSERT_TRUE(cbm_should_skip_dir(".idea", CBM_MODE_FULL)); PASS(); } TEST(skip_claude) { ASSERT_TRUE(cbm_should_skip_dir(".claude", CBM_MODE_FULL)); PASS(); } /* Not skipped in full mode */ TEST(no_skip_src) { ASSERT_FALSE(cbm_should_skip_dir("src", CBM_MODE_FULL)); PASS(); } TEST(no_skip_lib) { ASSERT_FALSE(cbm_should_skip_dir("lib", CBM_MODE_FULL)); PASS(); } TEST(no_skip_docs_full) { ASSERT_FALSE(cbm_should_skip_dir("docs", CBM_MODE_FULL)); PASS(); } TEST(no_skip_test_full) { ASSERT_FALSE(cbm_should_skip_dir("__tests__", CBM_MODE_FULL)); PASS(); } /* Fast mode additional skips */ TEST(skip_fast_docs) { ASSERT_TRUE(cbm_should_skip_dir("docs", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_examples) { ASSERT_TRUE(cbm_should_skip_dir("examples", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_tests) { ASSERT_TRUE(cbm_should_skip_dir("__tests__", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_fixtures) { ASSERT_TRUE(cbm_should_skip_dir("fixtures", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_testdata) { ASSERT_TRUE(cbm_should_skip_dir("testdata", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_generated) { ASSERT_TRUE(cbm_should_skip_dir("generated", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_assets) { ASSERT_TRUE(cbm_should_skip_dir("assets", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_3rdparty) { ASSERT_TRUE(cbm_should_skip_dir("third_party", CBM_MODE_FAST)); PASS(); } TEST(skip_fast_e2e) { ASSERT_TRUE(cbm_should_skip_dir("e2e", CBM_MODE_FAST)); PASS(); } /* ── Suffix filters ────────────────────────────────────────────── */ TEST(suffix_pyc) { ASSERT_TRUE(cbm_has_ignored_suffix("module.pyc", CBM_MODE_FULL)); PASS(); } TEST(suffix_o) { ASSERT_TRUE(cbm_has_ignored_suffix("main.o", CBM_MODE_FULL)); PASS(); } TEST(suffix_so) { ASSERT_TRUE(cbm_has_ignored_suffix("lib.so", CBM_MODE_FULL)); PASS(); } TEST(suffix_png) { ASSERT_TRUE(cbm_has_ignored_suffix("icon.png", CBM_MODE_FULL)); PASS(); } TEST(suffix_jpg) { ASSERT_TRUE(cbm_has_ignored_suffix("photo.jpg", CBM_MODE_FULL)); PASS(); } TEST(suffix_wasm) { ASSERT_TRUE(cbm_has_ignored_suffix("app.wasm", CBM_MODE_FULL)); PASS(); } TEST(suffix_db) { ASSERT_TRUE(cbm_has_ignored_suffix("data.db", CBM_MODE_FULL)); PASS(); } TEST(suffix_sqlite) { ASSERT_TRUE(cbm_has_ignored_suffix("store.sqlite3", CBM_MODE_FULL)); PASS(); } TEST(suffix_tmp) { ASSERT_TRUE(cbm_has_ignored_suffix("file.tmp", CBM_MODE_FULL)); PASS(); } TEST(suffix_tilde) { ASSERT_TRUE(cbm_has_ignored_suffix("file~", CBM_MODE_FULL)); PASS(); } /* Not ignored */ TEST(suffix_go) { ASSERT_FALSE(cbm_has_ignored_suffix("main.go", CBM_MODE_FULL)); PASS(); } TEST(suffix_py) { ASSERT_FALSE(cbm_has_ignored_suffix("app.py", CBM_MODE_FULL)); PASS(); } TEST(suffix_c) { ASSERT_FALSE(cbm_has_ignored_suffix("lib.c", CBM_MODE_FULL)); PASS(); } /* Fast mode additional suffixes */ TEST(suffix_fast_zip) { ASSERT_TRUE(cbm_has_ignored_suffix("archive.zip", CBM_MODE_FAST)); PASS(); } TEST(suffix_fast_pdf) { ASSERT_TRUE(cbm_has_ignored_suffix("manual.pdf", CBM_MODE_FAST)); PASS(); } TEST(suffix_fast_mp3) { ASSERT_TRUE(cbm_has_ignored_suffix("sound.mp3", CBM_MODE_FAST)); PASS(); } TEST(suffix_fast_pem) { ASSERT_TRUE(cbm_has_ignored_suffix("cert.pem", CBM_MODE_FAST)); PASS(); } /* ── Filename skip (fast mode) ─────────────────────────────────── */ TEST(fn_skip_license) { ASSERT_TRUE(cbm_should_skip_filename("LICENSE", CBM_MODE_FAST)); PASS(); } TEST(fn_skip_changelog) { ASSERT_TRUE(cbm_should_skip_filename("CHANGELOG.md", CBM_MODE_FAST)); PASS(); } TEST(fn_skip_gosum) { ASSERT_TRUE(cbm_should_skip_filename("go.sum", CBM_MODE_FAST)); PASS(); } TEST(fn_skip_yarnlock) { ASSERT_TRUE(cbm_should_skip_filename("yarn.lock", CBM_MODE_FAST)); PASS(); } TEST(fn_skip_pkglock) { ASSERT_TRUE(cbm_should_skip_filename("package-lock.json", CBM_MODE_FAST)); PASS(); } /* Not skipped in full mode */ TEST(fn_no_skip_license_full) { ASSERT_FALSE(cbm_should_skip_filename("LICENSE", CBM_MODE_FULL)); PASS(); } /* ── Fast mode patterns ────────────────────────────────────────── */ TEST(pattern_dts) { ASSERT_TRUE(cbm_matches_fast_pattern("types.d.ts", CBM_MODE_FAST)); PASS(); } TEST(pattern_pbgo) { ASSERT_TRUE(cbm_matches_fast_pattern("service.pb.go", CBM_MODE_FAST)); PASS(); } TEST(pattern_pb2py) { ASSERT_TRUE(cbm_matches_fast_pattern("api_pb2.py", CBM_MODE_FAST)); PASS(); } TEST(pattern_mock) { ASSERT_TRUE(cbm_matches_fast_pattern("mock_service.go", CBM_MODE_FAST)); PASS(); } TEST(pattern_test_dot) { ASSERT_TRUE(cbm_matches_fast_pattern("App.test.js", CBM_MODE_FAST)); PASS(); } TEST(pattern_spec) { ASSERT_TRUE(cbm_matches_fast_pattern("App.spec.ts", CBM_MODE_FAST)); PASS(); } TEST(pattern_stories) { ASSERT_TRUE(cbm_matches_fast_pattern("Button.stories.tsx", CBM_MODE_FAST)); PASS(); } /* Not matched in full mode */ TEST(pattern_dts_full) { ASSERT_FALSE(cbm_matches_fast_pattern("types.d.ts", CBM_MODE_FULL)); PASS(); } /* ── File discovery (integration) — cross-platform via test_helpers.h ── */ /* A custom CBM_CACHE_DIR may legitimately sit inside a repository — tests do it * routinely. Walking into it would pull every other project's graph database into * this project's file list, so the walk prunes it by absolute path. */ TEST(discover_prunes_the_cache_tree) { char *base = th_mktempdir("cbm_disc_cache"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); /* Source-looking files inside the cache must not be discovered. */ th_write_file(TH_PATH(base, "cache/other_project/leaked.go"), "package leaked\n"); const char *saved = getenv("CBM_CACHE_DIR"); char *saved_copy = saved ? strdup(saved) : NULL; char cache_dir[1024]; snprintf(cache_dir, sizeof(cache_dir), "%s/cache", base); cbm_setenv("CBM_CACHE_DIR", cache_dir, 1); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); if (saved_copy) { cbm_setenv("CBM_CACHE_DIR", saved_copy, 1); free(saved_copy); } else { cbm_unsetenv("CBM_CACHE_DIR"); } ASSERT_EQ(rc, 0); for (int i = 0; i < count; i++) { ASSERT(strstr(files[i].rel_path, "leaked.go") == NULL); } ASSERT_EQ(count, 1); /* only src/main.go */ cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_simple) { char *base = th_mktempdir("cbm_disc_simple"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); th_write_file(TH_PATH(base, "src/app.py"), "print(1)\n"); th_write_file(TH_PATH(base, "src/icon.png"), "binary\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 2); /* main.go + app.py, not icon.png */ bool found_go = false, found_py = false; for (int i = 0; i < count; i++) { if (files[i].language == CBM_LANG_GO) found_go = true; if (files[i].language == CBM_LANG_PYTHON) found_py = true; } ASSERT_TRUE(found_go); ASSERT_TRUE(found_py); cbm_discover_free(files, count); th_cleanup(base); PASS(); } /* A directory with more immediate subdirectories than the walk stack's * initial capacity must still be fully discovered — dotnet/runtime's * src/tests/JIT/Regression/JitBlue holds 855 sibling dirs and made the whole * pipeline hard-fail with files=0 (walk_push_subdir treated the fixed cap as * a hard error instead of growing). */ TEST(discover_wide_sibling_fanout_exceeds_initial_walk_stack) { char *base = th_mktempdir("cbm_disc_wide"); ASSERT(base != NULL); enum { WIDE_SIBLINGS = 600 }; for (int i = 0; i < WIDE_SIBLINGS; i++) { char rel[64]; (void)snprintf(rel, sizeof(rel), "wide/d%03d/f.py", i); th_write_file(TH_PATH(base, rel), "x = 1\n"); } cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, WIDE_SIBLINGS); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_bounded_count_is_allocation_free_and_limit_exact) { char *base = th_mktempdir("cbm_disc_bounded"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/first.c"), "int first;\n"); th_write_file(TH_PATH(base, "src/second.py"), "second = 2\n"); th_write_file(TH_PATH(base, "src/ignored.png"), "not source\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL}; int limited_count = -1; cbm_discover_status_t limited = cbm_discover_count_bounded(base, &opts, 1, cbm_now_ms() + 2000, &limited_count); int exact_count = -1; cbm_discover_status_t exact = cbm_discover_count_bounded(base, &opts, 2, cbm_now_ms() + 2000, &exact_count); th_cleanup(base); ASSERT_EQ(limited, CBM_DISCOVER_LIMIT_EXCEEDED); ASSERT_EQ(limited_count, 1); ASSERT_EQ(exact, CBM_DISCOVER_OK); ASSERT_EQ(exact_count, 2); PASS(); } TEST(discover_bounded_count_fails_closed_after_deadline) { char *base = th_mktempdir("cbm_disc_deadline"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "source.c"), "int source;\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL}; int count = 99; cbm_discover_status_t status = cbm_discover_count_bounded(base, &opts, 100, 1, &count); th_cleanup(base); ASSERT_EQ(status, CBM_DISCOVER_ERROR); ASSERT_EQ(count, -1); PASS(); } /* The allocation-free bounded count must apply the same shebang fallback as * full discovery (issue #1199): extensionless scripts count, extensionless * plain text does not, and the limit boundary stays exact over that mix. */ TEST(discover_bounded_count_matches_shebang_discovery) { char *base = th_mktempdir("cbm_disc_bounded_sb"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "keep.c"), "int keep;\n"); th_write_file(TH_PATH(base, "bin/build"), "#!/usr/bin/env python3\nprint(1)\n"); th_write_file(TH_PATH(base, "bin/deploy"), "#!/bin/bash\necho hi\n"); th_write_file(TH_PATH(base, "bin/notes"), "just some plain text\nno shebang here\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL}; cbm_file_info_t *files = NULL; int count = 0; int full_rc = cbm_discover(base, &opts, &files, &count); bool has_build = discover_has_rel_path(files, count, "bin/build"); bool has_deploy = discover_has_rel_path(files, count, "bin/deploy"); bool has_notes = discover_has_rel_path(files, count, "bin/notes"); cbm_discover_free(files, count); int exact_count = -1; cbm_discover_status_t exact = cbm_discover_count_bounded(base, &opts, 3, cbm_now_ms() + 2000, &exact_count); int limited_count = -1; cbm_discover_status_t limited = cbm_discover_count_bounded(base, &opts, 2, cbm_now_ms() + 2000, &limited_count); th_cleanup(base); ASSERT_EQ(full_rc, 0); ASSERT_TRUE(has_build); ASSERT_TRUE(has_deploy); ASSERT_FALSE(has_notes); ASSERT_EQ(count, 3); ASSERT_EQ(exact, CBM_DISCOVER_OK); ASSERT_EQ(exact_count, 3); ASSERT_EQ(limited, CBM_DISCOVER_LIMIT_EXCEEDED); ASSERT_EQ(limited_count, 2); PASS(); } TEST(discover_resource_policy_off_matches_legacy_discovery) { char *base = th_mktempdir("cbm_disc_policy_off"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/first.c"), "int first;\n"); th_write_file(TH_PATH(base, "src/second.py"), "second = 2\n"); th_write_file(TH_PATH(base, "src/ignored.png"), "not source\n"); cbm_file_info_t *legacy_files = NULL; int legacy_count = 0; cbm_discover_opts_t legacy_opts = {.mode = CBM_MODE_FULL}; ASSERT_EQ(cbm_discover(base, &legacy_opts, &legacy_files, &legacy_count), CBM_DISCOVER_OK); cbm_index_resource_policy_t policy; cbm_index_policy_init(&policy); cbm_index_resource_violation_t violation = {0}; cbm_file_info_t *policy_files = NULL; int policy_count = 0; cbm_discover_opts_t policy_opts = { .mode = CBM_MODE_FULL, .resource_policy = &policy, .resource_violation = &violation, }; cbm_discover_status_t status = cbm_discover(base, &policy_opts, &policy_files, &policy_count); ASSERT_EQ(status, CBM_DISCOVER_OK); ASSERT_EQ(policy_count, legacy_count); ASSERT_EQ(violation.resource, CBM_INDEX_RESOURCE_NONE); for (int index = 0; index < legacy_count; index++) { ASSERT_STR_EQ(policy_files[index].rel_path, legacy_files[index].rel_path); ASSERT_EQ(policy_files[index].size, legacy_files[index].size); } cbm_discover_free(legacy_files, legacy_count); cbm_discover_free(policy_files, policy_count); th_cleanup(base); PASS(); } TEST(discover_resource_file_limit_is_exact_and_counts_only_accepted_sources) { char *base = th_mktempdir("cbm_disc_policy_files"); ASSERT(base != NULL); th_write_file(TH_PATH(base, ".gitignore"), "ignored.c\n"); th_write_file(TH_PATH(base, "accepted.c"), "int accepted;\n"); th_write_file(TH_PATH(base, "ignored.c"), "int ignored;\n"); th_write_file(TH_PATH(base, "unsupported.png"), "not source\n"); cbm_index_resource_policy_t policy; cbm_index_policy_init(&policy); policy.max_files = (cbm_index_limit_u64_t){.enabled = true, .value = 1}; cbm_index_resource_violation_t violation = {0}; cbm_discover_opts_t opts = { .mode = CBM_MODE_FULL, .resource_policy = &policy, .resource_violation = &violation, }; cbm_file_info_t *files = NULL; int count = 0; ASSERT_EQ(cbm_discover(base, &opts, &files, &count), CBM_DISCOVER_OK); ASSERT_EQ(count, 1); ASSERT_EQ(violation.resource, CBM_INDEX_RESOURCE_NONE); cbm_discover_free(files, count); th_write_file(TH_PATH(base, "second.py"), "second = 2\n"); files = NULL; count = 99; ASSERT_EQ(cbm_discover(base, &opts, &files, &count), CBM_DISCOVER_LIMIT_EXCEEDED); ASSERT(files == NULL); ASSERT_EQ(count, 0); ASSERT_EQ(violation.resource, CBM_INDEX_RESOURCE_FILES); ASSERT_EQ(violation.observed, 2); ASSERT_EQ(violation.limit, 1); th_cleanup(base); PASS(); } TEST(discover_resource_source_bytes_allows_equality_and_rejects_one_more_byte) { char *base = th_mktempdir("cbm_disc_policy_bytes"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "exact.c"), "1234567"); cbm_index_resource_policy_t policy; cbm_index_policy_init(&policy); policy.max_source_bytes = (cbm_index_limit_u64_t){.enabled = true, .value = 7}; cbm_index_resource_violation_t violation = {0}; cbm_discover_opts_t opts = { .mode = CBM_MODE_FULL, .resource_policy = &policy, .resource_violation = &violation, }; cbm_file_info_t *files = NULL; int count = 0; ASSERT_EQ(cbm_discover(base, &opts, &files, &count), CBM_DISCOVER_OK); ASSERT_EQ(count, 1); cbm_discover_free(files, count); th_write_file(TH_PATH(base, "plus.py"), "x"); files = NULL; count = 99; ASSERT_EQ(cbm_discover(base, &opts, &files, &count), CBM_DISCOVER_LIMIT_EXCEEDED); ASSERT(files == NULL); ASSERT_EQ(count, 0); ASSERT_EQ(violation.resource, CBM_INDEX_RESOURCE_SOURCE_BYTES); ASSERT_EQ(violation.observed, 8); ASSERT_EQ(violation.limit, 7); th_cleanup(base); PASS(); } TEST(discover_resource_file_budget_excludes_existing_oversized_skip) { char *base = th_mktempdir("cbm_disc_policy_oversized"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "accepted.c"), "x"); th_write_file(TH_PATH(base, "oversized.py"), "123"); const char *saved_limit = getenv("CBM_MAX_FILE_BYTES"); char *saved_limit_copy = saved_limit ? strdup(saved_limit) : NULL; cbm_setenv("CBM_MAX_FILE_BYTES", "2", 1); cbm_index_resource_policy_t policy; cbm_index_policy_init(&policy); policy.max_files = (cbm_index_limit_u64_t){.enabled = true, .value = 1}; cbm_index_resource_violation_t violation = {0}; cbm_discover_opts_t opts = { .mode = CBM_MODE_FULL, .resource_policy = &policy, .resource_violation = &violation, }; cbm_file_info_t *files = NULL; int count = 0; cbm_discover_status_t initial_status = cbm_discover(base, &opts, &files, &count); bool oversized_did_not_consume_budget = initial_status == CBM_DISCOVER_OK && count == 2 && violation.resource == CBM_INDEX_RESOURCE_NONE; cbm_discover_free(files, count); th_write_file(TH_PATH(base, "second.c"), "y"); files = NULL; count = 0; cbm_discover_status_t exceeded_status = cbm_discover(base, &opts, &files, &count); bool accepted_sources_exceeded = exceeded_status == CBM_DISCOVER_LIMIT_EXCEEDED && files == NULL && count == 0 && violation.resource == CBM_INDEX_RESOURCE_FILES && violation.observed == 2 && violation.limit == 1; if (saved_limit_copy) { cbm_setenv("CBM_MAX_FILE_BYTES", saved_limit_copy, 1); } else { cbm_unsetenv("CBM_MAX_FILE_BYTES"); } free(saved_limit_copy); th_cleanup(base); ASSERT_TRUE(oversized_did_not_consume_budget); ASSERT_TRUE(accepted_sources_exceeded); PASS(); } TEST(discover_skips_git_dir) { char *base = th_mktempdir("cbm_disc_git"); ASSERT(base != NULL); th_mkdir_p(TH_PATH(base, ".git")); th_write_file(TH_PATH(base, ".git/config"), "x\n"); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_with_gitignore) { char *base = th_mktempdir("cbm_disc_gi"); ASSERT(base != NULL); th_mkdir_p(TH_PATH(base, ".git")); th_write_file(TH_PATH(base, ".gitignore"), "*.log\n"); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); th_write_file(TH_PATH(base, "src/debug.log"), "error\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_EQ(files[0].language, CBM_LANG_GO); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_with_global_xdg_ignore) { git_env_snapshot_t env = save_git_env(); char *tmp = th_mktempdir("cbm_disc_global_xdg"); ASSERT(tmp != NULL); char base[512], repo[512], home[512], xdg[512], xdg_env[512]; snprintf(base, sizeof(base), "%s", tmp); snprintf(repo, sizeof(repo), "%s/repo", base); snprintf(home, sizeof(home), "%s/home", base); snprintf(xdg, sizeof(xdg), "%s/xdg", base); snprintf(xdg_env, sizeof(xdg_env), "%s/", xdg); cbm_setenv("HOME", home, 1); cbm_setenv("XDG_CONFIG_HOME", xdg_env, 1); th_mkdir_p(TH_PATH(repo, ".git")); th_write_file(TH_PATH(repo, ".git/config"), "[core]\n"); th_write_file(TH_PATH(xdg, "git/ignore"), "secret.go\nignored_dir/\n"); th_write_file(TH_PATH(repo, "main.go"), "package main\n"); th_write_file(TH_PATH(repo, "secret.go"), "package secret\n"); th_write_file(TH_PATH(repo, "ignored_dir/thing.go"), "package ignored\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(repo, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_TRUE(discover_has_rel_path(files, count, "main.go")); ASSERT_FALSE(discover_has_rel_path(files, count, "secret.go")); ASSERT_FALSE(discover_has_rel_path(files, count, "ignored_dir/thing.go")); cbm_discover_free(files, count); restore_git_env(&env); th_cleanup(base); PASS(); } TEST(discover_global_excludesfile_from_gitconfig_tilde) { git_env_snapshot_t env = save_git_env(); char *tmp = th_mktempdir("cbm_disc_global_cfg"); ASSERT(tmp != NULL); char base[512], repo[512], home[512]; snprintf(base, sizeof(base), "%s", tmp); snprintf(repo, sizeof(repo), "%s/repo", base); snprintf(home, sizeof(home), "%s/home", base); cbm_setenv("HOME", home, 1); cbm_unsetenv("XDG_CONFIG_HOME"); th_mkdir_p(TH_PATH(repo, ".git")); th_write_file(TH_PATH(repo, ".git/config"), "[core]\n"); th_write_file(TH_PATH(home, ".gitconfig"), "[core]\n excludesFile = ~/custom-ignore\n"); th_write_file(TH_PATH(home, "custom-ignore"), "skip-me.go\n"); th_write_file(TH_PATH(repo, "keep.go"), "package keep\n"); th_write_file(TH_PATH(repo, "skip-me.go"), "package skip\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(repo, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_TRUE(discover_has_rel_path(files, count, "keep.go")); ASSERT_FALSE(discover_has_rel_path(files, count, "skip-me.go")); cbm_discover_free(files, count); restore_git_env(&env); th_cleanup(base); PASS(); } TEST(discover_repo_local_excludesfile_is_ignored) { git_env_snapshot_t env = save_git_env(); char *tmp = th_mktempdir("cbm_disc_repo_cfg_ignored"); ASSERT(tmp != NULL); char base[512], repo[512], home[512], secret[512], config[1024]; snprintf(base, sizeof(base), "%s", tmp); snprintf(repo, sizeof(repo), "%s/repo", base); snprintf(home, sizeof(home), "%s/home", base); snprintf(secret, sizeof(secret), "%s/secret-ignore", home); snprintf(config, sizeof(config), "[core]\n excludesFile = %s\n", secret); cbm_setenv("HOME", home, 1); cbm_unsetenv("XDG_CONFIG_HOME"); th_mkdir_p(TH_PATH(repo, ".git")); th_write_file(TH_PATH(repo, ".git/config"), config); th_write_file(secret, "skip-me.go\n"); th_write_file(TH_PATH(repo, "keep.go"), "package keep\n"); th_write_file(TH_PATH(repo, "skip-me.go"), "package skip\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(repo, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 2); ASSERT_TRUE(discover_has_rel_path(files, count, "keep.go")); ASSERT_TRUE(discover_has_rel_path(files, count, "skip-me.go")); cbm_discover_free(files, count); restore_git_env(&env); th_cleanup(base); PASS(); } TEST(discover_missing_global_excludes_is_noop) { git_env_snapshot_t env = save_git_env(); char *tmp = th_mktempdir("cbm_disc_global_missing"); ASSERT(tmp != NULL); char base[512], repo[512], home[512]; snprintf(base, sizeof(base), "%s", tmp); snprintf(repo, sizeof(repo), "%s/repo", base); snprintf(home, sizeof(home), "%s/home", base); cbm_setenv("HOME", home, 1); cbm_unsetenv("XDG_CONFIG_HOME"); th_mkdir_p(TH_PATH(repo, ".git")); th_write_file(TH_PATH(repo, ".git/config"), "[core]\n"); th_write_file(TH_PATH(repo, "main.go"), "package main\n"); th_write_file(TH_PATH(repo, "would-be-global.go"), "package global\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(repo, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 2); ASSERT_TRUE(discover_has_rel_path(files, count, "main.go")); ASSERT_TRUE(discover_has_rel_path(files, count, "would-be-global.go")); cbm_discover_free(files, count); restore_git_env(&env); th_cleanup(base); PASS(); } TEST(discover_cbmignore_negates_global_ignore) { git_env_snapshot_t env = save_git_env(); char *tmp = th_mktempdir("cbm_disc_global_neg"); ASSERT(tmp != NULL); char base[512], repo[512], home[512], xdg[512]; snprintf(base, sizeof(base), "%s", tmp); snprintf(repo, sizeof(repo), "%s/repo", base); snprintf(home, sizeof(home), "%s/home", base); snprintf(xdg, sizeof(xdg), "%s/xdg", base); cbm_setenv("HOME", home, 1); cbm_setenv("XDG_CONFIG_HOME", xdg, 1); th_mkdir_p(TH_PATH(repo, ".git")); th_write_file(TH_PATH(repo, ".git/config"), "[core]\n"); th_write_file(TH_PATH(xdg, "git/ignore"), "rescued.go\nblocked.go\n"); th_write_file(TH_PATH(repo, ".cbmignore"), "!rescued.go\n"); th_write_file(TH_PATH(repo, "main.go"), "package main\n"); th_write_file(TH_PATH(repo, "rescued.go"), "package rescued\n"); th_write_file(TH_PATH(repo, "blocked.go"), "package blocked\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(repo, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 2); ASSERT_TRUE(discover_has_rel_path(files, count, "main.go")); ASSERT_TRUE(discover_has_rel_path(files, count, "rescued.go")); ASSERT_FALSE(discover_has_rel_path(files, count, "blocked.go")); cbm_discover_free(files, count); restore_git_env(&env); th_cleanup(base); PASS(); } /* issue #234: a directory listed in the root .gitignore (e.g. "vendor/") must * be excluded from discovery even when untracked — Composer/PHP projects rely * on this. */ TEST(discover_gitignore_dir_excluded_issue234) { char *base = th_mktempdir("cbm_disc_gi234"); ASSERT(base != NULL); th_mkdir_p(TH_PATH(base, ".git")); th_write_file(TH_PATH(base, ".gitignore"), "vendor/\n"); th_write_file(TH_PATH(base, "src/main.php"), " 1KB) */ char bigpath[512]; snprintf(bigpath, sizeof(bigpath), "%s/big.go", base); FILE *f = fopen(bigpath, "w"); ASSERT(f != NULL); for (int i = 0; i < 200; i++) { fprintf(f, "// padding line %d to exceed 1KB\n", i); } fclose(f); cbm_discover_opts_t opts = {0}; opts.max_file_size = 1024; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_null_path) { cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(NULL, NULL, &files, &count); ASSERT_EQ(rc, -1); PASS(); } TEST(discover_nonexistent_path) { char *base = th_mktempdir("cbm_disc_noexist"); char fake[512]; snprintf(fake, sizeof(fake), "%s/nonexistent_12345", base ? base : "/tmp"); cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(fake, NULL, &files, &count); ASSERT_EQ(rc, -1); th_cleanup(base); PASS(); } TEST(discover_free_null) { cbm_discover_free(NULL, 0); PASS(); } TEST(discover_skips_worktrees) { char *base = th_mktempdir("cbm_disc_wt"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); th_write_file(TH_PATH(base, ".worktrees/feature/src/app.go"), "package app\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); bool found_main = false; for (int i = 0; i < count; i++) { if (strstr(files[i].rel_path, "main.go")) found_main = true; ASSERT_NULL(strstr(files[i].rel_path, ".worktrees")); } ASSERT_TRUE(found_main); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_cbmignore) { char *base = th_mktempdir("cbm_disc_cbmi"); ASSERT(base != NULL); th_mkdir_p(TH_PATH(base, ".git")); th_write_file(TH_PATH(base, ".cbmignore"), "generated/\n*.pb.go\n"); th_write_file(TH_PATH(base, "main.go"), "package main\n"); th_write_file(TH_PATH(base, "generated/types.go"), "package gen\n"); th_write_file(TH_PATH(base, "api.pb.go"), "package api\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_TRUE(strstr(files[0].rel_path, "main.go") != NULL); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_cbmignore_stacks) { char *base = th_mktempdir("cbm_disc_stack"); ASSERT(base != NULL); th_mkdir_p(TH_PATH(base, ".git")); th_write_file(TH_PATH(base, ".gitignore"), "*.log\n"); th_write_file(TH_PATH(base, ".cbmignore"), "docs/\n"); th_write_file(TH_PATH(base, "main.go"), "package main\n"); th_write_file(TH_PATH(base, "docs/api.go"), "package docs\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); bool found_docs = false; for (int i = 0; i < count; i++) { if (strstr(files[i].rel_path, "docs/")) found_docs = true; } ASSERT_FALSE(found_docs); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_symlink_skipped) { #ifdef _WIN32 /* Symlinks require elevated privileges on Windows — skip. * Guard the entire body: symlink() doesn't exist on Windows. */ SKIP_PLATFORM("Windows: symlinks need admin / symlink() unavailable"); #else char *base = th_mktempdir("cbm_disc_sym"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "real.go"), "package main\n"); char real_path[512], link_path[512]; snprintf(real_path, sizeof(real_path), "%s/real.go", base); snprintf(link_path, sizeof(link_path), "%s/link.go", base); symlink(real_path, link_path); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); bool found_real = false, found_link = false; for (int i = 0; i < count; i++) { if (strstr(files[i].rel_path, "real.go")) found_real = true; if (strstr(files[i].rel_path, "link.go")) found_link = true; } ASSERT_TRUE(found_real); ASSERT_FALSE(found_link); cbm_discover_free(files, count); th_cleanup(base); PASS(); #endif } /* #1815: a symlink skip must be as visible as the other two skip paths in * the same walk (gitignore/cbmignore/skip-list files, excluded dirs): a * subtree dropped because it's a symlink must not look identical to a * complete index. */ TEST(discover_symlink_skip_is_reported) { #ifdef _WIN32 SKIP_PLATFORM("Windows: symlinks need admin / symlink() unavailable"); #else char *base = th_mktempdir("cbm_disc_sym_rep"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "real.go"), "package main\n"); char real_path[512], link_path[512]; snprintf(real_path, sizeof(real_path), "%s/real.go", base); snprintf(link_path, sizeof(link_path), "%s/link.go", base); symlink(real_path, link_path); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; cbm_ignored_file_t *ignored = NULL; int ignored_count = 0; int ignored_total = 0; int rc = cbm_discover_ex2(base, &opts, &files, &count, NULL, NULL, &ignored, &ignored_count, &ignored_total); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); /* real.go only, same as discover_symlink_skipped */ ASSERT_EQ(ignored_total, 1); ASSERT_EQ(ignored_count, 1); ASSERT_STR_EQ(ignored[0].rel_path, "link.go"); ASSERT_STR_EQ(ignored[0].reason, "symlink"); cbm_discover_free(files, count); cbm_discover_free_ignored(ignored, ignored_count); th_cleanup(base); PASS(); #endif } TEST(discover_new_ignore_patterns) { char *base = th_mktempdir("cbm_disc_newign"); ASSERT(base != NULL); const char *dirs[] = {".next", ".terraform", "zig-cache", ".cargo", "elm-stuff", "bazel-out"}; for (int i = 0; i < 6; i++) { char path[512]; snprintf(path, sizeof(path), "%s/%s/file.go", base, dirs[i]); th_write_file(path, "package x\n"); } th_write_file(TH_PATH(base, "main.go"), "package main\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_TRUE(strstr(files[0].rel_path, "main.go") != NULL); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_generic_dirs_full_mode) { char *base = th_mktempdir("cbm_disc_genfull"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "bin/main.go"), "package bin\n"); th_write_file(TH_PATH(base, "build/main.go"), "package build\n"); th_write_file(TH_PATH(base, "out/main.go"), "package out\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 3); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_generic_dirs_fast_mode) { char *base = th_mktempdir("cbm_disc_genfast"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "bin/main.go"), "package bin\n"); th_write_file(TH_PATH(base, "build/main.go"), "package build\n"); th_write_file(TH_PATH(base, "out/main.go"), "package out\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FAST}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 0); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_deploy_excluded_full_mode) { char *base = th_mktempdir("cbm_disc_deploy"); ASSERT(base != NULL); th_write_file(TH_PATH(base, "src/main.go"), "package main\n"); th_write_file(TH_PATH(base, "deploy/main.go"), "package deploy\n"); th_write_file(TH_PATH(base, "deployed/main.go"), "package deployed\n"); cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); cbm_discover_free(files, count); th_cleanup(base); PASS(); } TEST(discover_cbmignore_no_git) { char *base = th_mktempdir("cbm_disc_nogit"); ASSERT(base != NULL); th_write_file(TH_PATH(base, ".cbmignore"), "scratch/\n"); th_write_file(TH_PATH(base, "main.go"), "package main\n"); th_write_file(TH_PATH(base, "scratch/tmp.go"), "package scratch\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; int rc = cbm_discover(base, &opts, &files, &count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 1); ASSERT_TRUE(strstr(files[0].rel_path, "main.go") != NULL); cbm_discover_free(files, count); th_cleanup(base); PASS(); } /* ── .cbmignore negation vs built-in skip dirs (issue #500) ────── */ static bool discover_excluded_contains(char **excluded, int count, const char *rel_path) { for (int i = 0; i < count; i++) { if (strcmp(excluded[i], rel_path) == 0) { return true; } } return false; } /* A "!obj/" negation in .cbmignore must un-skip the built-in ALWAYS_SKIP * "obj" dir so files inside it get discovered — and the un-skipped dir must * not be reported as an excluded subtree (#411 list stays coherent). */ TEST(discover_cbmignore_negates_always_skip_dir) { char *base = th_mktempdir("cbm_disc_cbmi_neg_obj"); ASSERT(base != NULL); th_write_file(TH_PATH(base, ".cbmignore"), "!obj/\n"); th_write_file(TH_PATH(base, "main.go"), "package main\n"); th_write_file(TH_PATH(base, "obj/generated.go"), "package obj\n"); cbm_discover_opts_t opts = {0}; cbm_file_info_t *files = NULL; int count = 0; char **excluded = NULL; int excluded_count = 0; int rc = cbm_discover_ex(base, &opts, &files, &count, &excluded, &excluded_count); ASSERT_EQ(rc, 0); ASSERT_EQ(count, 2); ASSERT_TRUE(discover_has_rel_path(files, count, "main.go")); ASSERT_TRUE(discover_has_rel_path(files, count, "obj/generated.go")); ASSERT_FALSE(discover_excluded_contains(excluded, excluded_count, "obj")); cbm_discover_free_excluded(excluded, excluded_count); cbm_discover_free(files, count); th_cleanup(base); PASS(); } /* ── Laravel compiled Blade view cache (issue #1735) ─────────────── */ /* storage/framework/views/ holds Laravel's compiled Blade templates: generated * PHP with no source value that the php_only grammar flags as parse damage. * It is skipped by default even when Laravel's stock .gitignore in that folder * is missing. The match is on the path suffix, component-aligned, so ordinary * views/ dirs (resources/views, app/views, app/framework/views) and look-alike * prefixes (mystorage/...) stay indexed. */ TEST(discover_skips_laravel_compiled_views_issue1735) { char *base = th_mktempdir("cbm_disc_laravel_views"); ASSERT(base != NULL); /* No .gitignore anywhere: the skip must come from the built-in rule. */ th_write_file(TH_PATH(base, "storage/framework/views/abc123.php"), "startSection('title'); ?>\n" "\n" "stopSection(); ?>\n"); th_write_file(TH_PATH(base, "backend/storage/framework/views/def456.php"), "\n"); th_write_file(TH_PATH(base, "resources/views/welcome.blade.php"), "