Skip to content

Commit

Permalink
Add get_file() to Namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
basicthinker committed Aug 27, 2023
1 parent 043bb72 commit 8e84408
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
21 changes: 21 additions & 0 deletions devchat/engine/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,27 @@ def is_valid_name(name: str) -> bool:
pattern = r'^$|^(?!.*\.\.)[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*$'
return bool(re.match(pattern, name))

def get_file(self, name: str, file: str) -> Optional[str]:
"""
:param name: The command name in the namespace.
:param file: The target file name.
:return: The full path of the target file in the command directory.
"""
if not self.is_valid_name(name):
return None
# Convert the dot-separated name to a path
path = os.path.join(*name.split('.'))
for branch in reversed(self.branches):
full_path = os.path.join(self.root_path, branch, path)
if os.path.isdir(full_path):
# If it exists and is a directory, check for the file
file_path = os.path.join(full_path, file)
if os.path.isfile(file_path):
# If the file exists, return its path
return file_path
# If no file is found, return None
return None

def list_files(self, name: str) -> Optional[List[str]]:
"""
:param name: The command name in the namespace.
Expand Down
39 changes: 38 additions & 1 deletion tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,44 @@ def test_is_valid_name():
assert Namespace.is_valid_name('a>b') is False


def test_get_files(tmp_path):
def test_get_file(tmp_path):
# Create a Namespace instance with the temporary directory as the root path
namespace = Namespace(str(tmp_path))

# Test case 1: a file that exists
# Create a file in the 'usr' branch
os.makedirs(os.path.join(tmp_path, 'usr', 'a', 'b', 'c'), exist_ok=True)
file_path = os.path.join(tmp_path, 'usr', 'a', 'b', 'c', 'file1.txt')
with open(file_path, 'w', encoding='utf-8') as file:
file.write('test')
assert namespace.get_file('a.b.c', 'file1.txt') == file_path

# Test case 2: a file that doesn't exist
assert namespace.get_file('d.e.f', 'file2.txt') is None

# Test case 3: a file that exists in a later branch
# Create a file in the 'sys' branch
os.makedirs(os.path.join(tmp_path, 'usr', 'g', 'h', 'i'), exist_ok=True)
os.makedirs(os.path.join(tmp_path, 'sys', 'g', 'h', 'i'), exist_ok=True)
file_path = os.path.join(tmp_path, 'sys', 'g', 'h', 'i', 'file3.txt')
with open(file_path, 'w', encoding='utf-8') as file:
file.write('test')
assert namespace.get_file('g.h.i', 'file3.txt') == file_path

# Test case 4: a file in 'usr' overwrites the same in 'sys'
# Create the same file in the 'usr' and 'sys' branches
os.makedirs(os.path.join(tmp_path, 'usr', 'j', 'k', 'l'), exist_ok=True)
usr_file_path = os.path.join(tmp_path, 'usr', 'j', 'k', 'l', 'file4.txt')
os.makedirs(os.path.join(tmp_path, 'sys', 'j', 'k', 'l'), exist_ok=True)
sys_file_path = os.path.join(tmp_path, 'sys', 'j', 'k', 'l', 'file4.txt')
with open(usr_file_path, 'w', encoding='utf-8') as file:
file.write('test')
with open(sys_file_path, 'w', encoding='utf-8') as file:
file.write('test')
assert namespace.get_file('j.k.l', 'file4.txt') == usr_file_path


def test_list_files(tmp_path):
# Create a Namespace instance with the temporary directory as the root path
namespace = Namespace(str(tmp_path))

Expand Down

0 comments on commit 8e84408

Please sign in to comment.