Skip to content

Commit

Permalink
Source Map Bugfix: Just getcwd() instead of relpath hackery (#691)
Browse files Browse the repository at this point in the history
* Source Map Bugfix: Just getcwd() instead of relpath hackery

* run the new tests serially

* per CR suggestion: cache so don't call os again
  • Loading branch information
tzaffi committed Mar 16, 2023
1 parent d54e165 commit 402c6ed
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
20 changes: 12 additions & 8 deletions pyteal/stack_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,8 @@ def __init__(

self._raw_code: str | None = None
self._status: PyTealFrameStatus | None = None
self._file: str | None = None
self._root: str | None = None

def __repr__(self) -> str:
"""
Expand Down Expand Up @@ -536,18 +538,20 @@ def location(self) -> str:
return f"{self.file()}:{self.lineno()}" if self.frame_info else ""

def file(self) -> str:
if not self.frame_info:
return ""
if self._file is None:
if not self.frame_info:
self._file = ""
else:
path = self.frame_info.filename
self._file = os.path.relpath(path) if self.rel_paths else path

path = self.frame_info.filename
return os.path.relpath(path) if self.rel_paths else path
return self._file

def root(self) -> str:
if not self.frame_info:
return ""
if self._root is None:
self._root = os.getcwd()

path = self.frame_info.filename
return path[: -len(self.file())]
return self._root

def code_qualname(self) -> str:
return (
Expand Down
47 changes: 45 additions & 2 deletions pyteal/stack_frame_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from unittest.mock import Mock
from unittest.mock import Mock, patch

from pyteal.stack_frame import NatalStackFrame, StackFrame
import pytest

from pyteal.stack_frame import NatalStackFrame, PyTealFrame, StackFrame


@pytest.mark.serial
def test_is_pyteal():
FrameInfo = Mock()
FrameInfo.return_value = Mock()
Expand All @@ -18,3 +21,43 @@ def test_is_pyteal():
FrameInfo.return_value.filename = "blahblah/pyteal/not_really..."
sf = StackFrame(FrameInfo(), None, NatalStackFrame())
assert not sf._is_pyteal()


@pytest.mark.serial
def test_file():
FrameInfo = Mock()
FrameInfo.return_value = Mock()

FrameInfo.return_value.filename = "not_pyteal.py"
ptf = PyTealFrame(FrameInfo(), None, NatalStackFrame(), None)
assert ptf._file is None
assert ptf.file() == "not_pyteal.py"
assert ptf._file == "not_pyteal.py"

from_root = "/Users/AMAZINGalgodev/github/algorand/beaker/beaker/"
FrameInfo.return_value.filename = from_root + "client/application_client.py"
ptf = PyTealFrame(FrameInfo(), None, NatalStackFrame(), None)
assert ptf._file is None
ending = (
"../AMAZINGalgodev/github/algorand/beaker/beaker/client/application_client.py"
)
assert ptf.file().endswith(ending)
assert ptf._file == ptf.file()

with patch("os.path.relpath", return_value="FOOFOO"):
FrameInfo.return_value.filename = from_root + "client/application_client.py"
ptf = PyTealFrame(FrameInfo(), None, NatalStackFrame(), None)
assert ptf._file is None
assert ptf.file() == "FOOFOO"
assert ptf._file == "FOOFOO"


def test_root():
FrameInfo = Mock()
FrameInfo.return_value = Mock()
ptf = PyTealFrame(FrameInfo(), None, NatalStackFrame(), None)

assert ptf._root is None
with patch("os.getcwd", return_value="FOOFOO"):
assert ptf.root() == "FOOFOO"
assert ptf._root == "FOOFOO"

0 comments on commit 402c6ed

Please sign in to comment.