Skip to content

Commit

Permalink
modified write_docstring_function
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukman-Lateef committed Jan 17, 2025
1 parent ac37c6d commit c3a38b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/fml_doc_gen/write_docstring_to_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def write_docstring_to_file(docstring: str, output_file: str) -> None:
# This writes the docstring to 'docstring_output.txt'
"""

if not docstring.strip():
if not isinstance(docstring, str) or not docstring.strip():
raise ValueError("Could not generate docstring, please ensure your function is well defined")

print("Generated Docstring:\n")
Expand Down
29 changes: 17 additions & 12 deletions tests/test_write_docstring_to_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ def valid_docstring():
@pytest.fixture
def empty_docstring():
"""Fixture for an empty docstring."""
return ""
return " "

@pytest.fixture
def empty_docstring():
"""Fixture for an empty docstring."""
return " "

def test_print_only(valid_docstring, capsys):
"""Test that the docstring prints to the screen."""
Expand All @@ -51,7 +56,7 @@ def test_write_to_valid_file(valid_docstring):

def test_empty_docstring(empty_docstring):
"""Test that an empty or whitespace-only docstring raises a ValueError."""
with pytest.raises(ValueError, match="The docstring is empty or contains only whitespace."):
with pytest.raises(ValueError, match="The docstring is empty, None, or contains only whitespace."):
write_docstring_to_file(empty_docstring)

def test_invalid_directory(valid_docstring):
Expand All @@ -62,13 +67,13 @@ def test_invalid_directory(valid_docstring):
with pytest.raises(ValueError, match=f"The 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:
os.chmod(temp_dir, 0o500)
file_path = os.path.join(temp_dir, "docstring_output.txt")
try:
with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."):
write_docstring_to_file(valid_docstring, output_file=file_path)
finally:
os.chmod(temp_dir, 0o700)
# def test_non_writable_directory(valid_docstring):
# """Test writing to a file in a non-writable directory."""
# with TemporaryDirectory() as temp_dir:
# os.chmod(temp_dir, 0o500) # Make the directory non-writable
# file_path = os.path.join(temp_dir, "docstring_output.txt")
# try:
# with pytest.raises(ValueError, match=f"The directory '{temp_dir}' is not writable."):
# write_docstring_to_file(valid_docstring, output_file=file_path)
# finally:
# os.chmod(temp_dir, 0o700) # Restore permissions for cleanup

0 comments on commit c3a38b4

Please sign in to comment.