test_grammar.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. import pytest
  2. from sentry.ownership.grammar import (
  3. Matcher,
  4. Owner,
  5. Rule,
  6. convert_codeowners_syntax,
  7. convert_schema_to_rules_text,
  8. dump_schema,
  9. get_source_code_path_from_stacktrace_path,
  10. load_schema,
  11. parse_code_owners,
  12. parse_rules,
  13. )
  14. fixture_data = """
  15. # cool stuff comment
  16. *.js #frontend m@ROBENOLT.com
  17. # good comment
  18. url:http://google.com/* #backend
  19. path:src/sentry/* david@sentry.io
  20. tags.foo:bar tagperson@sentry.io
  21. tags.foo:"bar baz" tagperson@sentry.io
  22. module:foo.bar #workflow
  23. module:"foo bar" meow@sentry.io
  24. codeowners:/src/components/ githubuser@sentry.io
  25. codeowners:frontend/*.ts githubmod@sentry.io
  26. """
  27. codeowners_fixture_data = r"""
  28. # cool stuff comment
  29. *.js @getsentry/frontend @NisanthanNanthakumar
  30. # good comment
  31. docs/* @getsentry/docs @getsentry/ecosystem
  32. src/sentry/* @AnotherUser
  33. api/* nisanthan.nanthakumar@sentry.io
  34. tests/file\ with\ spaces/ @NisanthanNanthakumar
  35. """
  36. def test_parse_rules():
  37. assert parse_rules(fixture_data) == [
  38. Rule(Matcher("path", "*.js"), [Owner("team", "frontend"), Owner("user", "m@robenolt.com")]),
  39. Rule(Matcher("url", "http://google.com/*"), [Owner("team", "backend")]),
  40. Rule(Matcher("path", "src/sentry/*"), [Owner("user", "david@sentry.io")]),
  41. Rule(Matcher("tags.foo", "bar"), [Owner("user", "tagperson@sentry.io")]),
  42. Rule(Matcher("tags.foo", "bar baz"), [Owner("user", "tagperson@sentry.io")]),
  43. Rule(Matcher("module", "foo.bar"), [Owner("team", "workflow")]),
  44. Rule(Matcher("module", "foo bar"), [Owner("user", "meow@sentry.io")]),
  45. Rule(Matcher("codeowners", "/src/components/"), [Owner("user", "githubuser@sentry.io")]),
  46. Rule(Matcher("codeowners", "frontend/*.ts"), [Owner("user", "githubmod@sentry.io")]),
  47. ]
  48. def test_dump_schema():
  49. assert dump_schema([Rule(Matcher("path", "*.js"), [Owner("team", "frontend")])]) == {
  50. "$version": 1,
  51. "rules": [
  52. {
  53. "matcher": {"type": "path", "pattern": "*.js"},
  54. "owners": [{"type": "team", "identifier": "frontend"}],
  55. }
  56. ],
  57. }
  58. def test_str_schema():
  59. assert str(Rule(Matcher("path", "*.js"), [Owner("team", "frontend")])) == "path:*.js #frontend"
  60. assert (
  61. str(Rule(Matcher("url", "http://google.com/*"), [Owner("team", "backend")]))
  62. == "url:http://google.com/* #backend"
  63. )
  64. assert (
  65. str(Rule(Matcher("tags.foo", "bar"), [Owner("user", "tagperson@sentry.io")]))
  66. == "tags.foo:bar tagperson@sentry.io"
  67. )
  68. assert (
  69. str(Rule(Matcher("tags.foo", "bar baz"), [Owner("user", "tagperson@sentry.io")]))
  70. == "tags.foo:bar baz tagperson@sentry.io"
  71. )
  72. assert (
  73. str(
  74. Rule(Matcher("codeowners", "/src/components/"), [Owner("user", "githubuser@sentry.io")])
  75. )
  76. == "codeowners:/src/components/ githubuser@sentry.io"
  77. )
  78. def test_load_schema():
  79. assert load_schema(
  80. {
  81. "$version": 1,
  82. "rules": [
  83. {
  84. "matcher": {"type": "path", "pattern": "*.js"},
  85. "owners": [{"type": "team", "identifier": "frontend"}],
  86. }
  87. ],
  88. }
  89. ) == [Rule(Matcher("path", "*.js"), [Owner("team", "frontend")])]
  90. def test_load_tag_schema():
  91. assert load_schema(
  92. {
  93. "$version": 1,
  94. "rules": [
  95. {
  96. "matcher": {"type": "tags.release", "pattern": "*"},
  97. "owners": [{"type": "user", "identifier": "test@sentry.io"}],
  98. }
  99. ],
  100. }
  101. ) == [Rule(Matcher("tags.release", "*"), [Owner("user", "test@sentry.io")])]
  102. def test_matcher_test_url():
  103. data = {"request": {"url": "http://example.com/foo.js"}}
  104. assert Matcher("url", "*.js").test(data)
  105. assert Matcher("url", "http://*.com/foo.js").test(data)
  106. assert not Matcher("url", "*.py").test(data)
  107. assert not Matcher("url", "*.jsx").test(data)
  108. assert not Matcher("path", "*.js").test(data)
  109. assert not Matcher("url", "*.js").test({})
  110. def test_matcher_test_url_none():
  111. assert not Matcher("url", "doesnt_matter").test(None)
  112. assert not Matcher("url", "doesnt_matter").test({})
  113. assert not Matcher("url", "doesnt_matter").test({"request": None})
  114. assert not Matcher("url", "doesnt_matter").test({"request": {"url": None}})
  115. def test_matcher_test_exception():
  116. data = {
  117. "exception": {
  118. "values": [
  119. {
  120. "stacktrace": {
  121. "frames": [
  122. {"filename": "foo/file.py"},
  123. {"abs_path": "/usr/local/src/other/app.py"},
  124. ]
  125. }
  126. }
  127. ]
  128. }
  129. }
  130. assert Matcher("path", "*.py").test(data)
  131. assert Matcher("path", "foo/*.py").test(data)
  132. assert Matcher("path", "/usr/local/src/*/app.py").test(data)
  133. assert not Matcher("path", "*.js").test(data)
  134. assert not Matcher("path", "*.jsx").test(data)
  135. assert not Matcher("url", "*.py").test(data)
  136. assert not Matcher("path", "*.py").test({})
  137. def test_matcher_file_abs_path_same_frame():
  138. data = {
  139. "exception": {
  140. "values": [
  141. {
  142. "stacktrace": {
  143. "frames": [
  144. {"filename": "foo/file.py", "abs_path": "/usr/local/src/other/app.py"},
  145. ]
  146. }
  147. }
  148. ]
  149. }
  150. }
  151. assert Matcher("path", "/usr/local/src/*/app.py").test(data)
  152. assert Matcher("path", "*local/src/*").test(data)
  153. def test_matcher_test_stacktrace():
  154. data = {
  155. "stacktrace": {
  156. "frames": [{"filename": "foo/file.py"}, {"abs_path": "/usr/local/src/other/app.py"}]
  157. }
  158. }
  159. assert Matcher("path", "*.py").test(data)
  160. assert Matcher("path", "foo/*.py").test(data)
  161. assert Matcher("path", "/usr/local/src/*/app.py").test(data)
  162. assert not Matcher("path", "*.js").test(data)
  163. assert not Matcher("path", "*.jsx").test(data)
  164. assert not Matcher("url", "*.py").test(data)
  165. assert not Matcher("path", "*.py").test({})
  166. def test_matcher_test_threads():
  167. data = {
  168. "threads": {
  169. "values": [
  170. {
  171. "stacktrace": {
  172. "frames": [
  173. {"filename": "foo/file.py"},
  174. {"abs_path": "/usr/local/src/other/app.py"},
  175. ]
  176. }
  177. }
  178. ]
  179. }
  180. }
  181. assert Matcher("path", "*.py").test(data)
  182. assert Matcher("path", "foo/*.py").test(data)
  183. assert Matcher("path", "/usr/local/src/*/app.py").test(data)
  184. assert Matcher("codeowners", "*.py").test(data)
  185. assert Matcher("codeowners", "foo/*.py").test(data)
  186. assert Matcher("codeowners", "/usr/local/src/*/app.py").test(data)
  187. assert not Matcher("path", "*.js").test(data)
  188. assert not Matcher("path", "*.jsx").test(data)
  189. assert not Matcher("url", "*.py").test(data)
  190. assert not Matcher("path", "*.py").test({})
  191. def test_matcher_test_platform_java_threads():
  192. data = {
  193. "platform": "java",
  194. "threads": {
  195. "values": [
  196. {
  197. "stacktrace": {
  198. "frames": [
  199. {
  200. "module": "jdk.internal.reflect.NativeMethodAccessorImpl",
  201. "filename": "NativeMethodAccessorImpl.java",
  202. }
  203. ]
  204. }
  205. }
  206. ]
  207. },
  208. }
  209. assert Matcher("path", "*.java").test(data)
  210. assert Matcher("path", "jdk/internal/reflect/*.java").test(data)
  211. assert Matcher("path", "jdk/internal/*/NativeMethodAccessorImpl.java").test(data)
  212. assert Matcher("codeowners", "*.java").test(data)
  213. assert Matcher("codeowners", "jdk/internal/reflect/*.java").test(data)
  214. assert Matcher("codeowners", "jdk/internal/*/NativeMethodAccessorImpl.java").test(data)
  215. assert not Matcher("path", "*.js").test(data)
  216. assert not Matcher("path", "*.jsx").test(data)
  217. assert not Matcher("url", "*.py").test(data)
  218. assert not Matcher("path", "*.py").test({})
  219. def test_matcher_test_platform_cocoa_threads():
  220. data = {
  221. "platform": "cocoa",
  222. "threads": {
  223. "values": [
  224. {
  225. "stacktrace": {
  226. "frames": [
  227. {
  228. "package": "SampleProject",
  229. "abs_path": "/Users/gszeto/code/SwiftySampleProject/SampleProject/Classes/App Delegate/AppDelegate.swift",
  230. # munged_filename: "SampleProject/Classes/App Delegate/AppDelegate.swift"
  231. }
  232. ]
  233. }
  234. }
  235. ]
  236. },
  237. }
  238. assert Matcher("path", "*.swift").test(data)
  239. assert Matcher("path", "SampleProject/Classes/App Delegate/AppDelegate.swift").test(data)
  240. assert not Matcher(
  241. "path", "SwiftySampleProject/SampleProject/Classes/App Delegate/AppDelegate.swift"
  242. ).test(data)
  243. assert Matcher("path", "**/App Delegate/AppDelegate.swift").test(data)
  244. assert Matcher("codeowners", "*.swift").test(data)
  245. assert Matcher("codeowners", "SampleProject/Classes/App Delegate/*.swift").test(data)
  246. assert Matcher("codeowners", "SampleProject/Classes/App Delegate/AppDelegate.swift").test(data)
  247. assert not Matcher(
  248. "codeowners", "SwiftySampleProject/SampleProject/Classes/App Delegate/AppDelegate.swift"
  249. ).test(data)
  250. assert Matcher("codeowners", "**/App Delegate/AppDelegate.swift").test(data)
  251. assert not Matcher("path", "*.js").test(data)
  252. assert not Matcher("path", "*.jsx").test(data)
  253. assert not Matcher("url", "*.py").test(data)
  254. assert not Matcher("path", "*.py").test({})
  255. def test_matcher_test_platform_react_native():
  256. data = {
  257. "platform": "javascript",
  258. "exception": {
  259. "values": [
  260. {
  261. "stacktrace": {
  262. "frames": [
  263. {
  264. "function": "callFunctionReturnFlushedQueue",
  265. "module": "react-native/Libraries/BatchedBridge/MessageQueue",
  266. "filename": "node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js",
  267. "abs_path": "app:///node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js",
  268. "lineno": 115,
  269. "colno": 5,
  270. "in_app": False,
  271. "data": {"sourcemap": "app:///main.jsbundle.map"},
  272. },
  273. {
  274. "function": "apply",
  275. "filename": "native",
  276. "abs_path": "native",
  277. "in_app": True,
  278. },
  279. {
  280. "function": "onPress",
  281. "module": "src/screens/EndToEndTestsScreen",
  282. "filename": "src/screens/EndToEndTestsScreen.tsx",
  283. "abs_path": "app:///src/screens/EndToEndTestsScreen.tsx",
  284. "lineno": 57,
  285. "colno": 11,
  286. "in_app": True,
  287. "data": {"sourcemap": "app:///main.jsbundle.map"},
  288. },
  289. ]
  290. }
  291. }
  292. ],
  293. },
  294. }
  295. assert Matcher("path", "src/screens/EndToEndTestsScreen.tsx").test(data)
  296. assert Matcher("path", "src/*/EndToEndTestsScreen.tsx").test(data)
  297. assert Matcher("path", "*/EndToEndTestsScreen.tsx").test(data)
  298. assert Matcher("path", "**/EndToEndTestsScreen.tsx").test(data)
  299. assert Matcher("path", "*.tsx").test(data)
  300. assert Matcher("codeowners", "src/screens/EndToEndTestsScreen.tsx").test(data)
  301. assert Matcher("codeowners", "*.tsx").test(data)
  302. assert not Matcher("url", "*.tsx").test(data)
  303. # external lib matching still works
  304. assert Matcher("path", "**/Libraries/BatchedBridge/MessageQueue.js").test(data)
  305. # we search on filename and abs_path, if a user explicitly tests on the abs_path, we let them
  306. assert Matcher("path", "app:///src/screens/EndToEndTestsScreen.tsx").test(data)
  307. def test_matcher_test_platform_other_flutter():
  308. data = {
  309. "platform": "other",
  310. "sdk": {"name": "sentry.dart.flutter"},
  311. "exception": {
  312. "values": [
  313. {
  314. "type": "StateError",
  315. "value": "Bad state: try catch",
  316. "stacktrace": {
  317. "frames": [
  318. {
  319. "function": "_dispatchPointerDataPacket",
  320. "filename": "hooks.dart",
  321. "abs_path": "dart:ui/hooks.dart",
  322. "lineno": 94,
  323. "colno": 31,
  324. "in_app": False,
  325. },
  326. {
  327. "function": "_InkResponseState._handleTap",
  328. "package": "flutter",
  329. "filename": "ink_well.dart",
  330. "abs_path": "package:flutter/src/material/ink_well.dart",
  331. "lineno": 1005,
  332. "colno": 21,
  333. "in_app": False,
  334. },
  335. {
  336. "function": "MainScaffold.build.<fn>",
  337. "package": "sentry_flutter_example",
  338. "filename": "main.dart",
  339. "abs_path": "package:sentry_flutter_example/main.dart",
  340. "lineno": 117,
  341. "colno": 32,
  342. "in_app": True,
  343. },
  344. {
  345. "function": "tryCatchModule",
  346. "package": "sentry_flutter_example",
  347. "filename": "test.dart",
  348. "abs_path": "package:sentry_flutter_example/a/b/test.dart",
  349. "lineno": 8,
  350. "colno": 5,
  351. "in_app": True,
  352. },
  353. ]
  354. },
  355. }
  356. ]
  357. },
  358. }
  359. assert Matcher("path", "a/b/test.dart").test(data)
  360. assert Matcher("path", "a/*/test.dart").test(data)
  361. assert Matcher("path", "*/test.dart").test(data)
  362. assert Matcher("path", "**/test.dart").test(data)
  363. assert Matcher("path", "*.dart").test(data)
  364. assert Matcher("codeowners", "a/b/test.dart").test(data)
  365. assert Matcher("codeowners", "*.dart").test(data)
  366. assert not Matcher("url", "*.dart").test(data)
  367. # non in-app/user code still works here,
  368. assert Matcher("path", "src/material/ink_well.dart").test(data)
  369. # we search on filename and abs_path, if a user explicitly tests on the abs_path, we let them
  370. assert Matcher("path", "package:sentry_flutter_example/a/b/test.dart").test(data)
  371. def test_matcher_test_platform_none_threads():
  372. data = {
  373. "threads": {
  374. "values": [
  375. {
  376. "stacktrace": {
  377. "frames": [
  378. {
  379. "module": "jdk.internal.reflect.NativeMethodAccessorImpl",
  380. "filename": "NativeMethodAccessorImpl.java",
  381. }
  382. ]
  383. }
  384. }
  385. ]
  386. },
  387. }
  388. # since no platform, we won't be able to fully-qualify(filename munge) based off module and filename
  389. # matching will still work based on just the filename, but we won't be able to match on the src path
  390. assert Matcher("path", "NativeMethodAccessorImpl.java").test(data)
  391. assert Matcher("path", "*.java").test(data)
  392. assert Matcher("codeowners", "NativeMethodAccessorImpl.java").test(data)
  393. assert Matcher("codeowners", "*.java").test(data)
  394. assert not Matcher("path", "jdk/internal/reflect/*.java").test(data)
  395. assert not Matcher("path", "jdk/internal/*/NativeMethodAccessorImpl.java").test(data)
  396. assert not Matcher("path", "*.js").test(data)
  397. assert not Matcher("path", "*.jsx").test(data)
  398. assert not Matcher("codeowners", "jdk/internal/reflect/*.java").test(data)
  399. assert not Matcher("codeowners", "jdk/internal/*/NativeMethodAccessorImpl.java").test(data)
  400. assert not Matcher("codeowners", "*.js").test(data)
  401. assert not Matcher("codeowners", "*.jsx").test(data)
  402. assert not Matcher("url", "*.py").test(data)
  403. assert not Matcher("path", "*.py").test({})
  404. def test_matcher_test_tags():
  405. data = {
  406. "tags": [["foo", "foo_value"], ["bar", "barval"]],
  407. }
  408. assert Matcher("tags.foo", "foo_value").test(data)
  409. assert Matcher("tags.bar", "barval").test(data)
  410. assert not Matcher("tags.barz", "barval").test(data)
  411. def test_matcher_test_module():
  412. data = {
  413. "stacktrace": {
  414. "frames": [
  415. {
  416. "module": "com.android.internal.os.Init",
  417. "filename": "Init.java",
  418. "abs_path": "Init.java",
  419. },
  420. {
  421. "module": "com.android.internal.os.RuntimeInit$MethodAndArgsCaller",
  422. "filename": "RuntimeInit.java",
  423. "abs_path": "RuntimeInit.java",
  424. },
  425. {
  426. "module": "com.sentry.somethinginthemiddle.CustomModuleForMeowing",
  427. "filename": "SourceFile",
  428. "abs_path": "SourceFile",
  429. },
  430. ]
  431. },
  432. }
  433. assert Matcher("module", "*os.Init").test(data)
  434. assert Matcher("module", "*somethinginthemiddle*").test(data)
  435. assert Matcher("module", "com.android.internal.os.RuntimeInit$MethodAndArgsCaller").test(data)
  436. assert Matcher("module", "com.android*").test(data)
  437. assert not Matcher("module", "com.android").test(data)
  438. assert not Matcher("module", "os.Init").test(data)
  439. assert not Matcher("module", "*somethingattheend").test(data)
  440. assert not Matcher("module", "com.android.internal.os").test(data)
  441. @pytest.mark.parametrize("data", [{}, {"tags": None}, {"tags": [None]}])
  442. def test_matcher_test_tags_without_tag_data(data):
  443. assert not Matcher("tags.foo", "foo_value").test(data)
  444. assert not Matcher("tags.bar", "barval").test(data)
  445. def _assert_matcher(matcher: Matcher, path_details, expected):
  446. """Helper function to reduce repeated code"""
  447. frames = {"stacktrace": {"frames": path_details}}
  448. assert matcher.test(frames) == expected
  449. @pytest.mark.parametrize(
  450. "path_details, expected",
  451. [
  452. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  453. (
  454. [
  455. {"filename": "config/subdir/baz.txt"},
  456. {"abs_path": "/usr/local/src/config/subdir/baz.txt"},
  457. ],
  458. True,
  459. ),
  460. ([{"filename": "not_in_repo.py"}, {"abs_path": "/root/not_in_repo.py"}], True),
  461. ],
  462. )
  463. def test_codeowners_match_any_file(path_details, expected):
  464. """* and ** should match to any file"""
  465. _assert_matcher(Matcher("codeowners", "**"), path_details, expected)
  466. _assert_matcher(Matcher("codeowners", "*"), path_details, expected)
  467. @pytest.mark.parametrize(
  468. "path_details, expected",
  469. [
  470. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  471. (
  472. [
  473. {"filename": "config/subdir/baz.txt"},
  474. {"abs_path": "/usr/local/src/config/subdir/baz.txt"},
  475. ],
  476. False,
  477. ),
  478. (
  479. [
  480. {"filename": "config/subdir/baz.py"},
  481. {"abs_path": "/usr/local/src/config/subdir/baz.py"},
  482. ],
  483. True,
  484. ),
  485. (
  486. [
  487. {"filename": "config/dir.py/baz.js"},
  488. {"abs_path": "/usr/local/src/config/dir.py/baz.js"},
  489. ],
  490. True,
  491. ),
  492. ],
  493. )
  494. def test_codeowners_match_extension(path_details, expected):
  495. """*.py should match to any .py file or directory in the repo"""
  496. _assert_matcher(Matcher("codeowners", "*.py"), path_details, expected)
  497. @pytest.mark.parametrize(
  498. "path_details, expected",
  499. [
  500. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  501. (
  502. [
  503. {"filename": "config/subdir/baz.py"},
  504. {"abs_path": "/usr/local/src/config/subdir/baz.py"},
  505. ],
  506. False,
  507. ),
  508. (
  509. [
  510. {"config/subdir/filename": "test.py"},
  511. {"abs_path": "/usr/local/src/config/subdir/test.py"},
  512. ],
  513. True,
  514. ),
  515. (
  516. [
  517. {"filename": "config/test.py/not_test.json"},
  518. {"abs_path": "/usr/local/src/config/test.py/not_test.json"},
  519. ],
  520. True,
  521. ),
  522. ],
  523. )
  524. def test_codeowners_match_specific_filename(path_details, expected):
  525. """test.py should match to any test.py file or directory in the repo"""
  526. _assert_matcher(Matcher("codeowners", "test.py"), path_details, expected)
  527. @pytest.mark.parametrize(
  528. "path_details, expected",
  529. [
  530. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  531. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/foo/test.py"}], False),
  532. (
  533. [
  534. {"filename": "foo/test.py/dir_allowed"},
  535. {"abs_path": "/usr/local/src/foo/test.py/dir_allowed"},
  536. ],
  537. True,
  538. ),
  539. ],
  540. )
  541. def test_codeowners_match_specific_path(path_details, expected):
  542. """
  543. When codeowners is converted to issue owners, the code path is prepended
  544. /usr/local/src/foo/test.py should match to any foo/test.py within the code path
  545. """
  546. _assert_matcher(Matcher("codeowners", "/usr/local/src/foo/test.py"), path_details, expected)
  547. @pytest.mark.parametrize(
  548. "path_details, expected",
  549. [
  550. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  551. (
  552. [
  553. {"filename": "foo/dir.py/py_dir.txt"},
  554. {"abs_path": "/usr/local/src/foo/dir.py/py_dir.txt"},
  555. ],
  556. True,
  557. ),
  558. ([{"filename": "foo/test.txt"}, {"abs_path": "/usr/local/src/foo/test.txt"}], False),
  559. (
  560. [{"filename": "config/foo/test.py"}, {"abs_path": "/usr/local/src/config/foo/test.py"}],
  561. False,
  562. ),
  563. ],
  564. )
  565. def test_codeowners_match_abs_wildcard(path_details, expected):
  566. """/usr/local/src/foo/*.py should match any file or directory"""
  567. _assert_matcher(Matcher("codeowners", "/usr/local/src/foo/*.py"), path_details, expected)
  568. @pytest.mark.parametrize(
  569. "path_details, expected",
  570. [
  571. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  572. (
  573. [{"filename": "foo/subdir/baz.py"}, {"abs_path": "/usr/local/src/foo/subdir/baz.py"}],
  574. True,
  575. ),
  576. ([{"filename": "foo"}, {"abs_path": "/usr/local/src/foo"}], False),
  577. (
  578. [
  579. {"filename": "config/subdir/test.py"},
  580. {"abs_path": "/usr/local/src/config/subdir/test.py"},
  581. ],
  582. False,
  583. ),
  584. (
  585. [
  586. {"filename": "config/src/test.py"},
  587. {"abs_path": "/usr/local/src/config/src/foo/test.py"},
  588. ],
  589. False,
  590. ),
  591. (
  592. [
  593. {"filename": "config/src/foo/subdir/test.py"},
  594. {"abs_path": "/usr/local/src/config/src/foo/subdir/test.py"},
  595. ],
  596. False,
  597. ),
  598. ],
  599. )
  600. def test_codeowners_match_recursive_directory(path_details, expected):
  601. """
  602. /usr/local/src/foo/ should match recursively to any file within the /src/foo directory"
  603. /usr/local/src/foo/** should do the same"
  604. """
  605. _assert_matcher(Matcher("codeowners", "/usr/local/src/foo/"), path_details, expected)
  606. _assert_matcher(Matcher("codeowners", "/usr/local/src/foo/**"), path_details, expected)
  607. @pytest.mark.parametrize(
  608. "path_details, expected",
  609. [
  610. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  611. (
  612. [{"filename": "foo/subdir/baz.py"}, {"abs_path": "/usr/local/src/foo/subdir/baz.py"}],
  613. False,
  614. ),
  615. (
  616. [
  617. {"filename": "config/subdir/test.py"},
  618. {"abs_path": "/usr/local/src/config/subdir/test.py"},
  619. ],
  620. False,
  621. ),
  622. (
  623. [
  624. {"filename": "config/src/foo/test.py"},
  625. {"abs_path": "/usr/local/src/config/src/foo/test.py"},
  626. ],
  627. False,
  628. ),
  629. ],
  630. )
  631. def test_codeowners_match_nonrecursive_directory(path_details, expected):
  632. """
  633. /src/foo/* should match to any file directly within the /src/foo directory
  634. src/foo/* should match to any file directly withing any src/foo directory
  635. """
  636. _assert_matcher(Matcher("codeowners", "/usr/local/src/foo/*"), path_details, expected)
  637. @pytest.mark.parametrize(
  638. "path_details, single_star_expected, double_star_expected",
  639. [
  640. (
  641. [{"filename": "foo/bar/test.py"}, {"abs_path": "/usr/local/src/foo/bar/test.py"}],
  642. True,
  643. True,
  644. ),
  645. (
  646. [
  647. {"filename": "foo/bar/baz/test.py"},
  648. {"abs_path": "/usr/local/src/foo/bar/baz/test.py"},
  649. ],
  650. False,
  651. True,
  652. ),
  653. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], False, True),
  654. ([{"filename": "test.py"}, {"abs_path": "/usr/local/src/test.py"}], False, False),
  655. ],
  656. )
  657. def test_codeowners_match_wildcard_directory(
  658. path_details, single_star_expected, double_star_expected
  659. ):
  660. """
  661. /src/foo/*/test.py should only match with test.py 1 directory deeper than foo
  662. /src/foo/**/test.py can match with test.py anywhere under foo
  663. """
  664. _assert_matcher(Matcher("codeowners", "foo/*/test.py"), path_details, single_star_expected)
  665. _assert_matcher(
  666. Matcher("codeowners", "/usr/local/src/foo/*/test.py"), path_details, single_star_expected
  667. )
  668. _assert_matcher(Matcher("codeowners", "foo/**/test.py"), path_details, double_star_expected)
  669. _assert_matcher(
  670. Matcher("codeowners", "/usr/local/src/foo/**/test.py"), path_details, double_star_expected
  671. )
  672. @pytest.mark.parametrize(
  673. "path_details, expected",
  674. [
  675. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  676. ([{"filename": "foo/test.jy"}, {"abs_path": "/usr/local/src/foo/test.jy"}], True),
  677. ([{"filename": "foo/test.;y"}, {"abs_path": "/usr/local/src/foo/test.;y"}], True),
  678. ([{"filename": "foo/test.pt"}, {"abs_path": "/usr/local/src/foo/test.pt"}], False),
  679. ([{"filename": "foo/test./y"}, {"abs_path": "/usr/local/src/foo/test./y"}], False),
  680. ],
  681. )
  682. def test_codeowners_match_question_mark(path_details, expected):
  683. """
  684. "?" should match any character execept slash
  685. """
  686. _assert_matcher(Matcher("codeowners", "test.?y"), path_details, expected)
  687. @pytest.mark.parametrize(
  688. "path_details, expected",
  689. [
  690. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  691. ([{"filename": "bar/foo/test.py"}, {"abs_path": "/usr/local/src/bar/foo/test.jy"}], True),
  692. ([{"filename": "foo"}, {"abs_path": "/usr/local/src/foo"}], False),
  693. ],
  694. )
  695. def test_codeowners_match_loose_directory(path_details, expected):
  696. """
  697. unanchored directories can match to a foo directory anywhere in the tree
  698. """
  699. _assert_matcher(Matcher("codeowners", "foo/"), path_details, expected)
  700. @pytest.mark.parametrize(
  701. "path_details, expected",
  702. [
  703. ([{"filename": "foo/test.py"}, {"abs_path": "/usr/local/src/foo/test.py"}], True),
  704. ([{"filename": "foo/test.js"}, {"abs_path": "/usr/local/src/foo/test.js"}], True),
  705. ([{"filename": "foo/test."}, {"abs_path": "/usr/local/src/foo/test."}], True),
  706. ([{"filename": "foo/test.d/file"}, {"abs_path": "/usr/local/src/foo/test.d/file"}], True),
  707. ([{"filename": "foo/test./file"}, {"abs_path": "/usr/local/src/foo/test./file"}], True),
  708. ],
  709. )
  710. def test_codeowners_match_wildcard_extension(path_details, expected):
  711. """
  712. "*" can match 0 or more characters in files or directories
  713. """
  714. _assert_matcher(Matcher("codeowners", "test.*"), path_details, expected)
  715. @pytest.mark.parametrize(
  716. "path_details, expected",
  717. [
  718. ([{"filename": "foo/\\"}, {"abs_path": "/usr/local/src/foo/\\"}], True),
  719. (
  720. [
  721. {"filename": "foo/subdir/\\filename"},
  722. {"abs_path": "/usr/local/src/foo/subdir/\\filename"},
  723. ],
  724. False,
  725. ),
  726. (
  727. [
  728. {"filename": "foo/subdir/\\/backslash_dir"},
  729. {"abs_path": "/usr/local/src/foo/subdir/\\/backslash_dir"},
  730. ],
  731. True,
  732. ),
  733. (
  734. [
  735. {"filename": "config/subdir/test.py"},
  736. {"abs_path": "/usr/local/src/config/subdir/test.py"},
  737. ],
  738. False,
  739. ),
  740. ],
  741. )
  742. def test_codeowners_match_backslash(path_details, expected):
  743. """\\ should ignore anything after the backslash and only match with files named '\'"""
  744. _assert_matcher(Matcher("codeowners", "\\filename"), path_details, expected)
  745. @pytest.mark.parametrize(
  746. "path_details, expected",
  747. [
  748. ([{"filename": "foo/"}, {"abs_path": "/usr/local/src/foo/"}], True),
  749. (
  750. [
  751. {"filename": "/foo/subdir/"},
  752. {"abs_path": "/usr/local/src/foo/subdir/"},
  753. ],
  754. True,
  755. ),
  756. (
  757. [
  758. {"filename": "config/subdir/test.py"},
  759. {"abs_path": "/usr/local/src/config/subdir/test.py"},
  760. ],
  761. True,
  762. ),
  763. ],
  764. )
  765. def test_codeowners_match_forwardslash(path_details, expected):
  766. _assert_matcher(Matcher("codeowners", "/"), path_details, expected)
  767. def test_codeowners_match_threads():
  768. data = {
  769. "threads": {
  770. "values": [
  771. {
  772. "stacktrace": {
  773. "frames": [
  774. {"filename": "foo/file.py"},
  775. {"abs_path": "/usr/local/src/other/app.py"},
  776. ]
  777. },
  778. "crashed": False,
  779. "current": False,
  780. }
  781. ]
  782. }
  783. }
  784. assert Matcher("codeowners", "*.py").test(data)
  785. assert Matcher("codeowners", "foo/file.py").test(data)
  786. assert Matcher("codeowners", "/**/app.py").test(data)
  787. assert Matcher("codeowners", "/usr/*/src/*/app.py").test(data)
  788. def test_parse_code_owners():
  789. assert parse_code_owners(codeowners_fixture_data) == (
  790. ["@getsentry/frontend", "@getsentry/docs", "@getsentry/ecosystem"],
  791. ["@NisanthanNanthakumar", "@AnotherUser", "@NisanthanNanthakumar"],
  792. ["nisanthan.nanthakumar@sentry.io"],
  793. )
  794. def test_parse_code_owners_with_line_of_spaces():
  795. data = f"{codeowners_fixture_data}\n \n"
  796. assert parse_code_owners(data) == (
  797. ["@getsentry/frontend", "@getsentry/docs", "@getsentry/ecosystem"],
  798. ["@NisanthanNanthakumar", "@AnotherUser", "@NisanthanNanthakumar"],
  799. ["nisanthan.nanthakumar@sentry.io"],
  800. )
  801. def test_get_source_code_path_from_stacktrace_path():
  802. code_mapping = type("", (), {})()
  803. code_mapping.stack_root = "webpack://docs"
  804. code_mapping.source_root = "docs"
  805. assert (
  806. get_source_code_path_from_stacktrace_path(
  807. "webpack://docs/index.js",
  808. code_mapping,
  809. )
  810. == "docs/index.js"
  811. )
  812. def test_convert_codeowners_syntax():
  813. code_mapping = type("", (), {})()
  814. code_mapping.stack_root = "webpack://docs"
  815. code_mapping.source_root = "docs"
  816. assert (
  817. convert_codeowners_syntax(
  818. codeowners_fixture_data,
  819. {
  820. "@getsentry/frontend": "front-sentry",
  821. "@getsentry/docs": "docs-sentry",
  822. "@getsentry/ecosystem": "ecosystem",
  823. "@NisanthanNanthakumar": "nisanthan.nanthakumar@sentry.io",
  824. "@AnotherUser": "anotheruser@sentry.io",
  825. "nisanthan.nanthakumar@sentry.io": "nisanthan.nanthakumar@sentry.io",
  826. },
  827. code_mapping,
  828. )
  829. == "\n# cool stuff comment\ncodeowners:*.js front-sentry nisanthan.nanthakumar@sentry.io\n# good comment\n\n\ncodeowners:webpack://docs/* docs-sentry ecosystem\ncodeowners:src/sentry/* anotheruser@sentry.io\ncodeowners:api/* nisanthan.nanthakumar@sentry.io\n"
  830. )
  831. def test_convert_codeowners_syntax_excludes_invalid():
  832. code_mapping = type("", (), {})()
  833. code_mapping.stack_root = "webpack://static/"
  834. code_mapping.source_root = ""
  835. codeowners = (
  836. codeowners_fixture_data
  837. + r"""
  838. # some invalid rules
  839. debug[0-9].log @NisanthanNanthakumar
  840. !important/*.log @NisanthanNanthakumar
  841. file 1.txt @NisanthanNanthakumar @getsentry/ecosystem
  842. \#somefile.txt @NisanthanNanthakumar
  843. # some anchored paths
  844. /scripts/test.js @getsentry/ops
  845. config/hooks @getsentry/ops
  846. config/relay/ @getsentry/relay
  847. # not anchored path
  848. docs-ui/ @getsentry/docs @getsentry/ecosystem
  849. """
  850. )
  851. assert (
  852. convert_codeowners_syntax(
  853. codeowners,
  854. {
  855. "@getsentry/frontend": "front-sentry",
  856. "@getsentry/docs": "docs-sentry",
  857. "@getsentry/ecosystem": "ecosystem",
  858. "@getsentry/ops": "ops",
  859. "@getsentry/relay": "relay",
  860. "@NisanthanNanthakumar": "nisanthan.nanthakumar@sentry.io",
  861. "@AnotherUser": "anotheruser@sentry.io",
  862. "nisanthan.nanthakumar@sentry.io": "nisanthan.nanthakumar@sentry.io",
  863. },
  864. code_mapping,
  865. )
  866. == """
  867. # cool stuff comment
  868. codeowners:*.js front-sentry nisanthan.nanthakumar@sentry.io
  869. # good comment
  870. codeowners:webpack://static/docs/* docs-sentry ecosystem
  871. codeowners:webpack://static/src/sentry/* anotheruser@sentry.io
  872. codeowners:webpack://static/api/* nisanthan.nanthakumar@sentry.io
  873. # some invalid rules
  874. codeowners:file nisanthan.nanthakumar@sentry.io ecosystem
  875. # some anchored paths
  876. codeowners:webpack://static/scripts/test.js ops
  877. codeowners:webpack://static/config/hooks ops
  878. codeowners:webpack://static/config/relay/ relay
  879. # not anchored path
  880. codeowners:docs-ui/ docs-sentry ecosystem
  881. """
  882. )
  883. def test_convert_schema_to_rules_text():
  884. assert (
  885. convert_schema_to_rules_text(
  886. {
  887. "$version": 1,
  888. "rules": [
  889. {
  890. "matcher": {"type": "path", "pattern": "*.js"},
  891. "owners": [
  892. {"type": "team", "identifier": "frontend"},
  893. {"type": "user", "identifier": "m@robenolt.com"},
  894. ],
  895. },
  896. {
  897. "matcher": {"type": "url", "pattern": "http://google.com/*"},
  898. "owners": [{"type": "team", "identifier": "backend"}],
  899. },
  900. {
  901. "matcher": {"type": "path", "pattern": "src/sentry/*"},
  902. "owners": [{"type": "user", "identifier": "david@sentry.io"}],
  903. },
  904. {
  905. "matcher": {"type": "tags.foo", "pattern": "bar"},
  906. "owners": [{"type": "user", "identifier": "tagperson@sentry.io"}],
  907. },
  908. {
  909. "matcher": {"type": "tags.foo", "pattern": "bar baz"},
  910. "owners": [{"type": "user", "identifier": "tagperson@sentry.io"}],
  911. },
  912. {
  913. "matcher": {"type": "module", "pattern": "foo.bar"},
  914. "owners": [{"type": "team", "identifier": "workflow"}],
  915. },
  916. {
  917. "matcher": {"type": "module", "pattern": "foo bar"},
  918. "owners": [{"type": "user", "identifier": "meow@sentry.io"}],
  919. },
  920. ],
  921. }
  922. )
  923. == "path:*.js #frontend m@robenolt.com\nurl:http://google.com/* #backend\npath:src/sentry/* david@sentry.io\ntags.foo:bar tagperson@sentry.io\ntags.foo:bar baz tagperson@sentry.io\nmodule:foo.bar #workflow\nmodule:foo bar meow@sentry.io\n"
  924. )