Skip to content

Commit

Permalink
Merge pull request #19 from UBC-MDS/ml2_write_docstring
Browse files Browse the repository at this point in the history
Ml2 write docstring to file
  • Loading branch information
Lukman-Lateef authored Jan 17, 2025
2 parents 272adc0 + 0646970 commit d93049c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/fml_doc_gen/write_docstring_to_file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

def write_docstring_to_file(docstring: str, output_file: str) -> None:
def write_docstring_to_file(docstring: str, output_file: str = None) -> None:
"""
Writes the generated docstring to a specified output file.
Expand Down Expand Up @@ -31,4 +32,22 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None:
# This writes the docstring to 'docstring_output.txt'
"""

pass

print(docstring)
print("\n*** End of Docstring ***\n")

if output_file:
output_dir = os.path.dirname(output_file) or "."
if not os.path.exists(output_dir):
raise ValueError(f"This directory '{output_dir}' does not exist.")

if not os.access(output_dir, os.W_OK):
raise ValueError(f"This directory '{output_dir}' is not writable")

try:
with open(output_file, 'w') as file:
file.write(docstring)
except ValueError as e:
print(f"An error occurred while writing to the file: {e}")


62 changes: 62 additions & 0 deletions tests/test_write_docstring_to_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import os
from tempfile import NamedTemporaryFile, TemporaryDirectory
import pytest
from fml_doc_gen.write_docstring_to_file import write_docstring_to_file


@pytest.fixture
def valid_docstring():
"""Fixture for a valid docstring."""
return """
Writes the generated docstring to a specified output file.
Parameters
----------
docstring : str
The docstring to be written to the file.
output_file : str
The path to the output file.
Returns
-------
None
This function does not return anything.
"""


def test_write_to_valid_file(valid_docstring):
"""Test writing the docstring to a valid file"""
with NamedTemporaryFile(suffix=".txt",delete=False) as temp_file:
file_path = temp_file.name

try:
write_docstring_to_file(valid_docstring, output_file=file_path)

with open(file_path, "r") as file:
content = file.read()

assert content.strip() == valid_docstring.strip()
finally:
if os.path.exists(file_path):
os.remove(file_path)


def test_invalid_directory(valid_docstring):
"""Test writing to a file in a non-existent directory."""
with TemporaryDirectory() as temp_dir:
non_existent_dir = os.path.join(temp_dir, "nonexistent")
file_path = os.path.join(non_existent_dir, "docstring_output.txt")
with pytest.raises(ValueError, match=f"This directory '{non_existent_dir}' does not exist"):
write_docstring_to_file(valid_docstring, output_file=file_path)

def test_non_writable_directory(valid_docstring):
"""Test writing to a file in a non-writable directory."""
with TemporaryDirectory() as temp_dir:
# Make the temp_dir directory non writable
os.chmod(temp_dir, 0o500)
file_path = os.path.join(temp_dir, "docstring_output.txt")
try:
with pytest.raises(ValueError, match=f"This directory '{temp_dir}' is not writable"):
write_docstring_to_file(valid_docstring, output_file=file_path)
finally:
os.chmod(temp_dir, 0o700)

0 comments on commit d93049c

Please sign in to comment.