Skip to content

Commit

Permalink
Make external local paths relative to project dir
Browse files Browse the repository at this point in the history
  • Loading branch information
haraldkl committed Nov 17, 2024
1 parent b74475e commit 6fe3fb2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion ford/external_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ def load_external_modules(project):
urlopen(urljoin(url, "modules.json")).read().decode("utf8")
)
else:
url = pathlib.Path(url).resolve()
if not pathlib.Path(url).is_absolute():
url = project.settings.directory.joinpath(url).resolve()
extModules = modules_from_local(url)
except (URLError, json.JSONDecodeError) as error:
extModules = []
Expand Down
9 changes: 5 additions & 4 deletions ford/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class ProjectSettings:
creation_date: str = "%Y-%m-%dT%H:%M:%S.%f%z"
css: Optional[Path] = None
dbg: bool = True
directory: Path = Path.cwd()
display: List[str] = field(default_factory=lambda: ["public", "protected"])
doc_license: str = ""
docmark: str = "!"
Expand Down Expand Up @@ -255,14 +256,14 @@ def from_markdown_metadata(cls, meta: Dict[str, Any], parent: Optional[str] = No
def normalise_paths(self, directory=None):
if directory is None:
directory = Path.cwd()
directory = Path(directory).absolute()
self.directory = Path(directory).absolute()
field_types = get_type_hints(self)

if self.favicon == FAVICON_PATH:
self.favicon = Path(__file__).parent / FAVICON_PATH

if self.md_base_dir == Path("."):
self.md_base_dir = directory
self.md_base_dir = self.directory

for key, value in asdict(self).items():
if value is None:
Expand All @@ -272,10 +273,10 @@ def normalise_paths(self, directory=None):

if is_same_type(default_type, List[Path]):
value = getattr(self, key)
setattr(self, key, [normalise_path(directory, v) for v in value])
setattr(self, key, [normalise_path(self.directory, v) for v in value])

if is_same_type(default_type, Path):
setattr(self, key, normalise_path(directory, value))
setattr(self, key, normalise_path(self.directory, value))

if self.relative:
self.project_url = self.output_dir
Expand Down
12 changes: 8 additions & 4 deletions test/test_projects/test_676.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,21 @@ def external_project(tmp_path_factory, monkeymodule):
external_project = path / "base"
top_level_project = path / "plugin"

# Generate the individual projects from their common parent
# directory, to check that local path definitions are
# relative to the project directory, irrespective of the
# working directory.
os.chdir(path)

# Run FORD in the two projects
# First project has "externalize: True" and will generate JSON dump
with monkeymodule.context() as m:
os.chdir(external_project)
m.setattr(sys, "argv", ["ford", "doc.md"])
m.setattr(sys, "argv", ["ford", "base/doc.md"])
ford.run()

# Second project uses JSON from first to link to external modules
with monkeymodule.context() as m:
os.chdir(top_level_project)
m.setattr(sys, "argv", ["ford", "doc.md"])
m.setattr(sys, "argv", ["ford", "plugin/doc.md"])
ford.run()

# Make sure we're in a directory where relative paths won't
Expand Down

0 comments on commit 6fe3fb2

Please sign in to comment.