Ticket #6137: ticket_6137_2.diff

File ticket_6137_2.diff, 4.1 KB (added by meissenPlate, 4 years ago)

Revised patch adding test + topfile entry.

  • twisted/python/test/test_dist.py

    diff --git twisted/python/test/test_dist.py twisted/python/test/test_dist.py
    index 34c6789..f7c32d2 100644
    Tests for parts of our release automation system. 
    88
    99import os
    1010import sys
     11from collections import Counter
    1112
    1213from distutils.core import Distribution
    1314
    version = versions.Version("twisted.blat", 9, 8, 10) 
    198199
    199200
    200201
     202class GetAllScriptsTest(TestCase):
     203    """
     204    Tests for L{dist.getAllScripts} which lists all the scripts that should be
     205    included in a complete distribution of Twisted, with its subprojects.
     206    """
     207
     208    # getAllScripts relies on a list in twisted.python.dist.twisted_subprojects
     209    # to tell it what subprojects might have scripts it needs to get. It
     210    # searches recursively in bin/, at the root of the Twisted distribution.
     211    # Thus testing it relies on manipulating the contents of that list and
     212    # directory.
     213
     214    def setUp(self):
     215        # A mock up of Twisted's root directory.
     216        basedir = self.mktemp()
     217        os.mkdir(basedir)
     218        os.mkdir(os.path.join(basedir, 'bin'))
     219        # The temporary directories will be deleted by Trial.
     220
     221        # getAllScripts uses a hardcoded relative path.
     222        oldCWD = os.getcwd()
     223        os.chdir(basedir)
     224        self.addCleanup(os.chdir, oldCWD)
     225
     226    def test_getsCorrectScripts(self):
     227        """
     228        L{getAllScripts} lists exactly the scripts in Twisted and its
     229        subprojects that should be included in a distribution. It may encounter
     230        additional scripts, files, or directories, but it will not list them
     231        unless unless they are part of a Twisted subproject (see
     232        twisted.dist.twisted_subprojects).
     233        """
     234        import os
     235        from twisted.python import dist
     236
     237        # Mock up getScripts to record its interactions with getAllScripts. The
     238        # real getScripts is tested separately.
     239        directoriesQueried = list()
     240        scriptsGiven = list()
     241        def mock_getScripts(projname, basedir=''):
     242            directoriesQueried.append(projname)
     243            scripts = ["dummyScript", "dummyScript2"]
     244            scriptsGiven.extend(scripts)
     245            return scripts
     246        self.patch(dist, "getScripts", mock_getScripts)
     247
     248        # Create some dummy subprojects for getAllScripts to look at.
     249        dummySubprojects = ["random", "subproject", "names", "kitty_kat"]
     250        # Also create some more directories that look like subprojects but
     251        # aren't listed as subprojects. getAllScripts should ignore them.
     252        redHerringDirectories = ["some", "directories", "meow"]
     253        # Also create some files that match the names of subprojects.
     254        # getAllScripts should ignore these as well.
     255        redHerringFiles = ["a", "few", "files"]
     256        # Just for kicks: files that do NOT match the name of a subproject.
     257        completelyUnrelatedFiles = ["unrelated", "file"]
     258
     259        self.patch(dist,
     260                   "twisted_subprojects",
     261                   dummySubprojects + redHerringFiles)
     262
     263        for name in dummySubprojects + redHerringDirectories:
     264            os.mkdir(os.path.join("bin", name))
     265        for name in redHerringFiles + completelyUnrelatedFiles:
     266            open(name, "w").close()
     267        # These are in Trial's temp directory, so we don't have to worry about
     268        # deleting them.
     269
     270        scriptsReturned = dist.getAllScripts()
     271
     272        # Check that getAllScripts called getScripts on the right directories.
     273        self.assertEqual(Counter(directoriesQueried),
     274                         Counter(dummySubprojects + ['']))
     275        # Check that getAllScripts aggregated the scripts correctly.
     276        self.assertEqual(Counter(scriptsReturned),
     277                         Counter(scriptsGiven))
     278        # For both lists: order doesn't matter, but multiplicity does.
     279
    201280class GetScriptsTest(TestCase):
    202281    """
    203282    Tests for L{dist.getScripts} which returns the scripts which should be