Skip to content

Commit a37b560

Browse files
authored
Merge pull request #297 from machow/fix-restore-chdir
fix: restore chdir behavior, add test
2 parents f4005ce + 87eba1f commit a37b560

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed

quartodoc/__main__.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ def build(config, filter, dry_run, watch, verbose):
173173
"""
174174
Generate API docs based on the given configuration file (`./_quarto.yml` by default).
175175
"""
176-
cfg_path = f"{os.getcwd()}/{config}"
177-
if not Path(cfg_path).exists():
176+
177+
cfg_path = Path(config).absolute()
178+
if not cfg_path.exists():
178179
raise FileNotFoundError(
179180
f"Configuration file {cfg_path} not found. Please create one."
180181
)
@@ -191,25 +192,26 @@ def build(config, filter, dry_run, watch, verbose):
191192
if dry_run:
192193
pass
193194
else:
194-
if watch:
195-
pkg_path = get_package_path(builder.package)
196-
print(f"Watching {pkg_path} for changes...")
197-
observer = Observer()
198-
observer._event_queue.maxsize = 1 # the default is 0 which is infinite, and there isn't a way to set this in the constructor
199-
event_handler = QuartoDocFileChangeHandler(callback=doc_build)
200-
observer.schedule(event_handler, pkg_path, recursive=True)
201-
observer.schedule(event_handler, cfg_path, recursive=True)
202-
observer.start()
203-
try:
204-
while True:
205-
time.sleep(1)
206-
except KeyboardInterrupt:
207-
pass
208-
finally:
209-
observer.stop()
210-
observer.join()
211-
else:
212-
doc_build()
195+
with chdir(Path(config).parent):
196+
if watch:
197+
pkg_path = get_package_path(builder.package)
198+
print(f"Watching {pkg_path} for changes...")
199+
observer = Observer()
200+
observer._event_queue.maxsize = 1 # the default is 0 which is infinite, and there isn't a way to set this in the constructor
201+
event_handler = QuartoDocFileChangeHandler(callback=doc_build)
202+
observer.schedule(event_handler, pkg_path, recursive=True)
203+
observer.schedule(event_handler, cfg_path, recursive=True)
204+
observer.start()
205+
try:
206+
while True:
207+
time.sleep(1)
208+
except KeyboardInterrupt:
209+
pass
210+
finally:
211+
observer.stop()
212+
observer.join()
213+
else:
214+
doc_build()
213215

214216

215217
@click.command(

quartodoc/tests/test_cli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pytest
2+
3+
from pathlib import Path
4+
from quartodoc.__main__ import build
5+
6+
7+
def test_cli_build_config_path(tmpdir):
8+
p_tmp = Path(tmpdir)
9+
p_config = Path(p_tmp, "_quarto.yml")
10+
p_config.write_text(
11+
"""
12+
quartodoc:
13+
package: quartodoc
14+
sections:
15+
- contents:
16+
- get_object
17+
"""
18+
)
19+
20+
# calling click CLI objects directly is super cumbersome ---
21+
try:
22+
build(["--config", str(p_config)])
23+
except SystemExit:
24+
pass
25+
26+
res = list(p_tmp.glob("reference/*"))
27+
28+
assert len(res) == 2
29+
assert "get_object.qmd" in [p.name for p in res]

0 commit comments

Comments
 (0)