Back to index

python3.2  3.2.2
Public Member Functions
test.test_os.WalkTests Class Reference

List of all members.

Public Member Functions

def test_traversal
def tearDown

Detailed Description

Tests for os.walk().

Definition at line 426 of file test_os.py.


Member Function Documentation

Definition at line 518 of file test_os.py.

00518 
00519     def tearDown(self):
00520         # Tear everything down.  This is a decent use for bottom-up on
00521         # Windows, which doesn't have a recursive delete command.  The
00522         # (not so) subtlety is that rmdir will fail unless the dir's
00523         # kids are removed first, so bottom up is essential.
00524         for root, dirs, files in os.walk(support.TESTFN, topdown=False):
00525             for name in files:
00526                 os.remove(os.path.join(root, name))
00527             for name in dirs:
00528                 dirname = os.path.join(root, name)
00529                 if not os.path.islink(dirname):
00530                     os.rmdir(dirname)
00531                 else:
00532                     os.remove(dirname)
00533         os.rmdir(support.TESTFN)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 429 of file test_os.py.

00429 
00430     def test_traversal(self):
00431         import os
00432         from os.path import join
00433 
00434         # Build:
00435         #     TESTFN/
00436         #       TEST1/              a file kid and two directory kids
00437         #         tmp1
00438         #         SUB1/             a file kid and a directory kid
00439         #           tmp2
00440         #           SUB11/          no kids
00441         #         SUB2/             a file kid and a dirsymlink kid
00442         #           tmp3
00443         #           link/           a symlink to TESTFN.2
00444         #       TEST2/
00445         #         tmp4              a lone file
00446         walk_path = join(support.TESTFN, "TEST1")
00447         sub1_path = join(walk_path, "SUB1")
00448         sub11_path = join(sub1_path, "SUB11")
00449         sub2_path = join(walk_path, "SUB2")
00450         tmp1_path = join(walk_path, "tmp1")
00451         tmp2_path = join(sub1_path, "tmp2")
00452         tmp3_path = join(sub2_path, "tmp3")
00453         link_path = join(sub2_path, "link")
00454         t2_path = join(support.TESTFN, "TEST2")
00455         tmp4_path = join(support.TESTFN, "TEST2", "tmp4")
00456 
00457         # Create stuff.
00458         os.makedirs(sub11_path)
00459         os.makedirs(sub2_path)
00460         os.makedirs(t2_path)
00461         for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path:
00462             f = open(path, "w")
00463             f.write("I'm " + path + " and proud of it.  Blame test_os.\n")
00464             f.close()
00465         if support.can_symlink():
00466             os.symlink(os.path.abspath(t2_path), link_path)
00467             sub2_tree = (sub2_path, ["link"], ["tmp3"])
00468         else:
00469             sub2_tree = (sub2_path, [], ["tmp3"])
00470 
00471         # Walk top-down.
00472         all = list(os.walk(walk_path))
00473         self.assertEqual(len(all), 4)
00474         # We can't know which order SUB1 and SUB2 will appear in.
00475         # Not flipped:  TESTFN, SUB1, SUB11, SUB2
00476         #     flipped:  TESTFN, SUB2, SUB1, SUB11
00477         flipped = all[0][1][0] != "SUB1"
00478         all[0][1].sort()
00479         self.assertEqual(all[0], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
00480         self.assertEqual(all[1 + flipped], (sub1_path, ["SUB11"], ["tmp2"]))
00481         self.assertEqual(all[2 + flipped], (sub11_path, [], []))
00482         self.assertEqual(all[3 - 2 * flipped], sub2_tree)
00483 
00484         # Prune the search.
00485         all = []
00486         for root, dirs, files in os.walk(walk_path):
00487             all.append((root, dirs, files))
00488             # Don't descend into SUB1.
00489             if 'SUB1' in dirs:
00490                 # Note that this also mutates the dirs we appended to all!
00491                 dirs.remove('SUB1')
00492         self.assertEqual(len(all), 2)
00493         self.assertEqual(all[0], (walk_path, ["SUB2"], ["tmp1"]))
00494         self.assertEqual(all[1], sub2_tree)
00495 
00496         # Walk bottom-up.
00497         all = list(os.walk(walk_path, topdown=False))
00498         self.assertEqual(len(all), 4)
00499         # We can't know which order SUB1 and SUB2 will appear in.
00500         # Not flipped:  SUB11, SUB1, SUB2, TESTFN
00501         #     flipped:  SUB2, SUB11, SUB1, TESTFN
00502         flipped = all[3][1][0] != "SUB1"
00503         all[3][1].sort()
00504         self.assertEqual(all[3], (walk_path, ["SUB1", "SUB2"], ["tmp1"]))
00505         self.assertEqual(all[flipped], (sub11_path, [], []))
00506         self.assertEqual(all[flipped + 1], (sub1_path, ["SUB11"], ["tmp2"]))
00507         self.assertEqual(all[2 - 2 * flipped], sub2_tree)
00508 
00509         if support.can_symlink():
00510             # Walk, following symlinks.
00511             for root, dirs, files in os.walk(walk_path, followlinks=True):
00512                 if root == link_path:
00513                     self.assertEqual(dirs, [])
00514                     self.assertEqual(files, ["tmp4"])
00515                     break
00516             else:
00517                 self.fail("Didn't follow symlink with followlinks=True")

Here is the call graph for this function:


The documentation for this class was generated from the following file: