|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 2 | +# All rights reserved |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from types import SimpleNamespace |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from executorch.devtools.observatory.lenses.stack_trace import StackTraceLens |
| 14 | +from executorch.devtools.observatory import utils |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize( |
| 18 | + "remote_url, expected", |
| 19 | + [ |
| 20 | + ("git@github.com:pytorch/executorch.git", "https://github.com/pytorch/executorch"), |
| 21 | + ("git@github.qualcomm.com:MLG/executorch", "https://github.qualcomm.com/MLG/executorch"), |
| 22 | + ( |
| 23 | + "ssh://git@github.enterprise.local:8022/org/repo.git", |
| 24 | + "https://github.enterprise.local:8022/org/repo", |
| 25 | + ), |
| 26 | + ("https://github.com/pytorch/executorch.git", "https://github.com/pytorch/executorch"), |
| 27 | + ("file:///tmp/executorch", None), |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_normalize_remote_url(remote_url, expected): |
| 31 | + assert utils._normalize_remote_url(remote_url) == expected # type: ignore[attr-defined] |
| 32 | + |
| 33 | + |
| 34 | +def test_stack_trace_uses_commit_links(monkeypatch, tmp_path): |
| 35 | + repo_root = tmp_path / "repo" |
| 36 | + repo_root.mkdir() |
| 37 | + source_file = repo_root / "pkg" / "module.py" |
| 38 | + source_file.parent.mkdir(parents=True) |
| 39 | + source_file.write_text("line = 1\n") |
| 40 | + |
| 41 | + frame = SimpleNamespace( |
| 42 | + filename=str(source_file), |
| 43 | + function="test_fn", |
| 44 | + lineno=1, |
| 45 | + code_context=["line = 1\n"], |
| 46 | + ) |
| 47 | + |
| 48 | + git_info = SimpleNamespace( |
| 49 | + commit_blob_url="https://github.com/org/repo/blob/abc123", |
| 50 | + branch_blob_url="https://github.com/org/repo/blob/main", |
| 51 | + github_link="https://github.com/org/repo/tree/main", |
| 52 | + ) |
| 53 | + |
| 54 | + from executorch.devtools.observatory.lenses import stack_trace as stack_trace_mod |
| 55 | + |
| 56 | + monkeypatch.setattr(stack_trace_mod, "inspect", SimpleNamespace(stack=lambda: [frame])) |
| 57 | + monkeypatch.setattr(stack_trace_mod, "get_repo_root", lambda: str(repo_root)) |
| 58 | + monkeypatch.setattr(stack_trace_mod, "get_git_info", lambda: git_info) |
| 59 | + monkeypatch.setattr(stack_trace_mod, "is_in_repo", lambda path: str(path).startswith(str(repo_root))) |
| 60 | + |
| 61 | + frames = StackTraceLens._get_stack_trace() |
| 62 | + assert len(frames) == 1 |
| 63 | + assert frames[0]["link"] == "https://github.com/org/repo/blob/abc123/pkg/module.py#L1" |
| 64 | + |
| 65 | + |
| 66 | +def test_stack_trace_falls_back_to_branch_links(monkeypatch, tmp_path): |
| 67 | + repo_root = tmp_path / "repo" |
| 68 | + repo_root.mkdir() |
| 69 | + source_file = repo_root / "pkg" / "fallback.py" |
| 70 | + source_file.parent.mkdir(parents=True) |
| 71 | + source_file.write_text("line = 2\n") |
| 72 | + |
| 73 | + frame = SimpleNamespace( |
| 74 | + filename=str(source_file), |
| 75 | + function="fallback_fn", |
| 76 | + lineno=2, |
| 77 | + code_context=["line = 2\n"], |
| 78 | + ) |
| 79 | + |
| 80 | + git_info = SimpleNamespace( |
| 81 | + commit_blob_url=None, |
| 82 | + branch_blob_url="https://github.com/org/repo/blob/main", |
| 83 | + github_link="https://github.com/org/repo/tree/main", |
| 84 | + ) |
| 85 | + |
| 86 | + from executorch.devtools.observatory.lenses import stack_trace as stack_trace_mod |
| 87 | + |
| 88 | + monkeypatch.setattr(stack_trace_mod, "inspect", SimpleNamespace(stack=lambda: [frame])) |
| 89 | + monkeypatch.setattr(stack_trace_mod, "get_repo_root", lambda: str(repo_root)) |
| 90 | + monkeypatch.setattr(stack_trace_mod, "get_git_info", lambda: git_info) |
| 91 | + monkeypatch.setattr(stack_trace_mod, "is_in_repo", lambda path: str(path).startswith(str(repo_root))) |
| 92 | + |
| 93 | + frames = StackTraceLens._get_stack_trace() |
| 94 | + assert len(frames) == 1 |
| 95 | + assert frames[0]["link"] == "https://github.com/org/repo/blob/main/pkg/fallback.py#L2" |
0 commit comments