android-res-exclusion.test.ts 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Android resource XML is excluded from the index by default (#1047).
  3. *
  4. * A `res/` tree holds only non-code resources (layouts, value bags, drawables,
  5. * menus) split into typed, optionally qualified subdirectories. None of it yields
  6. * a code symbol, yet on an Android app it dominates the file count (one report:
  7. * 26k XML = 97% of files, 0 symbols), bloating the DB, slowing indexing, and
  8. * skewing explore results. CodeGraph now default-ignores the Android resource
  9. * type directories — `res/layout/`, `res/values/`, `res/drawable/`, … and their
  10. * `-<qualifier>` variants — at discovery.
  11. *
  12. * Guardrails this locks in:
  13. * - Real code (`.java`) is still indexed.
  14. * - The one XML that DOES carry symbols — a MyBatis mapper under
  15. * `src/main/resources/` — is untouched (it never lives under `res/`).
  16. * - Plain non-`res/` XML (`pom.xml`) is unaffected.
  17. * - `res/raw/` is deliberately KEPT — it holds arbitrary bundled assets that can
  18. * be code-ish, so we don't drop it.
  19. */
  20. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  21. import * as fs from 'fs';
  22. import * as path from 'path';
  23. import * as os from 'os';
  24. import CodeGraph from '../src/index';
  25. describe('Android resource XML exclusion (#1047)', () => {
  26. let dir: string;
  27. let cg: CodeGraph;
  28. const write = (rel: string, body: string) => {
  29. const p = path.join(dir, rel);
  30. fs.mkdirSync(path.dirname(p), { recursive: true });
  31. fs.writeFileSync(p, body);
  32. };
  33. beforeEach(async () => {
  34. dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-android-res-'));
  35. // Android resource files (every typed subdir, incl. a locale qualifier) — all
  36. // should be EXCLUDED.
  37. write('app/src/main/res/layout/activity_main.xml', '<LinearLayout><TextView/></LinearLayout>\n');
  38. write('app/src/main/res/values/strings.xml', '<resources><string name="app_name">App</string></resources>\n');
  39. write('app/src/main/res/values-es/strings.xml', '<resources><string name="app_name">App</string></resources>\n');
  40. write('app/src/main/res/drawable/ic_foo.xml', '<vector android:height="24dp"/>\n');
  41. write('app/src/main/res/menu/main_menu.xml', '<menu><item android:id="@+id/x"/></menu>\n');
  42. // Real code, a MyBatis mapper (the one XML with symbols), plain XML, and a
  43. // res/raw asset — all should be KEPT.
  44. write('app/src/main/java/com/example/Main.java', 'package com.example;\npublic class Main { void run(){} }\n');
  45. write('src/main/resources/FooMapper.xml',
  46. '<mapper namespace="com.example.FooDao"><select id="findAll">SELECT * FROM foo</select></mapper>\n');
  47. write('pom.xml', '<project><artifactId>demo</artifactId></project>\n');
  48. write('app/src/main/res/raw/payload.xml', '<data><item>1</item></data>\n');
  49. cg = CodeGraph.initSync(dir);
  50. await cg.indexAll();
  51. });
  52. afterEach(() => {
  53. if (cg) cg.destroy();
  54. if (fs.existsSync(dir)) fs.rmSync(dir, { recursive: true, force: true });
  55. });
  56. it('excludes Android resource XML but keeps code, MyBatis mappers, plain XML, and res/raw', () => {
  57. const indexed = new Set(cg.getFiles().map((f) => f.path));
  58. // Excluded: every resource type dir, including the qualifier variant.
  59. expect(indexed).not.toContain('app/src/main/res/layout/activity_main.xml');
  60. expect(indexed).not.toContain('app/src/main/res/values/strings.xml');
  61. expect(indexed).not.toContain('app/src/main/res/values-es/strings.xml');
  62. expect(indexed).not.toContain('app/src/main/res/drawable/ic_foo.xml');
  63. expect(indexed).not.toContain('app/src/main/res/menu/main_menu.xml');
  64. // Kept: real code, plain XML, and the deliberately-spared res/raw asset.
  65. expect(indexed).toContain('app/src/main/java/com/example/Main.java');
  66. expect(indexed).toContain('pom.xml');
  67. expect(indexed).toContain('app/src/main/res/raw/payload.xml');
  68. // Kept AND still carries symbols: the MyBatis mapper (non-regression — the
  69. // only valuable XML, and it never lives under res/).
  70. const mapper = cg.getFiles().find((f) => f.path === 'src/main/resources/FooMapper.xml');
  71. expect(mapper).toBeDefined();
  72. expect(mapper!.nodeCount).toBeGreaterThan(1); // file node + ≥1 statement
  73. });
  74. });