/vendor/packages/twisted/twisted/python/test/test_dist.py

https://github.com/openhatch/oh-mainline · Python · 173 lines · 109 code · 23 blank · 41 comment · 0 complexity · a9b09a61fafb50865a0f32f3beeb2e03 MD5 · raw file

  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for parts of our release automation system.
  5. """
  6. import os
  7. from distutils.core import Distribution
  8. from twisted.trial.unittest import TestCase
  9. from twisted.python import dist
  10. from twisted.python.dist import get_setup_args, ConditionalExtension
  11. from twisted.python.filepath import FilePath
  12. class SetupTest(TestCase):
  13. """
  14. Tests for L{get_setup_args}.
  15. """
  16. def test_conditionalExtensions(self):
  17. """
  18. Passing C{conditionalExtensions} as a list of L{ConditionalExtension}
  19. objects to get_setup_args inserts a custom build_ext into the result
  20. which knows how to check whether they should be
  21. """
  22. good_ext = ConditionalExtension("whatever", ["whatever.c"],
  23. condition=lambda b: True)
  24. bad_ext = ConditionalExtension("whatever", ["whatever.c"],
  25. condition=lambda b: False)
  26. args = get_setup_args(conditionalExtensions=[good_ext, bad_ext])
  27. # ext_modules should be set even though it's not used. See comment
  28. # in get_setup_args
  29. self.assertEquals(args["ext_modules"], [good_ext, bad_ext])
  30. cmdclass = args["cmdclass"]
  31. build_ext = cmdclass["build_ext"]
  32. builder = build_ext(Distribution())
  33. builder.prepare_extensions()
  34. self.assertEquals(builder.extensions, [good_ext])
  35. def test_win32Definition(self):
  36. """
  37. When building on Windows NT, the WIN32 macro will be defined as 1.
  38. """
  39. ext = ConditionalExtension("whatever", ["whatever.c"],
  40. define_macros=[("whatever", 2)])
  41. args = get_setup_args(conditionalExtensions=[ext])
  42. builder = args["cmdclass"]["build_ext"](Distribution())
  43. self.patch(os, "name", "nt")
  44. builder.prepare_extensions()
  45. self.assertEquals(ext.define_macros, [("whatever", 2), ("WIN32", 1)])
  46. class GetVersionTest(TestCase):
  47. """
  48. Tests for L{dist.getVersion}.
  49. """
  50. def setUp(self):
  51. self.dirname = self.mktemp()
  52. os.mkdir(self.dirname)
  53. def test_getVersionCore(self):
  54. """
  55. Test that getting the version of core reads from the
  56. [base]/_version.py file.
  57. """
  58. f = open(os.path.join(self.dirname, "_version.py"), "w")
  59. f.write("""
  60. from twisted.python import versions
  61. version = versions.Version("twisted", 0, 1, 2)
  62. """)
  63. f.close()
  64. self.assertEquals(dist.getVersion("core", base=self.dirname), "0.1.2")
  65. def test_getVersionOther(self):
  66. """
  67. Test that getting the version of a non-core project reads from
  68. the [base]/[projname]/_version.py file.
  69. """
  70. os.mkdir(os.path.join(self.dirname, "blat"))
  71. f = open(os.path.join(self.dirname, "blat", "_version.py"), "w")
  72. f.write("""
  73. from twisted.python import versions
  74. version = versions.Version("twisted.blat", 9, 8, 10)
  75. """)
  76. f.close()
  77. self.assertEquals(dist.getVersion("blat", base=self.dirname), "9.8.10")
  78. class GetScriptsTest(TestCase):
  79. def test_scriptsInSVN(self):
  80. """
  81. getScripts should return the scripts associated with a project
  82. in the context of Twisted SVN.
  83. """
  84. basedir = self.mktemp()
  85. os.mkdir(basedir)
  86. os.mkdir(os.path.join(basedir, 'bin'))
  87. os.mkdir(os.path.join(basedir, 'bin', 'proj'))
  88. f = open(os.path.join(basedir, 'bin', 'proj', 'exy'), 'w')
  89. f.write('yay')
  90. f.close()
  91. scripts = dist.getScripts('proj', basedir=basedir)
  92. self.assertEquals(len(scripts), 1)
  93. self.assertEquals(os.path.basename(scripts[0]), 'exy')
  94. def test_scriptsInRelease(self):
  95. """
  96. getScripts should return the scripts associated with a project
  97. in the context of a released subproject tarball.
  98. """
  99. basedir = self.mktemp()
  100. os.mkdir(basedir)
  101. os.mkdir(os.path.join(basedir, 'bin'))
  102. f = open(os.path.join(basedir, 'bin', 'exy'), 'w')
  103. f.write('yay')
  104. f.close()
  105. scripts = dist.getScripts('proj', basedir=basedir)
  106. self.assertEquals(len(scripts), 1)
  107. self.assertEquals(os.path.basename(scripts[0]), 'exy')
  108. def test_noScriptsInSVN(self):
  109. """
  110. When calling getScripts for a project which doesn't actually
  111. have any scripts, in the context of an SVN checkout, an
  112. empty list should be returned.
  113. """
  114. basedir = self.mktemp()
  115. os.mkdir(basedir)
  116. os.mkdir(os.path.join(basedir, 'bin'))
  117. os.mkdir(os.path.join(basedir, 'bin', 'otherproj'))
  118. scripts = dist.getScripts('noscripts', basedir=basedir)
  119. self.assertEquals(scripts, [])
  120. def test_getScriptsTopLevel(self):
  121. """
  122. Passing the empty string to getScripts returns scripts that are (only)
  123. in the top level bin directory.
  124. """
  125. basedir = FilePath(self.mktemp())
  126. basedir.createDirectory()
  127. bindir = basedir.child("bin")
  128. bindir.createDirectory()
  129. included = bindir.child("included")
  130. included.setContent("yay included")
  131. subdir = bindir.child("subdir")
  132. subdir.createDirectory()
  133. subdir.child("not-included").setContent("not included")
  134. scripts = dist.getScripts("", basedir=basedir.path)
  135. self.assertEquals(scripts, [included.path])
  136. def test_noScriptsInSubproject(self):
  137. """
  138. When calling getScripts for a project which doesn't actually
  139. have any scripts in the context of that project's individual
  140. project structure, an empty list should be returned.
  141. """
  142. basedir = self.mktemp()
  143. os.mkdir(basedir)
  144. scripts = dist.getScripts('noscripts', basedir=basedir)
  145. self.assertEquals(scripts, [])