Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions custom_components/pyscript/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
CONFIG_SCHEMA = vol.Schema({DOMAIN: PYSCRIPT_SCHEMA}, extra=vol.ALLOW_EXTRA)


def _relative_script_path(path: str, root: str) -> str:
"""Return a slash-separated path relative to the pyscript root."""
return os.path.relpath(path, root).replace(os.sep, "/")


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Component setup, run import config flow for each entry in config."""
await restore_state(hass)
Expand Down Expand Up @@ -528,11 +533,7 @@ def glob_read_files(
for path, match, check_config, autoload in load_paths:
for this_path in sorted(glob.glob(os.path.join(pyscript_dir, path, match), recursive=True)):
rel_import_path = None
rel_path = this_path
if rel_path.startswith(pyscript_dir):
rel_path = rel_path[len(pyscript_dir) :]
if rel_path.startswith("/"):
rel_path = rel_path[1:]
rel_path = _relative_script_path(this_path, pyscript_dir)
if rel_path[0] == "#" or rel_path.find("/#") >= 0:
# skip "commented" files and directories
continue
Expand Down
15 changes: 15 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
from ast import literal_eval
import asyncio
from datetime import datetime as dt
import ntpath
import pathlib
from unittest.mock import mock_open, patch

import pytest

import custom_components.pyscript as pyscript_component
from custom_components.pyscript import trigger
from custom_components.pyscript.const import DOMAIN
from custom_components.pyscript.event import Event
Expand Down Expand Up @@ -70,6 +72,19 @@ async def wait_until_done(notify_q):
return await asyncio.wait_for(notify_q.get(), timeout=4)


def test_relative_script_path_normalizes_windows_paths(monkeypatch):
"""Script paths should derive portable context names on Windows."""
monkeypatch.setattr(pyscript_component.os.path, "relpath", ntpath.relpath)
monkeypatch.setattr(pyscript_component.os, "sep", "\\")

assert (
pyscript_component._relative_script_path( # pylint: disable=protected-access
r"C:\config\pyscript\hello.py", r"C:\config\pyscript"
)
== "hello.py"
)


@pytest.mark.asyncio
async def test_setup_makedirs_on_no_dir(hass, caplog):
"""Test setup calls os.makedirs when no dir found."""
Expand Down