/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_config.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-22 00:56:52 UTC
  • mfrom: (6621.2.26 py3_pokes)
  • Revision ID: jelmer@jelmer.uk-20170522005652-yjahcr9hwmjkno7n
Merge Python3 porting work ('py3 pokes')

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for finding and reading the bzr config file[s]."""
18
18
 
19
 
from cStringIO import StringIO
20
19
from textwrap import dedent
21
20
import os
22
21
import sys
23
22
import threading
24
23
 
 
24
import configobj
25
25
from testtools import matchers
26
26
 
27
 
from breezy import (
 
27
from .. import (
28
28
    branch,
29
29
    config,
30
30
    controldir,
39
39
    tests,
40
40
    trace,
41
41
    )
42
 
from breezy.symbol_versioning import (
 
42
from ..sixish import (
 
43
    BytesIO,
 
44
    )
 
45
from ..symbol_versioning import (
43
46
    deprecated_in,
44
47
    )
45
 
from breezy.transport import remote as transport_remote
46
 
from breezy.tests import (
 
48
from ..transport import remote as transport_remote
 
49
from . import (
47
50
    features,
48
51
    scenarios,
49
52
    test_server,
50
53
    )
51
 
from breezy.util.configobj import configobj
52
54
 
53
55
 
54
56
def lockable_config_scenarios():
355
357
    def get(self, filename):
356
358
        # from Transport
357
359
        try:
358
 
            return StringIO(self.files[filename])
 
360
            return BytesIO(self.files[filename])
359
361
        except KeyError:
360
362
            raise errors.NoSuchFile(filename)
361
363
 
406
408
class TestConfigObj(tests.TestCase):
407
409
 
408
410
    def test_get_bool(self):
409
 
        co = config.ConfigObj(StringIO(bool_config))
 
411
        co = config.ConfigObj(BytesIO(bool_config))
410
412
        self.assertIs(co.get_bool('DEFAULT', 'active'), True)
411
413
        self.assertIs(co.get_bool('DEFAULT', 'inactive'), False)
412
414
        self.assertIs(co.get_bool('UPPERCASE', 'active'), True)
419
421
        """
420
422
        co = config.ConfigObj()
421
423
        co['test'] = 'foo#bar'
422
 
        outfile = StringIO()
 
424
        outfile = BytesIO()
423
425
        co.write(outfile=outfile)
424
426
        lines = outfile.getvalue().splitlines()
425
427
        self.assertEqual(lines, ['test = "foo#bar"'])
443
445
        # This issue only affects test, but it's better to avoid
444
446
        # `co.write()` construct at all.
445
447
        # [bialix 20110222] bug report sent to ConfigObj's author
446
 
        outfile = StringIO()
 
448
        outfile = BytesIO()
447
449
        co.write(outfile=outfile)
448
450
        output = outfile.getvalue()
449
451
        # now we're trying to read it back
450
 
        co2 = config.ConfigObj(StringIO(output))
 
452
        co2 = config.ConfigObj(BytesIO(output))
451
453
        self.assertEqual(triple_quotes_value, co2['test'])
452
454
 
453
455
 
462
464
 
463
465
    def test_duplicate_section_name_error_line(self):
464
466
        try:
465
 
            co = configobj.ConfigObj(StringIO(erroneous_config),
 
467
            co = configobj.ConfigObj(BytesIO(erroneous_config),
466
468
                                     raise_errors=True)
467
 
        except config.configobj.DuplicateError, e:
 
469
        except config.configobj.DuplicateError as e:
468
470
            self.assertEqual(3, e.line_number)
469
471
        else:
470
472
            self.fail('Error in config file not detected')
662
664
        self.assertEqual('ini.conf', conf.file_name)
663
665
 
664
666
    def test_get_parser_file_parameter_is_deprecated_(self):
665
 
        config_file = StringIO(sample_config_text.encode('utf-8'))
 
667
        config_file = BytesIO(sample_config_text.encode('utf-8'))
666
668
        conf = config.IniBasedConfig.from_string(sample_config_text)
667
669
        conf = self.callDeprecated([
668
670
            'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
2680
2682
 
2681
2683
    def test_id(self):
2682
2684
        store = self.get_store(self)
2683
 
        if type(store) == config.TransportIniFileStore:
 
2685
        if isinstance(store, config.TransportIniFileStore):
2684
2686
            raise tests.TestNotApplicable(
2685
2687
                "%s is not a concrete Store implementation"
2686
2688
                " so it doesn't need an id" % (store.__class__.__name__,))
2689
2691
 
2690
2692
class TestStore(tests.TestCaseWithTransport):
2691
2693
 
2692
 
    def assertSectionContent(self, expected, (store, section)):
 
2694
    def assertSectionContent(self, expected, store_and_section):
2693
2695
        """Assert that some options have the proper values in a section."""
 
2696
        _, section = store_and_section
2694
2697
        expected_name, expected_options = expected
2695
2698
        self.assertEqual(expected_name, section.id)
2696
2699
        self.assertEqual(
4446
4449
        self.assertEqual(expected_password, password)
4447
4450
 
4448
4451
    def test_empty_config(self):
4449
 
        conf = config.AuthenticationConfig(_file=StringIO())
 
4452
        conf = config.AuthenticationConfig(_file=BytesIO())
4450
4453
        self.assertEqual({}, conf._get_config())
4451
4454
        self._got_user_passwd(None, None, conf, 'http', 'foo.net')
4452
4455
 
4453
4456
    def test_non_utf8_config(self):
4454
 
        conf = config.AuthenticationConfig(_file=StringIO(
4455
 
                'foo = bar\xff'))
 
4457
        conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar\xff'))
4456
4458
        self.assertRaises(errors.ConfigContentError, conf._get_config)
4457
4459
 
4458
4460
    def test_missing_auth_section_header(self):
4459
 
        conf = config.AuthenticationConfig(_file=StringIO('foo = bar'))
 
4461
        conf = config.AuthenticationConfig(_file=BytesIO(b'foo = bar'))
4460
4462
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4461
4463
 
4462
4464
    def test_auth_section_header_not_closed(self):
4463
 
        conf = config.AuthenticationConfig(_file=StringIO('[DEF'))
 
4465
        conf = config.AuthenticationConfig(_file=BytesIO(b'[DEF'))
4464
4466
        self.assertRaises(errors.ParseConfigError, conf._get_config)
4465
4467
 
4466
4468
    def test_auth_value_not_boolean(self):
4467
 
        conf = config.AuthenticationConfig(_file=StringIO(
4468
 
                """[broken]
 
4469
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4470
[broken]
4469
4471
scheme=ftp
4470
4472
user=joe
4471
4473
verify_certificates=askme # Error: Not a boolean
4473
4475
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4474
4476
 
4475
4477
    def test_auth_value_not_int(self):
4476
 
        conf = config.AuthenticationConfig(_file=StringIO(
4477
 
                """[broken]
 
4478
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4479
[broken]
4478
4480
scheme=ftp
4479
4481
user=joe
4480
4482
port=port # Error: Not an int
4482
4484
        self.assertRaises(ValueError, conf.get_credentials, 'ftp', 'foo.net')
4483
4485
 
4484
4486
    def test_unknown_password_encoding(self):
4485
 
        conf = config.AuthenticationConfig(_file=StringIO(
4486
 
                """[broken]
 
4487
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4488
[broken]
4487
4489
scheme=ftp
4488
4490
user=joe
4489
4491
password_encoding=unknown
4492
4494
                          'ftp', 'foo.net', 'joe')
4493
4495
 
4494
4496
    def test_credentials_for_scheme_host(self):
4495
 
        conf = config.AuthenticationConfig(_file=StringIO(
4496
 
                """# Identity on foo.net
 
4497
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4498
# Identity on foo.net
4497
4499
[ftp definition]
4498
4500
scheme=ftp
4499
4501
host=foo.net
4508
4510
        self._got_user_passwd(None, None, conf, 'ftp', 'bar.net')
4509
4511
 
4510
4512
    def test_credentials_for_host_port(self):
4511
 
        conf = config.AuthenticationConfig(_file=StringIO(
4512
 
                """# Identity on foo.net
 
4513
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4514
# Identity on foo.net
4513
4515
[ftp definition]
4514
4516
scheme=ftp
4515
4517
port=10021
4524
4526
        self._got_user_passwd(None, None, conf, 'ftp', 'foo.net')
4525
4527
 
4526
4528
    def test_for_matching_host(self):
4527
 
        conf = config.AuthenticationConfig(_file=StringIO(
4528
 
                """# Identity on foo.net
 
4529
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4530
# Identity on foo.net
4529
4531
[sourceforge]
4530
4532
scheme=bzr
4531
4533
host=bzr.sf.net
4545
4547
                              conf, 'bzr', 'bbzr.sf.net')
4546
4548
 
4547
4549
    def test_for_matching_host_None(self):
4548
 
        conf = config.AuthenticationConfig(_file=StringIO(
4549
 
                """# Identity on foo.net
 
4550
        conf = config.AuthenticationConfig(_file=BytesIO(b"""\
 
4551
# Identity on foo.net
4550
4552
[catchup bzr]
4551
4553
scheme=bzr
4552
4554
user=joe
4563
4565
                              conf, 'ftp', 'quux.net')
4564
4566
 
4565
4567
    def test_credentials_for_path(self):
4566
 
        conf = config.AuthenticationConfig(_file=StringIO(
4567
 
                """
 
4568
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4568
4569
[http dir1]
4569
4570
scheme=http
4570
4571
host=bar.org
4589
4590
                              conf, 'http', host='bar.org',path='/dir1/subdir')
4590
4591
 
4591
4592
    def test_credentials_for_user(self):
4592
 
        conf = config.AuthenticationConfig(_file=StringIO(
4593
 
                """
 
4593
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4594
4594
[with user]
4595
4595
scheme=http
4596
4596
host=bar.org
4608
4608
                              conf, 'http', 'bar.org', user='georges')
4609
4609
 
4610
4610
    def test_credentials_for_user_without_password(self):
4611
 
        conf = config.AuthenticationConfig(_file=StringIO(
4612
 
                """
 
4611
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4613
4612
[without password]
4614
4613
scheme=http
4615
4614
host=bar.org
4620
4619
                              conf, 'http', 'bar.org')
4621
4620
 
4622
4621
    def test_verify_certificates(self):
4623
 
        conf = config.AuthenticationConfig(_file=StringIO(
4624
 
                """
 
4622
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4625
4623
[self-signed]
4626
4624
scheme=https
4627
4625
host=bar.org
4684
4682
            'scheme': scheme, 'host': host, 'port': port,
4685
4683
            'user': user, 'realm': realm}
4686
4684
 
4687
 
        stdout = tests.StringIOWrapper()
4688
 
        stderr = tests.StringIOWrapper()
4689
 
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
4690
 
                                            stdout=stdout, stderr=stderr)
 
4685
        ui.ui_factory = tests.TestUIFactory(stdin=password + '\n')
4691
4686
        # We use an empty conf so that the user is always prompted
4692
4687
        conf = config.AuthenticationConfig()
4693
4688
        self.assertEqual(password,
4694
4689
                          conf.get_password(scheme, host, user, port=port,
4695
4690
                                            realm=realm, path=path))
4696
 
        self.assertEqual(expected_prompt, stderr.getvalue())
4697
 
        self.assertEqual('', stdout.getvalue())
 
4691
        self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
 
4692
        self.assertEqual('', ui.ui_factory.stdout.getvalue())
4698
4693
 
4699
4694
    def _check_default_username_prompt(self, expected_prompt_format, scheme,
4700
4695
                                       host=None, port=None, realm=None,
4705
4700
        expected_prompt = expected_prompt_format % {
4706
4701
            'scheme': scheme, 'host': host, 'port': port,
4707
4702
            'realm': realm}
4708
 
        stdout = tests.StringIOWrapper()
4709
 
        stderr = tests.StringIOWrapper()
4710
 
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n',
4711
 
                                            stdout=stdout, stderr=stderr)
 
4703
        ui.ui_factory = tests.TestUIFactory(stdin=username+ '\n')
4712
4704
        # We use an empty conf so that the user is always prompted
4713
4705
        conf = config.AuthenticationConfig()
4714
4706
        self.assertEqual(username, conf.get_user(scheme, host, port=port,
4715
4707
                          realm=realm, path=path, ask=True))
4716
 
        self.assertEqual(expected_prompt, stderr.getvalue())
4717
 
        self.assertEqual('', stdout.getvalue())
 
4708
        self.assertEqual(expected_prompt, ui.ui_factory.stderr.getvalue())
 
4709
        self.assertEqual('', ui.ui_factory.stdout.getvalue())
4718
4710
 
4719
4711
    def test_username_defaults_prompts(self):
4720
4712
        # HTTP prompts can't be tested here, see test_http.py
4751
4743
            u'SMTP %(user)s@%(host)s:%(port)d password: ', 'smtp', port=10025)
4752
4744
 
4753
4745
    def test_ssh_password_emits_warning(self):
4754
 
        conf = config.AuthenticationConfig(_file=StringIO(
4755
 
                """
 
4746
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4756
4747
[ssh with password]
4757
4748
scheme=ssh
4758
4749
host=bar.org
4760
4751
password=jimpass
4761
4752
"""))
4762
4753
        entered_password = 'typed-by-hand'
4763
 
        stdout = tests.StringIOWrapper()
4764
 
        stderr = tests.StringIOWrapper()
4765
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
4766
 
                                            stdout=stdout, stderr=stderr)
 
4754
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
4767
4755
 
4768
4756
        # Since the password defined in the authentication config is ignored,
4769
4757
        # the user is prompted
4774
4762
            'password ignored in section \[ssh with password\]')
4775
4763
 
4776
4764
    def test_ssh_without_password_doesnt_emit_warning(self):
4777
 
        conf = config.AuthenticationConfig(_file=StringIO(
4778
 
                """
 
4765
        conf = config.AuthenticationConfig(_file=BytesIO(b"""
4779
4766
[ssh with password]
4780
4767
scheme=ssh
4781
4768
host=bar.org
4782
4769
user=jim
4783
4770
"""))
4784
4771
        entered_password = 'typed-by-hand'
4785
 
        stdout = tests.StringIOWrapper()
4786
 
        stderr = tests.StringIOWrapper()
4787
 
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n',
4788
 
                                            stdout=stdout,
4789
 
                                            stderr=stderr)
 
4772
        ui.ui_factory = tests.TestUIFactory(stdin=entered_password + '\n')
4790
4773
 
4791
4774
        # Since the password defined in the authentication config is ignored,
4792
4775
        # the user is prompted
4804
4787
        store = StubCredentialStore()
4805
4788
        store.add_credentials("http", "example.com", "joe", "secret")
4806
4789
        config.credential_store_registry.register("stub", store, fallback=True)
4807
 
        conf = config.AuthenticationConfig(_file=StringIO())
 
4790
        conf = config.AuthenticationConfig(_file=BytesIO())
4808
4791
        creds = conf.get_credentials("http", "example.com")
4809
4792
        self.assertEqual("joe", creds["user"])
4810
4793
        self.assertEqual("secret", creds["password"])