-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a more advanced method to locate maindir
Added a more advanced method to locate maindir and filename for usage with the python scripts.
- Loading branch information
1 parent
f339ed9
commit 586726c
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
import pytest | ||
from fodt.constants import Directories, FileNames | ||
from fodt.helpers import Helpers | ||
|
||
class TestLocateMainDir: | ||
def test_locate_with_absolute_path_exists(self, tmp_path: Path) -> None: | ||
maindir = tmp_path / Directories.parts | ||
maindir.mkdir() | ||
mainfile = maindir / FileNames.main_document | ||
mainfile.touch() | ||
filename_dir = maindir / Directories.chapters | ||
filename_dir.mkdir() | ||
filename = filename_dir / "1.fodt" | ||
filename.touch() | ||
result_maindir, result_filename = Helpers.locate_maindir_and_filename( | ||
str(maindir), str(filename) | ||
) | ||
assert result_maindir == maindir | ||
assert result_filename == filename | ||
|
||
def test_locate_with_absolute_path_exists_no_main(self, tmp_path: Path) -> None: | ||
maindir = tmp_path / Directories.parts | ||
maindir.mkdir() | ||
mainfile = maindir / FileNames.main_document | ||
# mainfile.touch() # Do not create the main file | ||
filename_dir = maindir / Directories.chapters | ||
filename_dir.mkdir() | ||
filename = filename_dir / "1.fodt" | ||
filename.touch() | ||
with pytest.raises(FileNotFoundError) as excinfo: | ||
Helpers.locate_maindir_and_filename( | ||
str(maindir), str(filename) | ||
) | ||
assert (f"Could not find '{FileNames.main_document}' in a directory " | ||
f"called '{Directories.parts}'" in str(excinfo.value)) | ||
|
||
def test_locate_with_relative_path_in_maindir_exists(self, tmp_path: Path) -> None: | ||
maindir = tmp_path / Directories.parts | ||
maindir.mkdir() | ||
mainfile = maindir / FileNames.main_document | ||
mainfile.touch() # Ensure the main document exists | ||
# Change directory to maindir | ||
os.chdir(str(maindir)) | ||
filename_dir = Path(Directories.appendices) | ||
filename_dir.mkdir() | ||
filename = "A.fodt" | ||
filename_path = filename_dir / filename | ||
filename_path.touch() # Create the file within maindir | ||
filename_abs_path = maindir / filename_path | ||
result_maindir, result_filename = Helpers.locate_maindir_and_filename( | ||
str(maindir), str(filename_path) | ||
) | ||
assert result_maindir == maindir | ||
assert result_filename == filename_abs_path | ||
|
||
def test_locate_with_relative_path_not_in_maindir_but_in_cwd( | ||
self, tmp_path: Path | ||
): | ||
cwd = tmp_path / "cwd" | ||
cwd.mkdir() | ||
os.chdir(str(cwd)) | ||
filename = "1.fodt" | ||
filename_path = cwd / filename | ||
filename_path.touch() # Create the file in CWD | ||
maindir = tmp_path # Some dummy path that is not the maindir | ||
with pytest.raises(FileNotFoundError) as excinfo: | ||
Helpers.locate_maindir_and_filename( | ||
str(maindir), str(filename_path) | ||
) | ||
assert excinfo.match( | ||
f"Could not find '{FileNames.main_document}' in a directory " | ||
f"called '{Directories.parts}' by searching the parent " | ||
f"directories of filename." | ||
) | ||
|
||
def test_locate_with_absolute_path_not_exists(self, tmp_path: Path): | ||
maindir = tmp_path / Directories.parts | ||
maindir.mkdir() | ||
filename = tmp_path / "nonexistent.fodt" | ||
# Do not create the file, simulating a non-existent file scenario | ||
|
||
with pytest.raises(AssertionError): | ||
Helpers.locate_maindir_and_filename( | ||
str(maindir), str(filename) | ||
) |