Skip to content

Commit bb65404

Browse files
committed
add more tests for web utils
1 parent b082869 commit bb65404

File tree

6 files changed

+48
-31
lines changed

6 files changed

+48
-31
lines changed

src/idom/web/module.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .utils import (
1717
resolve_module_exports_from_file,
1818
resolve_module_exports_from_url,
19+
url_suffix,
1920
web_module_path,
2021
)
2122

@@ -57,8 +58,10 @@ def module_from_template(
5758
resolve_exports_depth: int = 5,
5859
) -> WebModule:
5960
cdn = cdn.rstrip("/")
61+
template_file = (
62+
Path(__file__).parent / "templates" / f"{template}{url_suffix(name)}"
63+
)
6064

61-
template_file = Path(__file__).parent / "templates" / f"{template}.js"
6265
if not template_file.exists():
6366
raise ValueError(f"No template for {template!r} exists")
6467

src/idom/web/templates/preact.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
export * from "$CDN/$PACKAGE";
2-
3-
import { h, Component, render } from "$CDN/preact";
4-
import htm from "$CDN/htm";
5-
6-
const html = htm.bind(h);
1+
import { render } from "$CDN/preact";
72

8-
export { h as createElement, render as renderElement };
3+
export { h as createElement, render as renderElement } from "$CDN/preact";
94

105
export function unmountElement(container) {
11-
preactRender(null, container);
6+
render(null, container);
127
}
8+
9+
export * from "$CDN/$PACKAGE";

src/idom/web/templates/react.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export * from "$CDN/$PACKAGE";
33
import * as react from "$CDN/react";
44
import * as reactDOM from "$CDN/react-dom";
55

6-
export const createElement = (component, props) => react.createElement(component, props);
6+
export const createElement = (component, props) =>
7+
react.createElement(component, props);
78
export const renderElement = reactDOM.render;
89
export const unmountElement = reactDOM.unmountComponentAtNode;

src/idom/web/utils.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import re
3-
from pathlib import Path
3+
from pathlib import Path, PurePosixPath
44
from typing import Set, Tuple
55
from urllib.parse import urlparse
66

@@ -12,9 +12,16 @@
1212
logger = logging.getLogger(__name__)
1313

1414

15+
def url_suffix(name: str) -> str:
16+
head, _, tail = name.partition("@") # handle version identifier
17+
version, _, tail = tail.partition("/") # get section after version
18+
return PurePosixPath(tail or head).suffix or ".js"
19+
20+
1521
def web_module_path(name: str) -> Path:
22+
name += url_suffix(name)
1623
path = IDOM_WED_MODULES_DIR.current.joinpath(*name.split("/"))
17-
return path.with_suffix(path.suffix + ".js")
24+
return path.with_suffix(path.suffix)
1825

1926

2027
def resolve_module_exports_from_file(file: Path, max_depth: int) -> Set[str]:
@@ -31,7 +38,7 @@ def resolve_module_exports_from_file(file: Path, max_depth: int) -> Set[str]:
3138
if urlparse(ref).scheme: # is an absolute URL
3239
export_names.update(resolve_module_exports_from_url(ref, max_depth - 1))
3340
else:
34-
path = _resolve_relative_file_path(file, ref)
41+
path = file.parent.joinpath(*ref.split("/"))
3542
export_names.update(resolve_module_exports_from_file(path, max_depth - 1))
3643

3744
return export_names
@@ -102,23 +109,19 @@ def resolve_module_exports_from_source(content: str) -> Tuple[Set[str], Set[str]
102109
return {n.strip() for n in names}, {r.strip() for r in references}
103110

104111

105-
def _resolve_relative_file_path(base_path: Path, rel_url: str) -> Path:
106-
if rel_url.startswith("./"):
107-
return base_path.parent / rel_url[2:]
108-
while rel_url.startswith("../"):
109-
base_path = base_path.parent
110-
rel_url = rel_url[3:]
111-
return base_path / rel_url
112-
113-
114112
def _resolve_relative_url(base_url: str, rel_url: str) -> str:
115113
if not rel_url.startswith("."):
116114
return rel_url
117-
elif rel_url.startswith("./"):
118-
return base_url.rsplit("/")[0] + rel_url[1:]
115+
116+
base_url = base_url.rsplit("/", 1)[0]
117+
118+
if rel_url.startswith("./"):
119+
return base_url + rel_url[1:]
120+
119121
while rel_url.startswith("../"):
120-
base_url = base_url.rsplit("/")[0]
122+
base_url = base_url.rsplit("/", 1)[0]
121123
rel_url = rel_url[3:]
124+
122125
return f"{base_url}/{rel_url}"
123126

124127

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export {one as One};
2-
export * from "./two.js";
2+
// use ../ just to check that it works
3+
export * from "../export-resolution/two.js";

tests/test_web/test_utils.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,30 @@ def test_resolve_module_exports_from_file_log_on_unknown_file_location(
5858
def test_resolve_module_exports_from_url():
5959
responses.add(
6060
responses.GET,
61-
"https://first.url",
62-
body="export const First = 1; export * from 'https://second.url';",
61+
"https://some.url/first.js",
62+
body="export const First = 1; export * from 'https://another.url/path/second.js';",
6363
)
6464
responses.add(
6565
responses.GET,
66-
"https://second.url",
67-
body="export const Second = 2;",
66+
"https://another.url/path/second.js",
67+
body="export const Second = 2; export * from '../third.js';",
68+
)
69+
responses.add(
70+
responses.GET,
71+
"https://another.url/third.js",
72+
body="export const Third = 3; export * from './fourth.js';",
73+
)
74+
responses.add(
75+
responses.GET,
76+
"https://another.url/fourth.js",
77+
body="export const Fourth = 4;",
6878
)
6979

70-
assert resolve_module_exports_from_url("https://first.url", 2) == {
80+
assert resolve_module_exports_from_url("https://some.url/first.js", 4) == {
7181
"First",
7282
"Second",
83+
"Third",
84+
"Fourth",
7385
}
7486

7587

0 commit comments

Comments
 (0)