GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Tests for `pathlib.Path.walk()` are a little fragile · Issue #103517 · python/cpython

Tests for pathlib.Path.walk() are a little fragile #103517

New issue
Copy link
New issue
Copy link
Closed
Closed
Tests for pathlib.Path.walk() are a little fragile#103517
Copy link
Labels
testsTests in the Lib/test dirTests in the Lib/test dirtopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@barneygale
barneygale
opened on Apr 13, 2023
Issue body actions

test_walk_topdown() attempts to handle all possible visitation orders, but the result is difficult to read. We can enforce a specific order by sorting dirnames.

cpython/Lib/test/test_pathlib.py

Lines 2680 to 2694 in a6f9594

def test_walk_topdown(self):
all = list(self.walk_path.walk())
self.assertEqual(len(all), 4)
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: TESTFN, SUB1, SUB11, SUB2
# flipped: TESTFN, SUB2, SUB1, SUB11
flipped = all[0][1][0] != "SUB1"
all[0][1].sort()
all[3 - 2 * flipped][-1].sort()
all[3 - 2 * flipped][1].sort()
self.assertEqual(all[0], (self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
self.assertEqual(all[1 + flipped], (self.sub1_path, ["SUB11"], ["tmp2"]))
self.assertEqual(all[2 + flipped], (self.sub11_path, [], []))
self.assertEqual(all[3 - 2 * flipped], self.sub2_tree)

test_walk_bottom_up() suffers similar problems, and also makes unjustified assertions about the order that siblings are visited (which is arbitrary and cannot be influenced by the user, contrary to top-down mode). It can be simplified to ensure that children are yielded before parents.

cpython/Lib/test/test_pathlib.py

Lines 2717 to 2735 in a6f9594

def test_walk_bottom_up(self):
all = list(self.walk_path.walk( top_down=False))
self.assertEqual(len(all), 4, all)
# We can't know which order SUB1 and SUB2 will appear in.
# Not flipped: SUB11, SUB1, SUB2, TESTFN
# flipped: SUB2, SUB11, SUB1, TESTFN
flipped = all[3][1][0] != "SUB1"
all[3][1].sort()
all[2 - 2 * flipped][-1].sort()
all[2 - 2 * flipped][1].sort()
self.assertEqual(all[3],
(self.walk_path, ["SUB1", "SUB2"], ["tmp1"]))
self.assertEqual(all[flipped],
(self.sub11_path, [], []))
self.assertEqual(all[flipped + 1],
(self.sub1_path, ["SUB11"], ["tmp2"]))
self.assertEqual(all[2 - 2 * flipped],
self.sub2_tree)

Linked PRs

  • GH-103517: Improve tests for pathlib.Path.walk() #103518

Activity

  1. barneygale
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    testsTests in the Lib/test dirTests in the Lib/test dir
    topic-pathlib
    on Apr 13, 2023
  2. barneygale
    added 2 commits that reference this issue on Apr 13, 2023

    pythonGH-103517: Improve tests for `pathlib.Path.walk()`

    24a488d

    GH-103517: Improve tests for `pathlib.Path.walk()` (GH-103518)

    0097c36
  3. carljm
    added 2 commits that reference this issue on Apr 17, 2023

    Merge branch 'main' into superopt

    5136459

    Merge branch 'superopt' into superopt_spec

    079c65a
  4. barneygale commented on Apr 20, 2023

    @barneygale
    barneygale
    on Apr 20, 2023
    ContributorAuthor
    More actions

    Resolved by #103518.

  5. barneygale
    closed this as completedon Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirTests in the Lib/test dirtopic-pathlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.