File: test_command.py

package info (click to toggle)
pastescript 3.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: python: 5,212; sh: 65; makefile: 61
file content (239 lines) | stat: -rw-r--r-- 7,720 bytes parent folder | download
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
#!/usr/bin/env python
from paste.script import command
from paste.script import create_distro
from paste.script import entrypoints
import contextlib
import io
import os
import re
import shutil
import sys
import tempfile
import textwrap
import unittest


@contextlib.contextmanager
def capture_stdout():
    stdout = sys.stdout
    try:
        sys.stdout = io.StringIO()
        yield sys.stdout
    finally:
        sys.stdout = stdout


@contextlib.contextmanager
def temporary_dir():
    old_dir = os.getcwd()
    try:
        if hasattr(tempfile, 'TemporaryDirectory'):
            # Python 3
            with tempfile.TemporaryDirectory() as tmpdir:
                os.chdir(tmpdir)
                yield
        else:
            # Python 2
            tmpdir = tempfile.mkdtemp()
            try:
                os.chdir(tmpdir)
                yield
            finally:
                shutil.rmtree(tmpdir)
    finally:
        os.chdir(old_dir)


class CommandTest(unittest.TestCase):
    maxDiff = 1024

    def test_help(self):
        usage = textwrap.dedent('''
            Usage: <test_command> [paster_options] COMMAND [command_options]

            Options:
              --version         show program's version number and exit
              --plugin=PLUGINS  Add a plugin to the list of commands (plugins are Egg
                                specs; will also require() the Egg)
              -h, --help        Show this help message

            Commands:
              create       Create the file layout for a Python distribution
              grep         Search project for symbol
              help         Display help
              make-config  Install a package and create a fresh config file/directory
              points       Show information about entry points
              post         Run a request for the described application
              request      Run a request for the described application
              serve        Serve the described application
              setup-app    Setup an application, given a config file
        ''').strip() + "\n\n"

        with capture_stdout() as stdout:
            argv = sys.argv
            sys.argv = ['<test_command>', '--help']
            try:
                try:
                    command.run(['--help'])
                except SystemExit as exc:
                    self.assertEqual(exc.code, 0)
                else:
                    self.fail("SystemExit not raised")
            finally:
                sys.argv = argv


class CreateDistroCommandTest(unittest.TestCase):
    maxDiff = 1024

    def setUp(self):
        self.cmd = create_distro.CreateDistroCommand('create_distro')

    def test_list_templates(self):
        templates = textwrap.dedent('''
            Available templates:
              basic_package:  A basic setuptools-enabled package
              paste_deploy:   A web application deployed through paste.deploy
        ''').strip() + "\n"
        with capture_stdout() as stdout:
            self.cmd.run(['--list-templates'])
            self.assertEqual(templates, stdout.getvalue())

    def test_basic_package(self):
        inputs = [
            '1.0',  # Version
            'description',   # Description
            'long description',   # Long description
            'keyword1 keyword2',   # Keywords
            'author name',   # Author name
            'author@domain.com',   # Author email
            'http://example.com',   # URL of homepage
            'license',   # License
            'True',   # zip_safe
        ]
        name = 'test'

        setup_cfg = textwrap.dedent('''
            [egg_info]
            tag_build = dev
            tag_svn_revision = true
        ''').strip() + '\n'

        setup_py = textwrap.dedent(r'''
            from setuptools import setup, find_packages
            import sys, os

            version = '1.0'

            setup(name='test',
                  version=version,
                  description="description",
                  long_description="""\
            long description""",
                  classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
                  keywords='keyword1 keyword2',
                  author='author name',
                  author_email='author@domain.com',
                  url='http://example.com',
                  license='license',
                  packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
                  include_package_data=True,
                  zip_safe=True,
                  install_requires=[
                      # -*- Extra requirements: -*-
                  ],
                  entry_points="""
                  # -*- Entry points: -*-
                  """,
                  )
        ''').strip() + "\n"

        with temporary_dir():
            stdin = sys.stdin
            try:
                sys.stdin = io.StringIO('\n'.join(inputs))
                with capture_stdout():
                    self.cmd.run(['--template=basic_package', name])
            finally:
                sys.stdin = stdin

            os.chdir(name)

            with open("setup.cfg") as f:
                self.assertEqual(setup_cfg, f.read())

            with open("setup.py") as f:
                self.assertEqual(setup_py, f.read())

            with open(os.path.join(name, "__init__.py")) as f:
                self.assertEqual("#\n", f.read())


class EntryPointsTest(unittest.TestCase):
    maxDiff = 4096

    def setUp(self):
        self.cmd = entrypoints.EntryPointCommand('entrypoint')

    def test_paster_command(self):
        # Issue #20: Check that SuperGeneric works on Python 3
        paster = textwrap.dedent('''
            create = paste.script.create_distro:CreateDistroCommand
                (self, name)
            exe = paste.script.exe:ExeCommand
                (self, name)
            help = paste.script.help:HelpCommand
                (self, name)
            make-config = paste.script.appinstall:MakeConfigCommand
                (self, name)
            points = paste.script.entrypoints:EntryPointCommand
                (self, name)
            post = paste.script.request:RequestCommand
                (self, name)
            request = paste.script.request:RequestCommand
                (self, name)
            serve = paste.script.serve:ServeCommand
                (self, name)
            setup-app = paste.script.appinstall:SetupCommand
                (self, name)
        ''').strip()
        with capture_stdout() as stdout:
            res = self.cmd.run(['paster_command'])
            self.assertEqual(res, 0)
            out = stdout.getvalue()

        self.assertIn(paster, out)


class PostTest(unittest.TestCase):
    maxDiff = 4096

    def test_post(self):
        config = os.path.join('docs', 'example_app.ini')
        url = '/'
        with capture_stdout() as stdout:
            stdout.buffer = io.BytesIO()
            try:
                command.run(['post', config, url])
            except SystemExit as exc:
                self.assertEqual(exc.code, 0)
            else:
                self.fail("SystemExit not raised")
            out = stdout.buffer.getvalue()
            out = out.decode('utf-8')
        html_regex = textwrap.dedent('''
            <html>
            <head>
              <title>Test Application</title>
            </head>
            <body>
            .*
            </body>
            </html>
        ''').strip()
        html_regex = '\n%s\n' % html_regex
        html_regex = re.compile(html_regex, re.DOTALL)
        self.assertRegex(out, html_regex)

if __name__ == "__main__":
    unittest.main()