Skip to content

Commit d8fc315

Browse files
committed
www: make www_folder behavior testable
1 parent 23ee908 commit d8fc315

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

chatmaild/src/chatmaild/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self, inipath, params):
3333
self.password_min_length = int(params["password_min_length"])
3434
self.passthrough_senders = params["passthrough_senders"].split()
3535
self.passthrough_recipients = params["passthrough_recipients"].split()
36-
self.www_folder = params.get("www_folder")
36+
self.www_folder = params.get("www_folder", "")
3737
self.filtermail_smtp_port = int(params["filtermail_smtp_port"])
3838
self.filtermail_smtp_port_incoming = int(
3939
params["filtermail_smtp_port_incoming"]

cmdeploy/src/cmdeploy/__init__.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None:
624624
check_config(config)
625625
mail_domain = config.mail_domain
626626

627-
from .www import build_webpages
627+
from .www import build_webpages, get_paths
628628

629629
server.group(name="Create vmail group", group="vmail", system=True)
630630
server.user(name="Create vmail user", user="vmail", group="vmail", system=True)
@@ -737,24 +737,16 @@ def deploy_chatmail(config_path: Path, disable_mail: bool) -> None:
737737
packages=["fcgiwrap"],
738738
)
739739

740-
reporoot = importlib.resources.files(__package__).joinpath("../../../").resolve()
741-
www_path = Path(config.www_folder)
742-
# if www_folder was not set, use default directory
743-
if not config.www_folder:
744-
www_path = reporoot.joinpath("www")
740+
www_path, src_dir, build_dir = get_paths(config)
745741
# if www_folder was set to a non-existing folder, skip upload
746742
if not www_path.is_dir():
747743
logger.warning("Building web pages is disabled in chatmail.ini, skipping")
748744
else:
749-
build_dir = www_path.joinpath("build")
750-
src_dir = www_path.joinpath("src")
751745
# if www_folder is a hugo page, build it
752-
if src_dir.joinpath("index.md").is_file():
753-
build_webpages(src_dir, build_dir, config)
746+
if build_dir:
747+
www_path = build_webpages(src_dir, build_dir, config)
754748
# if it is not a hugo page, upload it as is
755-
else:
756-
build_dir = www_path
757-
files.rsync(f"{build_dir}/", "/var/www/html", flags=["-avz"])
749+
files.rsync(f"{www_path}/", "/var/www/html", flags=["-avz"])
758750

759751
_install_remote_venv_with_chatmaild(config)
760752
debug = False

cmdeploy/src/cmdeploy/tests/test_cmdeploy.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import importlib
12
import os
23

34
import pytest
45

56
from cmdeploy.cmdeploy import get_parser, main
7+
from cmdeploy.www import get_paths
68

79

810
@pytest.fixture(autouse=True)
@@ -27,3 +29,28 @@ def test_init_not_overwrite(self, capsys):
2729
assert main(["init", "chat.example.org"]) == 1
2830
out, err = capsys.readouterr()
2931
assert "path exists" in out.lower()
32+
33+
34+
def test_www_folder(example_config, tmp_path):
35+
reporoot = importlib.resources.files(__package__).joinpath("../../../../").resolve()
36+
assert not example_config.www_folder
37+
www_path, src_dir, build_dir = get_paths(example_config)
38+
assert www_path.absolute() == reporoot.joinpath("www").absolute()
39+
assert src_dir == reporoot.joinpath("www").joinpath("src")
40+
assert build_dir == reporoot.joinpath("www").joinpath("build")
41+
example_config.www_folder = "disabled"
42+
www_path, _, _ = get_paths(example_config)
43+
assert not www_path.is_dir()
44+
example_config.www_folder = str(tmp_path)
45+
www_path, src_dir, build_dir = get_paths(example_config)
46+
assert www_path == tmp_path
47+
assert not src_dir.exists()
48+
assert not build_dir
49+
src_path = tmp_path.joinpath("src")
50+
os.mkdir(src_path)
51+
with open(src_path / "index.md", "w") as f:
52+
f.write("# Test")
53+
www_path, src_dir, build_dir = get_paths(example_config)
54+
assert www_path == tmp_path
55+
assert src_dir == src_path
56+
assert build_dir == tmp_path.joinpath("build")

cmdeploy/src/cmdeploy/www.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,25 @@ def prepare_template(source):
3131
return render_vars, page_layout
3232

3333

34-
def build_webpages(src_dir, build_dir, config):
34+
def get_paths(config) -> (Path, Path, Path):
35+
reporoot = importlib.resources.files(__package__).joinpath("../../../").resolve()
36+
www_path = Path(config.www_folder)
37+
# if www_folder was not set, use default directory
38+
if config.www_folder == "":
39+
www_path = reporoot.joinpath("www")
40+
src_dir = www_path.joinpath("src")
41+
# if www_folder is a hugo page, build it
42+
if src_dir.joinpath("index.md").is_file():
43+
build_dir = www_path.joinpath("build")
44+
# if it is not a hugo page, upload it as is
45+
else:
46+
build_dir = None
47+
return www_path, src_dir, build_dir
48+
49+
50+
def build_webpages(src_dir, build_dir, config) -> Path:
3551
try:
36-
_build_webpages(src_dir, build_dir, config)
52+
return _build_webpages(src_dir, build_dir, config)
3753
except Exception:
3854
print(traceback.format_exc())
3955

@@ -107,17 +123,11 @@ def main():
107123
config = read_config(inipath)
108124
config.webdev = True
109125
assert config.mail_domain
110-
www_path = Path(config.www_folder)
111-
if not config.www_folder:
112-
www_path = reporoot.joinpath("www")
113-
src_path = www_path.joinpath("src")
114-
stats = None
115-
build_dir = www_path.joinpath("build")
116-
src_dir = www_path.joinpath("src")
117-
index_path = build_dir.joinpath("index.html")
118126

119127
# start web page generation, open a browser and wait for changes
120-
build_webpages(src_dir, build_dir, config)
128+
www_path, src_path, build_dir = get_paths(config)
129+
build_dir = build_webpages(src_path, build_dir, config)
130+
index_path = build_dir.joinpath("index.html")
121131
webbrowser.open(str(index_path))
122132
stats = snapshot_dir_stats(src_path)
123133
print(f"\nOpened URL: file://{index_path.resolve()}\n")
@@ -138,7 +148,7 @@ def main():
138148
changenum += 1
139149

140150
stats = newstats
141-
build_webpages(src_dir, build_dir, config)
151+
build_webpages(src_path, build_dir, config)
142152
print(f"[{changenum}] regenerated web pages at: {index_path}")
143153
print(f"URL: file://{index_path.resolve()}\n\n")
144154
count = 0

0 commit comments

Comments
 (0)