Skip to content

Commit 64683b1

Browse files
authored
Add file validity and existence check to create_file (#827)
…rectory` # Motivation <!-- Why is this change necessary? --> # Content <!-- Please include a summary of the change --> # Testing <!-- How was the change tested? --> # Please check the following before marking your PR as ready for review - [ ] I have added tests for my changes - [ ] I have updated the documentation or added new documentation as needed
1 parent da662c6 commit 64683b1

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/codegen/sdk/core/codebase.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,12 +489,9 @@ def create_file(self, filepath: str, content: str = "", sync: bool = True) -> TS
489489
ValueError: If the provided content cannot be parsed according to the file extension.
490490
"""
491491
# Check if file already exists
492-
# TODO: These checks break parse tests ???
493-
# Look into this!
494-
# if self.has_file(filepath):
495-
# raise ValueError(f"File {filepath} already exists in codebase.")
496-
# if os.path.exists(filepath):
497-
# raise ValueError(f"File {filepath} already exists on disk.")
492+
# NOTE: This check is also important to ensure the filepath is valid within the repo!
493+
if self.has_file(filepath):
494+
logger.warning(f"File {filepath} already exists in codebase. Overwriting...")
498495

499496
file_exts = self.ctx.extensions
500497
# Create file as source file if it has a registered extension
@@ -523,6 +520,11 @@ def create_directory(self, dir_path: str, exist_ok: bool = False, parents: bool
523520
Raises:
524521
FileExistsError: If the directory already exists and exist_ok is False.
525522
"""
523+
# Check if directory already exists
524+
# NOTE: This check is also important to ensure the filepath is valid within the repo!
525+
if self.has_directory(dir_path):
526+
logger.warning(f"Directory {dir_path} already exists in codebase. Overwriting...")
527+
526528
self.ctx.to_absolute(dir_path).mkdir(parents=parents, exist_ok=exist_ok)
527529

528530
def has_file(self, filepath: str, ignore_case: bool = False) -> bool:

0 commit comments

Comments
 (0)