Repository: instructlab/instructlab

Issue:
Error loading temp directory documents after Git clone for knowledge 
I tried generating dataset for knowledge using the below document values in my qna.yaml - 

```
document:
  repo: https://github.com/aartij22/doc_test.git
  commit: 543dd61
  patterns:
    - 'docs/*.md'
```

Running `ilab diff` gives the following error - 
```
knowledge/test/qna.yaml
Traceback (most recent call last):
  File "/Users/aajha/Desktop/cli/cli/utils.py", line 485, in read_taxonomy_file
    documents = get_documents(source=documents, logger=logger)
  File "/Users/aajha/Desktop/cli/cli/utils.py", line 226, in get_documents
    for file_path in glob.glob(os.path.join(working_dir, pattern)):
  File "/Users/aajha/.pyenv/versions/3.9.6/lib/python3.9/posixpath.py", line 76, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not Repo

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/aajha/Desktop/cli/venv/bin/ilab", line 8, in <module>
    sys.exit(cli())
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/core.py", line 1078, in main
    rv = self.invoke(ctx)
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/Users/aajha/Desktop/cli/venv/lib/python3.9/site-packages/click/decorators.py", line 33, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/aajha/Desktop/cli/cli/lab.py", line 254, in diff
    read_taxonomy(logger, taxonomy_path, taxonomy_base, yaml_rules)
  File "/Users/aajha/Desktop/cli/cli/utils.py", line 533, in read_taxonomy
    data, warnings, errors = read_taxonomy_file(logger, file_path, yaml_rules)
  File "/Users/aajha/Desktop/cli/cli/utils.py", line 504, in read_taxonomy_file
    raise TaxonomyReadingException(f"Exception {e} raised in {file_path}") from e
cli.utils.TaxonomyReadingException: Exception expected str, bytes or os.PathLike object, not Repo raised in /Users/aajha/Desktop/cli/taxonomy/knowledge/test/qna.yaml
```

Setting the [working_dir to repo](https://github.com/instruct-lab/cli/blob/3d997d0cd4e262570573b6903a8e741b256d65be/cli/utils.py#L222) in absence of skip_checkout converts the data type from `str` to `git.repo.base.Repo`






Discussion:
@anik120 This may be related to your recent refactor for testing.
Few more things that I noticed - 

- `skip_checkout` will always be set to [False](https://github.com/instruct-lab/cli/blob/3d997d0cd4e262570573b6903a8e741b256d65be/cli/utils.py#L207) even if the argument passed is True
- Converting `working_dir` to `str` solves the above error but does not return any document as the path set is incorrect - `<git.repo.base.Repo '/var/folders/tn/q414j_y108d7jfx9f8214lrh0000gn/T/tmpwf860580/.git'>`.
I have a fix up @bjhargrave @aartij22 PTAL! 🙏🏽 



Patch:
diff --git a/cli/utils.py b/cli/utils.py
index fcae976fc7..f6bea0649c 100644
--- a/cli/utils.py
+++ b/cli/utils.py
@@ -190,7 +190,7 @@ def get_taxonomy_diff(repo="taxonomy", base="origin/main"):
 def get_documents(
     logger,
     source: Dict[str, Union[str, List[str]]],
-    skip_checkout: Optional[bool] = False,
+    skip_checkout: bool = False,
 ) -> List[str]:
     """
     Retrieve the content of files from a Git repository.
@@ -206,9 +206,6 @@ def get_documents(
     file_patterns = source.get("patterns")
     with tempfile.TemporaryDirectory() as temp_dir:
         try:
-            skip_checkout = False
-            if skip_checkout:
-                skip_checkout = True
             repo = git_clone_checkout(
                 repo_url=repo_url,
                 commit_hash=commit_hash,
@@ -217,23 +214,13 @@ def get_documents(
             )
             file_contents = []
 
-            # hack for unit tests. Unit tests use temp dirs in tests/testdata
-            # that needs to be processed instead of the temp_dir created
-            working_dir = temp_dir
-            if not skip_checkout:
-                working_dir = repo
-
             logger.debug("Processing files...")
             for pattern in file_patterns:
-                for file_path in glob.glob(os.path.join(working_dir, pattern)):
+                for file_path in glob.glob(os.path.join(repo.working_dir, pattern)):
                     if os.path.isfile(file_path) and file_path.endswith(".md"):
                         with open(file_path, "r", encoding="utf-8") as file:
                             file_contents.append(file.read())
-            # hack for unit tests. Unit tests use temp dirs in tests/testdata
-            # and patches return string values that are paths to those directories.
-            # str object don't have close() method
-            if not isinstance(repo, str):
-                repo.close()
+
             return file_contents
         except (OSError, exc.GitCommandError, FileNotFoundError) as e:
             raise e


Test patch:
diff --git a/tests/test_utils.py b/tests/test_utils.py
index e152ccfe01..8ccdee9bd5 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -1,12 +1,12 @@
 # SPDX-License-Identifier: Apache-2.0
 
 # Standard
-from unittest.mock import patch
+from unittest.mock import Mock, patch
 import logging
 import unittest
 
 # Third Party
-# Third party
+import git
 import yaml
 
 # First Party
@@ -43,8 +43,11 @@ def test_chunk_docs_chunk_overlap_error(self):
             f"{exc.exception}",
         )
 
-    @patch("cli.utils.git_clone_checkout", return_value="tests/testdata/temp_repo")
-    def test_get_document(self, git_actions):
+    @patch(
+        "cli.utils.git_clone_checkout",
+        return_value=Mock(spec=git.Repo, working_dir="tests/testdata/temp_repo"),
+    )
+    def test_get_document(self, git_clone_checkout):
         with open(
             "tests/testdata/knowledge_valid.yaml", "r", encoding="utf-8"
         ) as qnafile:
@@ -53,5 +56,5 @@ def test_get_document(self, git_actions):
                 skip_checkout=True,
                 logger=logging.getLogger("_test_"),
             )
-            git_actions.assert_called_once()
+            git_clone_checkout.assert_called_once()
             self.assertEqual(len(documents), 2)