This file is indexed.

/usr/lib/python3/dist-packages/zope/testing/setupstack.txt is in python3-zope.testing 4.5.0-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
Stack-based test setUp and tearDown
===================================

Writing doctest setUp and tearDown functions can be a bit tedious,
especially when setUp/tearDown functions are combined.

the zope.testing.setupstack module provides a small framework for
automating test tear down.  It provides a generic setUp function that
sets up a stack. Normal test setUp functions call this function to set
up the stack and then use the register function to register tear-down
functions.

To see how this works we'll create a faux test:

    >>> class Test:
    ...     def __init__(self):
    ...         self.globs = {}
    >>> test = Test()

We'll register some tearDown functions that just print something:

    >>> import sys
    >>> import zope.testing.setupstack
    >>> zope.testing.setupstack.register(
    ...     test, lambda : sys.stdout.write('td 1\n'))
    >>> zope.testing.setupstack.register(
    ...     test, lambda : sys.stdout.write('td 2\n'))

Now, when we call the tearDown function:

    >>> zope.testing.setupstack.tearDown(test)
    td 2
    td 1

The registered tearDown functions are run. Note that they are run in
the reverse order that they were registered.


Extra positional arguments can be passed to register:

    >>> zope.testing.setupstack.register(
    ...    test, lambda x, y, z: sys.stdout.write('%s %s %s\n' % (x, y, z)),
    ...    1, 2, z=9)
    >>> zope.testing.setupstack.tearDown(test)
    1 2 9


Temporary Test Directory
------------------------

Often, tests create files as they demonstrate functionality.  They
need to arrange for the removeal of these files when the test is
cleaned up.

The setUpDirectory function automates this.  We'll get the current
directory first:

    >>> import os
    >>> here = os.getcwd()

We'll also create a new test:

    >>> test = Test()

Now we'll call the setUpDirectory function:

    >>> zope.testing.setupstack.setUpDirectory(test)

We don't have to call zope.testing.setupstack.setUp, because
setUpDirectory calls it for us.

Now the current working directory has changed:

    >>> here == os.getcwd()
    False
    >>> setupstack_cwd = os.getcwd()

We can create files to out heart's content:

    >>> with open('Data.fs', 'w') as f:
    ...     foo = f.write('xxx')
    >>> os.path.exists(os.path.join(setupstack_cwd, 'Data.fs'))
    True

We'll make the file read-only. This can cause problems on Windows, but
setupstack takes care of that by making files writable before trying
to remove them.

    >>> import stat
    >>> os.chmod('Data.fs', stat.S_IREAD)

On Unix systems, broken symlinks can cause problems because the chmod
attempt by the teardown hook will fail; let's set up a broken symlink as
well, and verify the teardown doesn't break because of that:

    >>> if hasattr(os, 'symlink'):
    ...     os.symlink('NotThere', 'BrokenLink')

When tearDown is called:

    >>> zope.testing.setupstack.tearDown(test)

We'll be back where we started:

    >>> here == os.getcwd()
    True

and the files we created will be gone (along with the temporary
directory that was created:

    >>> os.path.exists(os.path.join(setupstack_cwd, 'Data.fs'))
    False

Context-manager support
-----------------------

You can leverage context managers using the ``contextmanager`` method.
The result of calling the content manager's __enter__ method will be
returned. The context-manager's __exit__ method will be called as part
of test tear down:

    >>> class Manager(object):
    ...     def __init__(self, *args, **kw):
    ...         if kw:
    ...             args += (kw, )
    ...         self.args = args
    ...     def __enter__(self):
    ...         print_('enter', *self.args)
    ...         return 42
    ...     def __exit__(self, *args):
    ...         print_('exit', args, *self.args)

    >>> manager = Manager()
    >>> test = Test()

    >>> zope.testing.setupstack.context_manager(test, manager)
    enter
    42

    >>> zope.testing.setupstack.tearDown(test)
    exit (None, None, None)

.. faux mock

    >>> old_mock = sys.modules.get('mock')
    >>> class FauxMock:
    ...     @classmethod
    ...     def patch(self, *args, **kw):
    ...         return Manager(*args, **kw)

    >>> sys.modules['mock'] = FauxMock

By far the most commonly called context manager is ``mock.patch``, so
there's a convenience function to make that simpler:

    >>> zope.testing.setupstack.mock(test, 'time.time', return_value=42)
    enter time.time {'return_value': 42}
    42

    >>> zope.testing.setupstack.tearDown(test)
    exit (None, None, None) time.time {'return_value': 42}

globs
-----

Doctests have ``globs`` attributes used to hold test globals.
``setupstack`` was originally designed to work with doctests, but can
now work with either doctests, or other test objects, as long as the
test objects have either a ``globs`` attribute or a ``__dict__``
attribute.  The ``zope.testing.setupstack.globs`` function is used to
get the globals for a test object:

    >>> zope.testing.setupstack.globs(test) is test.globs
    True

Here, because the test object had a ``globs`` attribute, it was
returned. Because we used the test object above, it has a setupstack:

    >>> '__zope.testing.setupstack' in test.globs
    True

If we remove the ``globs`` attribute, the object's instance dictionary
will be used:

    >>> del test.globs
    >>> zope.testing.setupstack.globs(test) is test.__dict__
    True
    >>> zope.testing.setupstack.context_manager(test, manager)
    enter
    42

    >>> '__zope.testing.setupstack' in test.__dict__
    True

The ``globs`` function is used internally, but can also be used by
setup code to support either doctests or other test objects.

TestCase
--------

A TestCase class is provided that:

- Makes it easier to call setupstack apis, and

- provides an inheritable tearDown method.

In addition to a tearDown method, the class provides methods:

``setupDirectory()``
    Creates a temporary directory, runs the test, and cleans it up.

``register(func)``
    Register a tear-down function.

``context_manager(manager)``
    Enters a context manager and exits it on tearDown.

``mock(*args, **kw)``
    Enters  ``mock.patch`` with the given arguments.

    This is syntactic sugur for::

        context_manager(mock.patch(*args, **kw))

Here's an example:

    >>> open('t', 'w').close()

    >>> class MyTests(zope.testing.setupstack.TestCase):
    ... 
    ...     def setUp(self):
    ...         self.setUpDirectory()
    ...         self.context_manager(manager)
    ...         self.mock("time.time", return_value=42)
    ... 
    ...         @self.register
    ...         def _():
    ...             print('done w test')
    ... 
    ...     def test(self):
    ...         print((os.listdir('.')))

.. let's try it

    >>> import unittest
    >>> loader = unittest.TestLoader()
    >>> suite = loader.loadTestsFromTestCase(MyTests)
    >>> result = suite.run(unittest.TestResult())
    enter
    enter time.time {'return_value': 42}
    []
    done w test
    exit (None, None, None) time.time {'return_value': 42}
    exit (None, None, None)

.. cleanup

    >>> if old_mock:
    ...     sys.modules['mock'] = old_mock
    ... else:
    ...     del sys.modules['mock']
    >>> os.remove('t')