Skip to content

Commit 7cd934a

Browse files
committed
feat(#45): require full rsc pin name
1 parent 745785e commit 7cd934a

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

.env.dev

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# This allows the unit tests to run, while not using
2+
# the full <user_name>/<pin_name> format.
3+
PINS_ALLOW_RSC_SHORT_NAME=1
4+
15
# Pins optional config ----
26
#PINS_CACHE_DIR=.pins_cache
37
#PINS_DATA_DIR=.pins_data

.github/workflows/ci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ on:
88
release:
99
types: [published]
1010

11+
env:
12+
PINS_ALLOW_RSC_SHORT_NAME: 1
13+
1114
jobs:
1215
tests:
1316
name: "Tests"

pins/boards.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .errors import PinsError
1717
from .drivers import load_data, save_data, default_title
1818
from .utils import inform
19+
from .config import get_allow_rsc_short_name
1920

2021

2122
_log = logging.getLogger(__name__)
@@ -776,6 +777,16 @@ def pin_versions_prune(self, *args, **kwargs):
776777
super().pin_versions_prune(*args, **kwargs)
777778

778779
def validate_pin_name(self, name) -> None:
780+
# this should be the default behavior, expecting a full pin name.
781+
# but because the tests use short names, we allow it to be disabled via config
782+
if not get_allow_rsc_short_name() and name.count("/") != 1:
783+
raise ValueError(
784+
f"Invalid pin name: {name}"
785+
"\nRStudio Connect pin names must include user name. E.g. "
786+
"\nsome_user/mtcars, for the user some_user."
787+
)
788+
789+
# less strict test, that allows no slash: e.g. "mtcars"
779790
if name.count("/") > 1 or name.lstrip().startswith("/"):
780791
raise ValueError(f"Invalid pin name: {name}")
781792

pins/config.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@
77
PINS_ENV_DATA_DIR = "PINS_DATA_DIR"
88
PINS_ENV_CACHE_DIR = "PINS_CACHE_DIR"
99
PINS_ENV_INSECURE_READ = "PINS_ALLOW_PICKLE_READ"
10+
PINS_ENV_ALLOW_RSC_SHORT_NAME = "PINS_ALLOW_RSC_SHORT_NAME"
1011

1112
pins_options = SimpleNamespace(quiet=False)
1213

1314

15+
def _interpret_int(env_var_name):
16+
env_var = os.environ.get(env_var_name, "0")
17+
try:
18+
env_int = int(env_var)
19+
except ValueError:
20+
raise ValueError(
21+
f"{env_var_name} must be '0' or '1', but was set to " f"{repr(env_var)}."
22+
)
23+
24+
flag = bool(env_int)
25+
return flag
26+
27+
1428
def get_data_dir():
1529
return os.environ.get(PINS_ENV_DATA_DIR, appdirs.user_data_dir(PINS_NAME))
1630

@@ -21,15 +35,10 @@ def get_cache_dir():
2135

2236
def get_allow_pickle_read(flag):
2337
if flag is None:
24-
env_var = os.environ.get(PINS_ENV_INSECURE_READ, "0")
25-
try:
26-
env_int = int(env_var)
27-
except ValueError:
28-
raise ValueError(
29-
f"{PINS_ENV_INSECURE_READ} must be '0' or '1', but was set to "
30-
f"{repr(env_var)}."
31-
)
32-
33-
flag = bool(env_int)
38+
return _interpret_int(PINS_ENV_INSECURE_READ)
3439

3540
return flag
41+
42+
43+
def get_allow_rsc_short_name():
44+
return _interpret_int(PINS_ENV_ALLOW_RSC_SHORT_NAME)

pins/tests/test_boards.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,26 @@ def test_board_rsc_path_to_pin_safe(board_short):
396396
assert board_short.path_to_pin("me/some_pin") == "me/some_pin"
397397

398398

399+
@pytest.mark.fs_rsc
400+
def test_board_rsc_require_full_pin_name(board_short):
401+
# the tests set the special env var below to allow short pin names,
402+
# but here we test this is not what should happen by default.
403+
# we test that full names work with the RSC board above, so these two
404+
# things should cover us.
405+
from pins.config import PINS_ENV_ALLOW_RSC_SHORT_NAME
406+
407+
with rm_env(PINS_ENV_ALLOW_RSC_SHORT_NAME):
408+
with pytest.raises(ValueError) as exc_info:
409+
board_short.validate_pin_name("mtcars")
410+
411+
assert "Invalid pin name" in exc_info.value.args[0]
412+
413+
with pytest.raises(ValueError) as exc_info:
414+
board_short.path_to_pin("mtcars")
415+
416+
assert "Invalid pin name" in exc_info.value.args[0]
417+
418+
399419
# Manual Board Specific =======================================================
400420

401421
from pins.boards import BoardManual # noqa

0 commit comments

Comments
 (0)