frameworks-integration.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, it, expect, beforeAll, afterEach } from 'vitest';
  2. import * as fs from 'fs';
  3. import * as path from 'path';
  4. import * as os from 'os';
  5. import { CodeGraph } from '../src';
  6. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  7. beforeAll(async () => {
  8. await initGrammars();
  9. await loadAllGrammars();
  10. });
  11. describe('Django end-to-end framework extraction', () => {
  12. let tmpDir: string | undefined;
  13. afterEach(() => {
  14. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  15. tmpDir = undefined;
  16. });
  17. it('creates a route->view edge from urls.py to view class', async () => {
  18. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-django-'));
  19. fs.writeFileSync(path.join(tmpDir, 'manage.py'), '# marker\n');
  20. fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'django==4.2\n');
  21. fs.mkdirSync(path.join(tmpDir, 'users'));
  22. fs.writeFileSync(path.join(tmpDir, 'users/__init__.py'), '');
  23. fs.writeFileSync(
  24. path.join(tmpDir, 'users/views.py'),
  25. 'class UserListView:\n def get(self, request): pass\n'
  26. );
  27. fs.writeFileSync(
  28. path.join(tmpDir, 'users/urls.py'),
  29. 'from django.urls import path\n' +
  30. 'from users.views import UserListView\n' +
  31. 'urlpatterns = [path("users/", UserListView.as_view(), name="user-list")]\n'
  32. );
  33. const cg = CodeGraph.initSync(tmpDir);
  34. await cg.indexAll();
  35. // Route node exists
  36. const routes = cg.getNodesByKind('route');
  37. expect(routes.length).toBeGreaterThan(0);
  38. const route = routes.find((n) => n.name === 'users/');
  39. expect(route).toBeDefined();
  40. // View class exists
  41. const classNodes = cg.getNodesByKind('class');
  42. const view = classNodes.find((n) => n.name === 'UserListView');
  43. expect(view).toBeDefined();
  44. // Edge route -> view exists
  45. const edges = cg.getOutgoingEdges(route!.id);
  46. const toView = edges.find((e) => e.target === view!.id);
  47. expect(toView).toBeDefined();
  48. expect(toView!.kind).toBe('references');
  49. cg.close();
  50. });
  51. });
  52. describe('Flask end-to-end framework extraction', () => {
  53. let tmpDir: string | undefined;
  54. afterEach(() => {
  55. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  56. tmpDir = undefined;
  57. });
  58. it('resolves stacked routes across @login_required to a view named after a builtin (index)', async () => {
  59. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-flask-'));
  60. fs.writeFileSync(path.join(tmpDir, 'requirements.txt'), 'flask==3.0\n');
  61. fs.writeFileSync(
  62. path.join(tmpDir, 'app.py'),
  63. 'from flask import Blueprint, render_template\n' +
  64. 'from flask_login import login_required\n' +
  65. 'bp = Blueprint("main", __name__)\n' +
  66. '\n' +
  67. '@bp.route("/", methods=["GET", "POST"])\n' +
  68. '@bp.route("/index", methods=["GET", "POST"])\n' +
  69. '@login_required\n' +
  70. 'def index():\n' +
  71. ' return render_template("index.html")\n'
  72. );
  73. const cg = CodeGraph.initSync(tmpDir);
  74. await cg.indexAll();
  75. // Both stacked @bp.route decorators are extracted (the second was previously
  76. // dropped because @login_required broke the "def must follow" assumption).
  77. const routes = cg.getNodesByKind('route');
  78. expect(routes.map((r) => r.name).sort()).toEqual(['GET /', 'GET /index']);
  79. // The view function exists even though its name is a Python builtin method.
  80. const fn = cg.getNodesByKind('function').find((n) => n.name === 'index');
  81. expect(fn).toBeDefined();
  82. // Both routes resolve to it — exercises the bare-name builtin guard, which
  83. // previously filtered the `index` reference as a builtin method.
  84. for (const route of routes) {
  85. const edges = cg.getOutgoingEdges(route.id);
  86. const toView = edges.find((e) => e.target === fn!.id && e.kind === 'references');
  87. expect(toView, `route ${route.name} should resolve to index()`).toBeDefined();
  88. }
  89. cg.close();
  90. });
  91. });