Skip to content

Commit 5639cf5

Browse files
committed
compare target/source in add_web_module()
also adds remove_web_module()
1 parent fe62305 commit 5639cf5

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/idom/client/manage.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,20 @@ def add_web_module(
6060
source: Union[Path, str],
6161
) -> None:
6262
"""Add a web module from source"""
63-
if web_module_exists(package_name):
64-
raise ValueError(f"Web module {package_name!r} already exists")
65-
source = Path(source)
66-
if not source.exists():
63+
resolved_source = Path(source).resolve()
64+
if not resolved_source.exists():
6765
raise FileNotFoundError(f"Package source file does not exist: {str(source)!r}")
6866
target = web_module_path(package_name)
67+
if target.resolve() == resolved_source:
68+
return None # already added
6969
target.parent.mkdir(parents=True, exist_ok=True)
70-
target.symlink_to(source.absolute())
70+
# this will raise an error if already exists
71+
target.symlink_to(resolved_source)
72+
73+
74+
def remove_web_module(package_name: str, must_exist: bool = False) -> None:
75+
"""Remove a web module"""
76+
web_module_path(package_name, must_exist).unlink()
7177

7278

7379
def restore() -> None:

tests/test_client/test_manage.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from idom.client.manage import (
55
add_web_module,
66
build,
7+
remove_web_module,
78
restore,
89
web_module_exists,
910
web_module_exports,
@@ -31,11 +32,25 @@ def test_add_web_module_source_must_exist(tmp_path):
3132

3233

3334
def test_cannot_add_web_module_if_already_exists(tmp_path):
35+
first_temp_file = tmp_path / "temp-1.js"
36+
second_temp_file = tmp_path / "temp-2.js"
37+
38+
first_temp_file.write_text("console.log('hello!')") # this won't get run
39+
second_temp_file.write_text("console.log('hello!')") # this won't get run
40+
41+
add_web_module("test", first_temp_file)
42+
with pytest.raises(FileExistsError):
43+
add_web_module("test", second_temp_file)
44+
45+
remove_web_module("test")
46+
47+
48+
def test_can_add_web_module_if_already_exists_and_source_is_same(tmp_path):
3449
temp_file = tmp_path / "temp.js"
35-
temp_file.write_text("console.log('hello!')") # this won't get run
50+
temp_file.write_text("console.log('hello!')")
51+
add_web_module("test", temp_file)
3652
add_web_module("test", temp_file)
37-
with pytest.raises(ValueError, match="already exists"):
38-
add_web_module("test", temp_file)
53+
remove_web_module("test")
3954

4055

4156
def test_web_module_path_must_exist():

0 commit comments

Comments
 (0)