-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add Posit product environment check helpers #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||||||||||||||||||||
from __future__ import annotations | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
import os | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def get_product() -> str | None: | ||||||||||||||||||||||||
"""Returns the product name if called with a Posit product. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
The products will always set the environment variable `POSIT_PRODUCT=<product-name>` | ||||||||||||||||||||||||
or `RSTUDIO_PRODUCT=<product-name>`. | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
RSTUDIO_PRODUCT is deprecated and acts as a fallback for backwards compatibility. | ||||||||||||||||||||||||
It is recommended to use POSIT_PRODUCT instead. | ||||||||||||||||||||||||
""" | ||||||||||||||||||||||||
return os.getenv("POSIT_PRODUCT") or os.getenv("RSTUDIO_PRODUCT") | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def is_local() -> bool: | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like your suggestion above. will leave deprecation warning in utils is_local but can map it use the global vars. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. going to keep this since staying with functional approach |
||||||||||||||||||||||||
"""Returns true if called while running locally.""" | ||||||||||||||||||||||||
return get_product() is None | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def is_running_on_connect() -> bool: | ||||||||||||||||||||||||
"""Returns true if called from a piece of content running on a Connect server.""" | ||||||||||||||||||||||||
return get_product() == "CONNECT" | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
def is_running_on_workbench() -> bool: | ||||||||||||||||||||||||
"""Returns true if called from within a Workbench server.""" | ||||||||||||||||||||||||
return get_product() == "WORKBENCH" | ||||||||||||||||||||||||
Comment on lines
+1
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to support dynamic environment variables here? Another option is to use static variables. from __future__ import annotations
import os
POSIT_PRODUCT = os.getenv("POSIT_PRODUCT") or os.getenv("RSTUDIO_PRODUCT")
IS_POSIT_CONNECT = POSIT_PRODUCT == "CONNECT"
IS_POSIT_WORKBENCH = POSIT_PRODUCT == "WORKBENCH" Then from from .. import runtime
if not runtime.POSIT_PRODUCT:
return; Feel free to disregard this if you have already thought through it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is great feedback. did not consider this. i think i could go either way here. your approach is definitely simpler. let me ensure its testable. i would think just importing the module should trigger this to run but can check. The connectapi and connectcreds equivalents are functions so followed suit but i like simple :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be a little misleading for users today since workbench doesn't support this var yet. I haven't tested this on workbench yet but I'm expecting to do something like this: posit-sdk-py/src/posit/connect/_utils.py Lines 35 to 45 in a089705
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the functional approach is going to best here, especially from a testing perspective. Complicates things when the values are set upon import. Going to keep it functional. @dbkegley yeah that is fine! can add these checks into the workbench runtime check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atheriel, do you have any thoughts here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always the option punt and not have any workbench related check for now. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# pyright: reportFunctionMemberAccess=false | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from posit.environment import ( | ||
get_product, | ||
is_local, | ||
is_running_on_connect, | ||
is_running_on_workbench, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("posit_product", "rstudio_product", "expected"), | ||
[ | ||
("CONNECT", None, "CONNECT"), | ||
(None, "WORKBENCH", "WORKBENCH"), | ||
("CONNECT", "WORKBENCH", "CONNECT"), | ||
(None, None, None), | ||
], | ||
) | ||
def test_get_product(posit_product, rstudio_product, expected): | ||
env = {} | ||
if posit_product is not None: | ||
env["POSIT_PRODUCT"] = posit_product | ||
if rstudio_product is not None: | ||
env["RSTUDIO_PRODUCT"] = rstudio_product | ||
with patch.dict("os.environ", env, clear=True): | ||
assert get_product() == expected | ||
|
||
|
||
def test_is_local(): | ||
with patch("posit.environment.get_product", return_value=None): | ||
assert is_local() is True | ||
|
||
with patch("posit.environment.get_product", return_value="CONNECT"): | ||
assert is_local() is False | ||
|
||
|
||
def test_is_running_on_connect(): | ||
with patch("posit.environment.get_product", return_value="CONNECT"): | ||
assert is_running_on_connect() is True | ||
|
||
with patch("posit.environment.get_product", return_value="WORKBENCH"): | ||
assert is_running_on_connect() is False | ||
|
||
|
||
def test_is_running_on_workbench(): | ||
with patch("posit.environment.get_product", return_value="WORKBENCH"): | ||
assert is_running_on_workbench() is True | ||
|
||
with patch("posit.environment.get_product", return_value="CONNECT"): | ||
assert is_running_on_workbench() is False |
Uh oh!
There was an error while loading. Please reload this page.