sources for test_config.py [rev. 38799]
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from __future__ import generators
import py
from py.__.test.config import gettopdir
from py.__.test.testing.test_collect import skipboxed
def test_tmpdir():
    d1 = py.test.ensuretemp('hello') 
    d2 = py.test.ensuretemp('hello') 
    assert d1 == d2
    assert d1.check(dir=1) 
def test_config_cmdline_options(): 
    o = py.test.ensuretemp('configoptions') 
    o.ensure("conftest.py").write(py.code.Source(""" 
        import py
        def _callback(option, opt_str, value, parser, *args, **kwargs):
            option.tdest = True
        Option = py.test.config.Option
        option = py.test.config.addoptions("testing group", 
            Option('-G', '--glong', action="store", default=42,
                   type="int", dest="gdest", help="g value."), 
            # XXX note: special case, option without a destination
            Option('-T', '--tlong', action="callback", callback=_callback,
                    help='t value'),
            )
        """))
    old = o.chdir() 
    try: 
        config = py.test.config._reparse(['-G', '17'])
    finally: 
        old.chdir() 
    assert config.option.gdest == 17 
def test_config_cmdline_options_only_lowercase(): 
    o = py.test.ensuretemp('test_config_cmdline_options_only_lowercase')
    o.ensure("conftest.py").write(py.code.Source(""" 
        import py
        Option = py.test.config.Option
        options = py.test.config.addoptions("testing group", 
            Option('-g', '--glong', action="store", default=42,
                   type="int", dest="gdest", help="g value."), 
            )
        """))
    old = o.chdir() 
    try: 
        py.test.raises(ValueError, """
            py.test.config._reparse(['-g', '17'])
        """)
    finally: 
        old.chdir() 
def test_parsing_again_fails():
    dir = py.test.ensuretemp("parsing_again_fails")
    config = py.test.config._reparse([str(dir)])
    py.test.raises(AssertionError, "config.parse([])")
def test_config_getvalue_honours_conftest():
    o = py.test.ensuretemp('testconfigget') 
    o.ensure("conftest.py").write("x=1")
    o.ensure("sub", "conftest.py").write("x=2 ; y = 3")
    config = py.test.config._reparse([str(o)])
    assert config.getvalue("x") == 1
    assert config.getvalue("x", o.join('sub')) == 2
    py.test.raises(KeyError, "config.getvalue('y')")
    config = py.test.config._reparse([str(o.join('sub'))])
    assert config.getvalue("x") == 2
    assert config.getvalue("y") == 3
    assert config.getvalue("x", o) == 1
    py.test.raises(KeyError, 'config.getvalue("y", o)')
def test_siblingconftest_fails_maybe():
    from py.__.test import config
    cfg = config.Config()
    o = py.test.ensuretemp('siblingconftest')
    o.ensure("__init__.py")
    o.ensure("sister1", "__init__.py")
    o.ensure("sister1", "conftest.py").write(py.code.Source("""
        x = 2
    """))
        
    o.ensure("sister2", "__init__.py")
    o.ensure("sister2", "conftest.py").write(py.code.Source("""
        raise SyntaxError
    """))
    assert cfg.getvalue(path=o.join('sister1'), name='x') == 2
    old = o.chdir()
    try:
        pytestpath = py.magic.autopath().dirpath().dirpath().dirpath().join(
                        'bin/py.test')
        print py.process.cmdexec('python "%s" sister1' % (pytestpath,))
        o.join('sister1').chdir()
        print py.process.cmdexec('python "%s"' % (pytestpath,))
    finally:
        old.chdir()
def test_config_overwrite():
    o = py.test.ensuretemp('testconfigget') 
    o.ensure("conftest.py").write("x=1")
    config = py.test.config._reparse([str(o)])
    assert config.getvalue('x') == 1
    config.option.x = 2
    assert config.getvalue('x') == 2
    config = py.test.config._reparse([str(o)])
    assert config.getvalue('x') == 1
def test_gettopdir():
    tmp = py.test.ensuretemp("topdir")
    assert gettopdir([tmp]) == tmp
    topdir =gettopdir([tmp.join("hello"), tmp.join("world")])
    assert topdir == tmp 
def test_gettopdir_pypkg():
    tmp = py.test.ensuretemp("topdir2")
    a = tmp.ensure('a', dir=1)
    b = tmp.ensure('a', 'b', '__init__.py')
    c = tmp.ensure('a', 'b', 'c.py')
    Z = tmp.ensure('Z', dir=1)
    assert gettopdir([c]) == a
    assert gettopdir([c, Z]) == tmp 
def test_config_init_direct():
    tmp = py.test.ensuretemp("_initdirect")
    tmp.ensure("__init__.py")
    tmp.ensure("conftest.py").write("x=1 ; y=2")
    hello = tmp.ensure("test_hello.py")
    config = py.test.config._reparse([hello])
    repr = config._makerepr(conftestnames=['x', 'y'])
    config2 = py.test.config._reparse([tmp.dirpath()])
    config2._initialized = False # we have to do that from tests
    config2._initdirect(topdir=tmp.dirpath(), repr=repr)
    for col1, col2 in zip(config.getcolitems(), config2.getcolitems()):
        assert col1.fspath == col2.fspath
    py.test.raises(AssertionError, "config2._initdirect(None, None)")
    from py.__.test.config import Config
    config3 = Config()
    config3._initdirect(topdir=tmp.dirpath(), repr=repr,
        coltrails=[(tmp.basename, (hello.basename,))])
    assert config3.getvalue('x') == 1
    assert config3.getvalue('y') == 2
    cols = config.getcolitems()
    assert len(cols) == 1
    col = cols[0]
    assert col.name == 'test_hello.py'
    assert col.parent.name == tmp.basename 
    assert col.parent.parent is None 
def test_config_make_and__mergerepr():
    tmp = py.test.ensuretemp("reprconfig1")
    tmp.ensure("__init__.py")
    tmp.ensure("conftest.py").write("x=1")
    config = py.test.config._reparse([tmp])
    repr = config._makerepr(conftestnames=['x'])
    config.option.verbose = 42
    repr2 = config._makerepr(conftestnames=[], optnames=['verbose'])
    config = py.test.config._reparse([tmp.dirpath()])
    py.test.raises(KeyError, "config.getvalue('x')")
    config._mergerepr(repr)
    assert config.getvalue('x') == 1
    config._mergerepr(repr2) 
    assert config.option.verbose == 42
def test_config_marshability():
    tmp = py.test.ensuretemp("configmarshal") 
    tmp.ensure("__init__.py")
    tmp.ensure("conftest.py").write("a = object()")
    config = py.test.config._reparse([tmp])
    py.test.raises(ValueError, "config._makerepr(conftestnames=['a'])")
    config.option.hello = lambda x: None
    py.test.raises(ValueError, "config._makerepr(conftestnames=[])")
    config._makerepr(conftestnames=[], optnames=[])
def test_config_rconfig():
    tmp = py.test.ensuretemp("rconfigopt")
    tmp.ensure("__init__.py")
    tmp.ensure("conftest.py").write(py.code.Source("""
    import py
    Option = py.test.config.Option
    option = py.test.config.addoptions("testing group", 
            Option('-G', '--glong', action="store", default=42,
                   type="int", dest="gdest", help="g value."))
    """))
    config = py.test.config._reparse([tmp, "-G", "11"])
    assert config.option.gdest == 11
    repr = config._makerepr(conftestnames=[])
    config = py.test.config._reparse([tmp.dirpath()])
    py.test.raises(AttributeError, "config.option.gdest")
    config._mergerepr(repr) 
    assert config.option.gdest == 11
class TestSessionAndOptions: 
    def setup_class(cls):
        cls.tmproot = py.test.ensuretemp(cls.__name__)
    def setup_method(self, method):
        self.tmpdir = self.tmproot.ensure(method.__name__, dir=1) 
    def test_sessionname_default(self):
        config = py.test.config._reparse([self.tmpdir])
        assert config._getsessionname() == 'TerminalSession'
    def test_sessionname_dist(self):
        config = py.test.config._reparse([self.tmpdir, '--dist'])
        assert config._getsessionname() == 'RSession'
    def test_implied_lsession(self):
        optnames = 'startserver runbrowser apigen=x rest boxed'.split()
        for x in optnames:
            config = py.test.config._reparse([self.tmpdir, '--%s' % x])
            assert config._getsessionname() == 'LSession'
        for x in 'startserver runbrowser rest'.split():
            config = py.test.config._reparse([self.tmpdir, '--dist', '--%s' % x])
            assert config._getsessionname() == 'RSession'
    def test_implied_remote_terminal_session(self):
        config = py.test.config._reparse([self.tmpdir, '--looponfailing'])
        assert config._getsessionname() == 'RemoteTerminalSession'
        config = py.test.config._reparse([self.tmpdir, '--exec=x'])
        assert config._getsessionname() == 'RemoteTerminalSession'
        config = py.test.config._reparse([self.tmpdir, '--dist', '--exec=x'])
        assert config._getsessionname() == 'RSession'
    def test_sessionname_lookup_custom(self):
        self.tmpdir.join("conftest.py").write(py.code.Source("""
            from py.__.test.session import Session
            class MySession(Session):
                def __init__(self, config):
                    self.config = config 
        """)) 
        config = py.test.config._reparse(["--session=MySession", self.tmpdir])
        session = config.initsession()
        assert session.__class__.__name__ == 'MySession'
    def test_initsession(self):
        config = py.test.config._reparse([self.tmpdir])
        session = config.initsession()
        assert session.config is config 
    def test_boxed_option_default(self):
        self.tmpdir.join("conftest.py").write("dist_hosts=[]")
        tmpdir = self.tmpdir.ensure("subdir", dir=1)
        config = py.test.config._reparse([tmpdir])
        config.initsession()
        assert not config.option.boxed
        config = py.test.config._reparse(['--dist', tmpdir])
        config.initsession()
        assert not config.option.boxed
    def test_boxed_option_from_conftest(self):
        self.tmpdir.join("conftest.py").write("dist_hosts=[]")
        tmpdir = self.tmpdir.ensure("subdir", dir=1)
        tmpdir.join("conftest.py").write(py.code.Source("""
            dist_hosts = []
            dist_boxed = True
        """))
        config = py.test.config._reparse(['--dist', tmpdir])
        config.initsession()
        assert config.option.boxed 
    def test_boxed_option_from_conftest2(self):
        tmpdir = self.tmpdir
        tmpdir.join("conftest.py").write(py.code.Source("""
            dist_boxed = False
        """))
        config = py.test.config._reparse([tmpdir, '--box'])
        assert config.option.boxed 
        config.initsession()
        assert config.option.boxed
    def test_dist_session_no_capturedisable(self):
        config = py.test.config._reparse([self.tmpdir, '-d', '-s'])
        py.test.raises(SystemExit, "config.initsession()")
    def test_getvalue_pathlist(self):
        tmpdir = self.tmpdir
        somepath = tmpdir.join("x", "y", "z")
        p = tmpdir.join("conftest.py")
        p.write("pathlist = ['.', %r]" % str(somepath))
        config = py.test.config._reparse([p])
        assert config.getvalue_pathlist('notexist') is None
        pl = config.getvalue_pathlist('pathlist')
        print pl
        assert len(pl) == 2
        assert pl[0] == tmpdir
        assert pl[1] == somepath
        config.option.mypathlist = [py.path.local()]
        pl = config.getvalue_pathlist('mypathlist')
        assert pl == [py.path.local()]
    def test_config_iocapturing(self):
        self.tmpdir
        config = py.test.config._reparse([self.tmpdir])
        assert config.getvalue("conf_iocapture")
        tmpdir = self.tmpdir.ensure("sub-with-conftest", dir=1)
        tmpdir.join("conftest.py").write(py.code.Source("""
            conf_iocapture = "sys"
        """))
        config = py.test.config._reparse([tmpdir])
        assert config.getvalue("conf_iocapture") == "sys"
        class dummy: pass
        config._startcapture(dummy)
        print 42
        py.std.os.write(1, "23")
        config._finishcapture(dummy)
        assert dummy._captured_out.strip() == "42"
        
        config = py.test.config._reparse([tmpdir.dirpath()])
        config._startcapture(dummy, path=tmpdir)
        print 42
        py.std.os.write(1, "23")
        config._finishcapture(dummy)
        assert dummy._captured_out.strip() == "42"
class TestConfigColitems:
    def setup_class(cls):
        cls.tmproot = py.test.ensuretemp(cls.__name__)
    def setup_method(self, method):
        self.tmpdir = self.tmproot.mkdir(method.__name__) 
    
    def test_getcolitems_onedir(self):
        config = py.test.config._reparse([self.tmpdir])
        colitems = config.getcolitems()
        assert len(colitems) == 1
        col = colitems[0]
        assert isinstance(col, py.test.collect.Directory)
        for col in col.listchain():
            assert col._config is config 
    def test_getcolitems_twodirs(self):
        config = py.test.config._reparse([self.tmpdir, self.tmpdir])
        colitems = config.getcolitems()
        assert len(colitems) == 2
        col1, col2 = colitems 
        assert col1.name == col2.name 
        assert col1.parent == col2.parent 
    def test_getcolitems_curdir_and_subdir(self):
        a = self.tmpdir.ensure("a", dir=1)
        config = py.test.config._reparse([self.tmpdir, a])
        colitems = config.getcolitems()
        assert len(colitems) == 2
        col1, col2 = colitems 
        assert col1.name == self.tmpdir.basename
        assert col2.name == 'a'
        for col in colitems:
            for subcol in col.listchain():
                assert col._config is config 
    def test__getcol_global_file(self):
        x = self.tmpdir.ensure("x.py")
        config = py.test.config._reparse([x])
        col = config._getcollector(x)
        assert isinstance(col, py.test.collect.Module)
        assert col.name == 'x.py'
        assert col.parent.name == self.tmpdir.basename 
        assert col.parent.parent is None
        for col in col.listchain():
            assert col._config is config 
    def test__getcol_global_dir(self):
        x = self.tmpdir.ensure("a", dir=1)
        config = py.test.config._reparse([x])
        col = config._getcollector(x)
        assert isinstance(col, py.test.collect.Directory)
        print col.listchain()
        assert col.name == 'a'
        assert col.parent is None
        assert col._config is config 
    def test__getcol_pkgfile(self):
        x = self.tmpdir.ensure("x.py")
        self.tmpdir.ensure("__init__.py")
        config = py.test.config._reparse([x])
        col = config._getcollector(x)
        assert isinstance(col, py.test.collect.Module)
        assert col.name == 'x.py'
        assert col.parent.name == x.dirpath().basename 
        assert col.parent.parent is None
        for col in col.listchain():
            assert col._config is config 
    def test_get_collector_trail_and_back(self):
        a = self.tmpdir.ensure("a", dir=1)
        self.tmpdir.ensure("a", "__init__.py")
        x = self.tmpdir.ensure("a", "trail.py")
        config = py.test.config._reparse([x])
        col = config._getcollector(x)
        trail = config.get_collector_trail(col)
        assert len(trail) == 2
        assert trail[0] == a.relto(config.topdir)
        assert trail[1] == ('trail.py',)
        col2 = config._getcollector(trail)
        assert col2.listnames() == col.listnames()
       
    def test_get_collector_trail_topdir_and_beyond(self):
        config = py.test.config._reparse([self.tmpdir])
        col = config._getcollector(config.topdir)
        trail = config.get_collector_trail(col)
        assert len(trail) == 2
        assert trail[0] == '.'
        assert trail[1] == ()
        col2 = config._getcollector(trail)
        assert col2.fspath == config.topdir
        assert len(col2.listchain()) == 1
        col3 = config._getcollector(config.topdir.dirpath())
        py.test.raises(ValueError, 
              "config.get_collector_trail(col3)")