Skip to content

Commit 530f423

Browse files
committed
Support ~ in input paths
The Arduino user folder (sketchbook) is at /home/github/Arduino and the Arduino data folder at /home/github, but the /home/github path is not obvious or documented and there is no way to use the HOME environment variable in inputs to this action. So the most user friendly way to specify these paths outside the workspace is using ~. This is not supported by default.
1 parent 91d68e0 commit 530f423

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

compilesketches/compilesketches.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def install_libraries_from_path(self, library_list):
626626
# If a name was specified, use it
627627
destination_name = library[self.dependency_destination_name_key]
628628
elif (
629-
source_path == pathlib.Path(os.environ["GITHUB_WORKSPACE"])
629+
source_path == absolute_path(os.environ["GITHUB_WORKSPACE"])
630630
):
631631
# If source_path is the root of the workspace (i.e., repository root), name the folder according to the
632632
# repository name, otherwise it will unexpectedly be "workspace"
@@ -1047,9 +1047,9 @@ def path_relative_to_workspace(path):
10471047
Keyword arguments:
10481048
path -- the path to make relative
10491049
"""
1050-
path = pathlib.Path(path)
1050+
path = absolute_path(path=path)
10511051
try:
1052-
relative_path = path.relative_to(os.environ["GITHUB_WORKSPACE"])
1052+
relative_path = path.relative_to(absolute_path(path=os.environ["GITHUB_WORKSPACE"]))
10531053
except ValueError:
10541054
# Path is outside workspace, so just use the given path
10551055
relative_path = path
@@ -1064,11 +1064,15 @@ def absolute_path(path):
10641064
Keyword arguments:
10651065
path -- the path to make absolute
10661066
"""
1067-
path = pathlib.Path(path)
1067+
# Make path into a pathlib.Path object, with ~ expanded
1068+
path = pathlib.Path(path).expanduser()
10681069
if not path.is_absolute():
10691070
# path is relative
10701071
path = pathlib.Path(os.environ["GITHUB_WORKSPACE"], path)
10711072

1073+
# Resolve .. and symlinks to get a true absolute path
1074+
path = path.resolve()
1075+
10721076
return path
10731077

10741078

compilesketches/tests/test_compilesketches.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,9 @@ def test_install_libraries_from_path(capsys, monkeypatch, mocker, path_exists, l
899899
joinpath_calls.append(unittest.mock.call(libraries_path, expected_destination_name))
900900
symlink_to_calls.append(
901901
unittest.mock.call(symlink_source_path,
902-
target=library[compilesketches.CompileSketches.dependency_source_path_key],
902+
target=compilesketches.absolute_path(
903+
library[compilesketches.CompileSketches.dependency_source_path_key]
904+
),
903905
target_is_directory=True))
904906

905907
# noinspection PyUnresolvedReferences
@@ -1500,22 +1502,47 @@ def test_parse_boolean_input(boolean_input, expected_output):
15001502
assert compilesketches.parse_boolean_input(boolean_input=boolean_input) == expected_output
15011503

15021504

1503-
def test_path_relative_to_workspace(monkeypatch):
1505+
@pytest.mark.parametrize("path, expected_relative_path",
1506+
# Path under workspace
1507+
[("/fooWorkspace/baz", pathlib.PurePath("baz")),
1508+
# Path outside workspace
1509+
("/bar/foo", pathlib.Path("/").resolve().joinpath("bar", "foo"))])
1510+
def test_path_relative_to_workspace(monkeypatch, path, expected_relative_path):
15041511
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
15051512

1506-
assert compilesketches.path_relative_to_workspace(path=pathlib.PurePath("/fooWorkspace", "baz")
1507-
) == pathlib.PurePath("baz")
1508-
assert compilesketches.path_relative_to_workspace(path="/fooWorkspace/baz") == pathlib.PurePath("baz")
1509-
# Test path outside workspace
1510-
assert compilesketches.path_relative_to_workspace(path="/bar/foo") == pathlib.PurePath("/bar/foo")
1513+
assert compilesketches.path_relative_to_workspace(path=path) == expected_relative_path
1514+
assert compilesketches.path_relative_to_workspace(path=pathlib.PurePath(path)) == expected_relative_path
15111515

15121516

1513-
@pytest.mark.parametrize("path, expected_absolute_path", [("/asdf", "/asdf"), ("asdf", "/fooWorkspace/asdf")])
1517+
@pytest.mark.parametrize("path, expected_absolute_path",
1518+
# Absolute path
1519+
[("/asdf", pathlib.Path("/").resolve().joinpath("asdf")),
1520+
# Relative path
1521+
("asdf", pathlib.Path("/").resolve().joinpath("fooWorkspace", "asdf")),
1522+
# Use of ~
1523+
("~/foo", pathlib.Path.home().joinpath("foo")),
1524+
# Use of ..
1525+
("/foo/bar/../baz", pathlib.Path("/").resolve().joinpath("foo", "baz"))
1526+
])
15141527
def test_absolute_path(monkeypatch, path, expected_absolute_path):
15151528
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
15161529

1517-
assert compilesketches.absolute_path(path=path) == pathlib.PurePath(expected_absolute_path)
1518-
assert compilesketches.absolute_path(path=pathlib.PurePath(path)) == pathlib.PurePath(expected_absolute_path)
1530+
assert compilesketches.absolute_path(path=path) == expected_absolute_path
1531+
assert compilesketches.absolute_path(path=pathlib.PurePath(path)) == expected_absolute_path
1532+
1533+
1534+
@pytest.mark.parametrize(
1535+
"path, expected_path",
1536+
[("foo/bar-relative-path", pathlib.PurePath("foo/bar-relative-path")),
1537+
("/foo/bar-absolute-path", pathlib.Path("/").resolve().joinpath("foo", "bar-absolute-path"))]
1538+
)
1539+
def test_absolute_relative_path_conversion(monkeypatch, path, expected_path):
1540+
monkeypatch.setenv("GITHUB_WORKSPACE", "/fooWorkspace")
1541+
assert compilesketches.path_relative_to_workspace(
1542+
path=compilesketches.absolute_path(
1543+
path=path
1544+
)
1545+
) == expected_path
15191546

15201547

15211548
def test_list_to_string():

0 commit comments

Comments
 (0)