Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ml2 write docstring to file #19

Merged
merged 21 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep this, but use raise ValueError instead :)



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)
Loading