Skip to content

Update tests filer#60

Merged
lvarin merged 2 commits intomasterfrom
update-test-filer
Feb 25, 2026
Merged

Update tests filer#60
lvarin merged 2 commits intomasterfrom
update-test-filer

Conversation

@trispera
Copy link
Member

@trispera trispera commented Feb 25, 2026

  • Replace fs by fsspec because of deprecation of pkg_resource starting pyhton>3.12 from Setuptools to fix CI/CD.
  • Update test_filer.py to use fsspec

Summary by Sourcery

Replace the deprecated fs-based test filesystem usage with fsspec and adjust directory tree assertions accordingly.

Build:

  • Update test dependencies to depend on fsspec instead of fs in setup.py.

Tests:

  • Refactor filer tests to build directory trees using fsspec instead of fs and normalize paths for comparison.

- Replace [fs](https://pypi.org/project/fs/) by [fsspec](https://pypi.org/project/fsspec/)
because of deprecation of [pkg_resource](https://setuptools.pypa.io/en/latest/history.html#deprecations-and-removals)
starting pyhton>3.12 from Setuptools to fix CI/CD.
- Update `test_filer.py` to use `fsspec`
@sourcery-ai
Copy link

sourcery-ai bot commented Feb 25, 2026

Reviewer's Guide

Replaces deprecated fs-based filesystem usage in tests with fsspec, updates the directory tree helper and expectations accordingly, and switches the test dependency from fs to fsspec.

Flow diagram for updated fsspec-based test_filer workflow

flowchart TD
    A[Test_run_start] --> B[Initialize_fsspec_filesystem]
    B --> C[Create_test_directory_tree_via_fsspec]
    C --> D[Invoke_filer_code_under_test]
    D --> E[Collect_directory_listing_or_metadata]
    E --> F[Assert_expectations_in_tests]
    F --> G[Test_run_end]
Loading

File-Level Changes

Change Details Files
Replace fs-based directory tree helper in tests with an fsspec-based implementation and adjust assertions to compare normalized tree output.
  • Remove use of fs.opener.open_fs and StringIO-based tree() call in getTree and reimplement getTree using fsspec.core.url_to_fs plus fs.walk to construct a textual directory tree representation.
  • Introduce normalize_tree helper to convert absolute paths returned from getTree into relative, prefix-based paths suitable for stable test assertions.
  • Refactor rmDir into a top-level helper using shutil.rmtree(ignore_errors=True) and reuse it in tests.
  • Update test_copyDir expectations to use normalized_tree output and new expected string format instead of the previous ASCII tree representation and stripLines helper.
tests/test_filer.py
Update test dependencies to use fsspec instead of fs.
  • Replace 'fs' with 'fsspec' in TEST_DEPS so the test suite pulls in the new filesystem abstraction library.
setup.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 3 issues, and left some high level feedback:

  • The new getTree implementation relies on the iteration order from fs.walk but then asserts against a fixed string; to avoid flaky tests, consider sorting dirs and files before writing them to the buffer.
  • The helper functions introduce new dependencies (fsspec.core.url_to_fs, shutil.rmtree) but there are no corresponding imports shown in this diff; ensure required modules are imported so tests remain self-contained.
  • The normalization helper uses the prefix dist1/dist2 while the actual directory variables are dst1/dst2; consider aligning the naming to avoid confusion and potential future mistakes when reading or modifying these tests.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `getTree` implementation relies on the iteration order from `fs.walk` but then asserts against a fixed string; to avoid flaky tests, consider sorting `dirs` and `files` before writing them to the buffer.
- The helper functions introduce new dependencies (`fsspec.core.url_to_fs`, `shutil.rmtree`) but there are no corresponding imports shown in this diff; ensure required modules are imported so tests remain self-contained.
- The normalization helper uses the prefix `dist1`/`dist2` while the actual directory variables are `dst1`/`dst2`; consider aligning the naming to avoid confusion and potential future mistakes when reading or modifying these tests.

## Individual Comments

### Comment 1
<location path="tests/test_filer.py" line_range="31-38" />
<code_context>
+
+    return out.getvalue()
+
+def normalize_tree(tree_str, abs_root, prefix):
+    """Convert absolute paths from getTree into relative paths."""
+    lines = []
+    for line in tree_str.splitlines():
+        stripped = line.replace(abs_root, prefix)
+        stripped = stripped.lstrip("/")
+        lines.append(stripped)
+    return "\n".join(lines)
+
+def rmDir(d):
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Make `normalize_tree` stricter about root replacement to avoid accidental substitutions.

`normalize_tree` currently uses `line.replace(abs_root, prefix)`, which may also modify occurrences of `abs_root` inside filenames or nested paths. To better match the intended behavior, only rewrite when the line actually begins with `abs_root`, e.g.:

```python
if line.startswith(abs_root):
    stripped = prefix + line[len(abs_root):]
else:
    stripped = line
stripped = stripped.lstrip("/")
```

```suggestion
def normalize_tree(tree_str, abs_root, prefix):
    """Convert absolute paths from getTree into relative paths."""
    lines = []
    for line in tree_str.splitlines():
        if line.startswith(abs_root):
            stripped = prefix + line[len(abs_root):]
        else:
            stripped = line
        stripped = stripped.lstrip("/")
        lines.append(stripped)
    return "\n".join(lines)
```
</issue_to_address>

### Comment 2
<location path="tests/test_filer.py" line_range="163-169" />
<code_context>

         # Let's try to copy
         copyDir(src, dst1)
+        tree = getTree(dst1)
+        abs_dst1 = os.path.abspath(dst1)
+        normalizedTree = normalize_tree(tree, abs_dst1, "dist1")
+        
+        expected = "dist1\na/\n3.txt\ndist1/a\n2.txt\n1.txt".strip()

-
-        self.assertEqual(getTree(dst1),
-                          stripLines('''
-                            |-- a
-                            |   |-- 1.txt
-                            |   `-- 2.txt
-                            `-- 3.txt
-                            '''
-                                     )
-                          )
+        self.assertEqual(normalizedTree, expected)

         # Copying to non-existing dst -----------------------------------------
</code_context>
<issue_to_address>
**suggestion (testing):** Revisit the expected tree format to better reflect directory structure and avoid brittle string comparisons.

The new expectation asserts against a flat string, which has two drawbacks:

* It obscures parent/child relationships by mixing root and child paths as plain lines.
* It bakes in the exact traversal order from `fsspec.walk`, making the test brittle to incidental ordering (e.g., `2.txt` before `1.txt`).

To keep the test focused on contents rather than ordering, consider either:
* Converting `normalizedTree` to a structured set of relative paths (e.g., `{"a/1.txt", "a/2.txt", "3.txt"}`) and asserting on that, or
* Comparing sorted lines from `normalizedTree.splitlines()` and `expected.splitlines()` if order is irrelevant.

```suggestion
        tree = getTree(dst1)
        abs_dst1 = os.path.abspath(dst1)
        normalizedTree = normalize_tree(tree, abs_dst1, "dist1")

        expected = "dist1\na/\n3.txt\ndist1/a\n2.txt\n1.txt".strip()

        normalized_lines = sorted(normalizedTree.splitlines())
        expected_lines = sorted(expected.splitlines())

        self.assertEqual(normalized_lines, expected_lines)
```
</issue_to_address>

### Comment 3
<location path="tests/test_filer.py" line_range="171-178" />
<code_context>
-                          )
+        self.assertEqual(normalizedTree, expected)

         # Copying to non-existing dst -----------------------------------------
-        self.assertFalse(os.path.exists(dst2))  # dst2 should not exist
-
-        # Let's try to copy
+        # # Let's try to copy
         copyDir(src, dst2)
+        tree = getTree(dst2)
+        abs_dst2 = os.path.abspath(dst2)
+        normalizedTree = normalize_tree(tree, abs_dst2, "dist2")

-        self.assertEqual(getTree(dst2),
-                          stripLines('''
-                            |-- a
-                            |   |-- 1.txt
-                            |   `-- 2.txt
-                            `-- 3.txt
-                            '''
-                                     )
-                          )
+        expected = "dist2\na/\n3.txt\ndist2/a\n2.txt\n1.txt".strip()
+
+        self.assertEqual(normalizedTree, expected)
</code_context>
<issue_to_address>
**suggestion (testing):** Consider keeping an explicit precondition check that `dst2` does not exist before running `copyDir`.

This check ensures the test is explicitly verifying the "copy to a non-existing destination" scenario. Without it, a pre-existing `dst2` (e.g., created by another test or fixture) could cause the test to pass while exercising a different code path, weakening its value and potentially hiding regressions. Reintroducing the assertion keeps the test’s preconditions clear and robust.

Suggested implementation:

```python
        # Copying to non-existing dst -----------------------------------------
        self.assertFalse(os.path.exists(dst2))  # dst2 should not exist

        # Let's try to copy
        copyDir(src, dst2)
        tree = getTree(dst2)
        abs_dst2 = os.path.abspath(dst2)
        normalizedTree = normalize_tree(tree, abs_dst2, "dist2")

```

If `os` is not already imported at the top of `tests/test_filer.py`, add `import os` to the imports section.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@trispera trispera requested a review from lvarin February 25, 2026 08:44
Copy link
Contributor

@lvarin lvarin left a comment

Choose a reason for hiding this comment

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

lgtm

@lvarin lvarin merged commit 762b89e into master Feb 25, 2026
7 checks passed
@trispera trispera deleted the update-test-filer branch February 25, 2026 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants