This file is indexed.

/usr/lib/python2.7/dist-packages/twext/internet/test/test_fswatch.py is in python-twext 0.1.b2.dev15059-1.

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
##
# Copyright (c) 2013-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##

"""
Tests for L{twext.internet.fswatch}.
"""

from twext.internet.fswatch import (
    DirectoryChangeListener, patchReactor, IDirectoryChangeListenee
)
from twisted.internet.kqreactor import KQueueReactor
from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase
from zope.interface import implements


class KQueueReactorTestFixture(object):

    def __init__(self, testCase, action=None, timeout=10):
        """
        Creates a kqueue reactor for use in unit tests.  The reactor is patched
        with the vnode event handler.  Once the reactor is running, it will
        call a supplied method.  It's expected that the method will ultimately
        trigger the stop() of the reactor.  The reactor will time out after 10
        seconds.

        @param testCase: a test method which is needed for adding cleanup to
        @param action: a method which will get called after the reactor is
            running
        @param timeout: how many seconds to keep the reactor running before
            giving up and stopping it
        """
        self.testCase = testCase
        self.reactor = KQueueReactor()
        patchReactor(self.reactor)
        self.action = action
        self.timeout = timeout

        def maybeStop():
            if self.reactor.running:
                return self.reactor.stop()

        self.testCase.addCleanup(maybeStop)


    def runReactor(self):
        """
        Run the test reactor, adding cleanup code to stop if after a timeout,
        and calling the action method
        """
        def getReadyToStop():
            self.reactor.callLater(self.timeout, self.reactor.stop)
        self.reactor.callWhenRunning(getReadyToStop)
        if self.action is not None:
            self.reactor.callWhenRunning(self.action)
        self.reactor.run(installSignalHandlers=False)



class DataStoreMonitor(object):
    """
    Stub IDirectoryChangeListenee
    """
    implements(IDirectoryChangeListenee)


    def __init__(self, reactor, storageService):
        """
        @param storageService: the service making use of the DataStore
            directory; we send it a hardStop() to shut it down
        """
        self._reactor = reactor
        self._storageService = storageService
        self.methodCalled = ""


    def disconnected(self):
        self.methodCalled = "disconnected"
        self._storageService.hardStop()
        self._reactor.stop()


    def deleted(self):
        self.methodCalled = "deleted"
        self._storageService.hardStop()
        self._reactor.stop()


    def renamed(self):
        self.methodCalled = "renamed"
        self._storageService.hardStop()
        self._reactor.stop()


    def connectionLost(self, reason):
        pass



class StubStorageService(object):
    """
    Implements hardStop for testing
    """

    def __init__(self, ignored):
        self.stopCalled = False


    def hardStop(self):
        self.stopCalled = True



class DirectoryChangeListenerTestCase(TestCase):

    def test_delete(self):
        """
        Verify directory deletions can be monitored
        """

        self.tmpdir = FilePath(self.mktemp())
        self.tmpdir.makedirs()

        def deleteAction():
            self.tmpdir.remove()

        resource = KQueueReactorTestFixture(self, deleteAction)
        storageService = StubStorageService(resource.reactor)
        delegate = DataStoreMonitor(resource.reactor, storageService)
        dcl = DirectoryChangeListener(resource.reactor, self.tmpdir.path, delegate)
        dcl.startListening()
        resource.runReactor()
        self.assertTrue(storageService.stopCalled)
        self.assertEquals(delegate.methodCalled, "deleted")


    def test_rename(self):
        """
        Verify directory renames can be monitored
        """

        self.tmpdir = FilePath(self.mktemp())
        self.tmpdir.makedirs()

        def renameAction():
            self.tmpdir.moveTo(FilePath(self.mktemp()))

        resource = KQueueReactorTestFixture(self, renameAction)
        storageService = StubStorageService(resource.reactor)
        delegate = DataStoreMonitor(resource.reactor, storageService)
        dcl = DirectoryChangeListener(resource.reactor, self.tmpdir.path, delegate)
        dcl.startListening()
        resource.runReactor()
        self.assertTrue(storageService.stopCalled)
        self.assertEquals(delegate.methodCalled, "renamed")