Skip to content

Commit

Permalink
Merge pull request #297 from machow/fix-restore-chdir
Browse files Browse the repository at this point in the history
fix: restore chdir behavior, add test
  • Loading branch information
machow authored Oct 16, 2023
2 parents f4005ce + 87eba1f commit a37b560
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
44 changes: 23 additions & 21 deletions quartodoc/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,9 @@ def build(config, filter, dry_run, watch, verbose):
"""
Generate API docs based on the given configuration file (`./_quarto.yml` by default).
"""
cfg_path = f"{os.getcwd()}/{config}"
if not Path(cfg_path).exists():

cfg_path = Path(config).absolute()
if not cfg_path.exists():
raise FileNotFoundError(
f"Configuration file {cfg_path} not found. Please create one."
)
Expand All @@ -191,25 +192,26 @@ def build(config, filter, dry_run, watch, verbose):
if dry_run:
pass
else:
if watch:
pkg_path = get_package_path(builder.package)
print(f"Watching {pkg_path} for changes...")
observer = Observer()
observer._event_queue.maxsize = 1 # the default is 0 which is infinite, and there isn't a way to set this in the constructor
event_handler = QuartoDocFileChangeHandler(callback=doc_build)
observer.schedule(event_handler, pkg_path, recursive=True)
observer.schedule(event_handler, cfg_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
observer.stop()
observer.join()
else:
doc_build()
with chdir(Path(config).parent):
if watch:
pkg_path = get_package_path(builder.package)
print(f"Watching {pkg_path} for changes...")
observer = Observer()
observer._event_queue.maxsize = 1 # the default is 0 which is infinite, and there isn't a way to set this in the constructor
event_handler = QuartoDocFileChangeHandler(callback=doc_build)
observer.schedule(event_handler, pkg_path, recursive=True)
observer.schedule(event_handler, cfg_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
observer.stop()
observer.join()
else:
doc_build()


@click.command(
Expand Down
29 changes: 29 additions & 0 deletions quartodoc/tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from pathlib import Path
from quartodoc.__main__ import build


def test_cli_build_config_path(tmpdir):
p_tmp = Path(tmpdir)
p_config = Path(p_tmp, "_quarto.yml")
p_config.write_text(
"""
quartodoc:
package: quartodoc
sections:
- contents:
- get_object
"""
)

# calling click CLI objects directly is super cumbersome ---
try:
build(["--config", str(p_config)])
except SystemExit:
pass

res = list(p_tmp.glob("reference/*"))

assert len(res) == 2
assert "get_object.qmd" in [p.name for p in res]

0 comments on commit a37b560

Please sign in to comment.