| 1 |
Testing the ``dirtree`` module |
|---|
| 2 |
============================== |
|---|
| 3 |
|
|---|
| 4 |
The ``dirtree`` module contains functions and classes to scan |
|---|
| 5 |
directory trees recursively. |
|---|
| 6 |
|
|---|
| 7 |
Testing the ``dir_level`` helper function |
|---|
| 8 |
----------------------------------------- |
|---|
| 9 |
|
|---|
| 10 |
The function ``dir_level`` returns the "depth" of a directory. For |
|---|
| 11 |
this it uses the heuristics of counting the path separators in a |
|---|
| 12 |
path. The following tests are intended to run under Posix systems:: |
|---|
| 13 |
|
|---|
| 14 |
>>> import dirtree |
|---|
| 15 |
|
|---|
| 16 |
>>> dirtree.dir_level("/the/root/directory") |
|---|
| 17 |
3 |
|---|
| 18 |
>>> dirtree.dir_level("/dir") |
|---|
| 19 |
1 |
|---|
| 20 |
|
|---|
| 21 |
As a special case, the root directory has a directory level of 0, |
|---|
| 22 |
since it's "above" a directory located in the root directory:: |
|---|
| 23 |
|
|---|
| 24 |
>>> dirtree.dir_level("/") |
|---|
| 25 |
0 |
|---|
| 26 |
|
|---|
| 27 |
Testing the ``DirectoryTree`` class |
|---|
| 28 |
----------------------------------- |
|---|
| 29 |
|
|---|
| 30 |
The class is instantiated with the root of the directory tree to |
|---|
| 31 |
scan. To test the class, use it on a known directory, the |
|---|
| 32 |
Websourcebrowser project directory. Note that the test routine sets |
|---|
| 33 |
the project directory as the current directory, so the following code |
|---|
| 34 |
should work. (To make the test independent of the actual directory, |
|---|
| 35 |
we have to jump through some hoops.) |
|---|
| 36 |
|
|---|
| 37 |
:: |
|---|
| 38 |
|
|---|
| 39 |
>>> import os |
|---|
| 40 |
>>> # assume the current directory holds the project |
|---|
| 41 |
>>> project_dir = os.getcwd() |
|---|
| 42 |
>>> dt = dirtree.DirectoryTree(".") |
|---|
| 43 |
>>> dt.read() |
|---|
| 44 |
>>> for item in dt.items: |
|---|
| 45 |
... print item.replace(project_dir, "pd") #doctest: +ELLIPSIS |
|---|
| 46 |
pd/.hg |
|---|
| 47 |
... |
|---|
| 48 |
pd/graphics/logo.png |
|---|
| 49 |
... |
|---|
| 50 |
pd/browser.py |
|---|
| 51 |
... |
|---|
| 52 |
pd/converter.py |
|---|
| 53 |
... |
|---|
| 54 |
|
|---|
| 55 |
If the root directory isn't accessible, raise a ``ReadError`` |
|---|
| 56 |
exception:: |
|---|
| 57 |
|
|---|
| 58 |
>>> dt = dirtree.DirectoryTree("/root/testdir") |
|---|
| 59 |
>>> dt.read() #doctest: +ELLIPSIS |
|---|
| 60 |
Traceback (most recent call last): |
|---|
| 61 |
... |
|---|
| 62 |
ReadError: root '/root/testdir' can't be scanned |
|---|
| 63 |
|
|---|
| 64 |
If we limit the directory depth (``depth``) to 1, the "lower" |
|---|
| 65 |
directory items shouldn't be present:: |
|---|
| 66 |
|
|---|
| 67 |
>>> dt = dirtree.DirectoryTree(".") |
|---|
| 68 |
>>> dt.read(depth=1) |
|---|
| 69 |
>>> items = [item.replace(project_dir, "pd") for item in dt.items] |
|---|
| 70 |
>>> "pd/browser.py" in items |
|---|
| 71 |
True |
|---|
| 72 |
>>> "pd/graphics" in items |
|---|
| 73 |
True |
|---|
| 74 |
>>> "pd/graphics/logo.png" in items |
|---|
| 75 |
False |
|---|
| 76 |
|
|---|
| 77 |
Upon reading the directory, the ignore patterns from the ``config`` |
|---|
| 78 |
module are applied automatically:: |
|---|
| 79 |
|
|---|
| 80 |
>>> import config |
|---|
| 81 |
>>> config.ignore_patterns = ["*.pyc"] |
|---|
| 82 |
>>> dt = dirtree.DirectoryTree(".") |
|---|
| 83 |
>>> dt.read() |
|---|
| 84 |
>>> items = [item.replace(project_dir, "pd") for item in dt.items] |
|---|
| 85 |
>>> "pd/browser.pyc" in items |
|---|
| 86 |
False |
|---|
| 87 |
|
|---|