/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/blackbox/test_whoami.py

  • Committer: Martin
  • Date: 2018-08-21 00:53:34 UTC
  • mto: This revision was merged to the branch mainline in revision 7074.
  • Revision ID: gzlist@googlemail.com-20180821005334-e1ogxakojyybpwib
Fix recursion check in C bencode implementation

Hard to get Cython to do the right thing but by inverting the
return code can use the standard except handling.

Avoid going through a Python call when encoding, which requires
the encode recursion check to work too.

Adjust tests to use a smaller limit to be more managable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
 
"""Black-box tests for bzr whoami."""
19
 
 
20
 
import os
21
 
 
22
 
import bzrlib
23
 
from bzrlib import osutils
24
 
from bzrlib.branch import Branch
25
 
from bzrlib.tests.blackbox import ExternalBase
26
 
 
27
 
 
28
 
class TestWhoami(ExternalBase):
29
 
 
30
 
    def test_whoami(self):
 
18
"""Black-box tests for brz whoami."""
 
19
 
 
20
import breezy
 
21
from breezy import (
 
22
    branch,
 
23
    config,
 
24
    errors,
 
25
    tests,
 
26
    )
 
27
from breezy.sixish import PY3
 
28
 
 
29
 
 
30
class TestWhoami(tests.TestCaseWithTransport):
 
31
 
 
32
    def assertWhoAmI(self, expected, *cmd_args, **kwargs):
 
33
        out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
 
34
        self.assertEqual('', err)
 
35
        lines = out.splitlines()
 
36
        self.assertLength(1, lines)
 
37
        if PY3 and isinstance(expected, bytes):
 
38
            expected = expected.decode(kwargs.get('encoding', 'ascii'))
 
39
        self.assertEqual(expected, lines[0].rstrip())
 
40
 
 
41
    def test_whoami_no_args_no_conf(self):
31
42
        # this should always identify something, if only "john@localhost"
32
43
        out = self.run_bzr("whoami")[0]
33
44
        self.assertTrue(len(out) > 0)
34
 
        self.assertEquals(1, out.count('@'))
 
45
        self.assertEqual(1, out.count('@'))
35
46
 
 
47
    def test_whoami_email_no_args(self):
36
48
        out = self.run_bzr("whoami --email")[0]
37
49
        self.assertTrue(len(out) > 0)
38
 
        self.assertEquals(1, out.count('@'))
 
50
        self.assertEqual(1, out.count('@'))
 
51
 
 
52
    def test_whoami_email_arg(self):
 
53
        # whoami --email is mutually exclusive with any arguments
 
54
        out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
 
55
        self.assertEqual("", out)
 
56
 
 
57
    def set_branch_email(self, b, email):
 
58
        b.get_config_stack().set('email', email)
39
59
 
40
60
    def test_whoami_branch(self):
41
61
        """branch specific user identity works."""
42
62
        wt = self.make_branch_and_tree('.')
43
 
        b = bzrlib.branch.Branch.open('.')
44
 
        b.get_config().set_user_option('email',
45
 
                                       'Branch Identity <branch@identi.ty>')
46
 
        bzr_email = os.environ.get('BZR_EMAIL')
47
 
        if bzr_email is not None:
48
 
            del os.environ['BZR_EMAIL']
49
 
        try:
50
 
            whoami = self.run_bzr("whoami")[0]
51
 
            self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
52
 
            whoami_email = self.run_bzr("whoami --email")[0]
53
 
            self.assertEquals('branch@identi.ty\n', whoami_email)
 
63
        b = breezy.branch.Branch.open('.')
 
64
        self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
 
65
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
 
66
        self.assertWhoAmI('branch@identi.ty', '--email')
54
67
 
55
 
            # Verify that the environment variable overrides the value
56
 
            # in the file
57
 
            os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
58
 
            whoami = self.run_bzr("whoami")[0]
59
 
            self.assertEquals('Different ID <other@environ.ment>\n', whoami)
60
 
            whoami_email = self.run_bzr("whoami --email")[0]
61
 
            self.assertEquals('other@environ.ment\n', whoami_email)
62
 
            del os.environ['BZR_EMAIL']
63
 
        finally:
64
 
            if bzr_email is not None:
65
 
                os.environ['BZR_EMAIL'] = bzr_email
 
68
        # Verify that the environment variable overrides the value
 
69
        # in the file
 
70
        self.overrideEnv('BRZ_EMAIL', 'Different ID <other@environ.ment>')
 
71
        self.assertWhoAmI('Different ID <other@environ.ment>')
 
72
        self.assertWhoAmI('other@environ.ment', '--email')
66
73
 
67
74
    def test_whoami_utf8(self):
68
75
        """verify that an identity can be in utf-8."""
69
 
        wt = self.make_branch_and_tree('.')
70
76
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
71
77
                     encoding='utf-8')
72
 
        bzr_email = os.environ.get('BZR_EMAIL')
73
 
        if bzr_email is not None:
74
 
            del os.environ['BZR_EMAIL']
75
 
        try:
76
 
            whoami = self.run_bzr("whoami", encoding='utf-8')[0]
77
 
            self.assertEquals('Branch Identity \xe2\x82\xac ' +
78
 
                              '<branch@identi.ty>\n', whoami)
79
 
            whoami_email = self.run_bzr("whoami --email",
80
 
                                        encoding='utf-8')[0]
81
 
            self.assertEquals('branch@identi.ty\n', whoami_email)
82
 
        finally:
83
 
            if bzr_email is not None:
84
 
                os.environ['BZR_EMAIL'] = bzr_email
 
78
        self.assertWhoAmI(b'Branch Identity \xe2\x82\xac <branch@identi.ty>',
 
79
                          encoding='utf-8')
 
80
        self.assertWhoAmI('branch@identi.ty', '--email')
85
81
 
86
82
    def test_whoami_ascii(self):
87
83
        """
89
85
        encoding.
90
86
        """
91
87
        wt = self.make_branch_and_tree('.')
92
 
        b = bzrlib.branch.Branch.open('.')
93
 
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
94
 
                                       '<branch@identi.ty>')
95
 
        bzr_email = os.environ.get('BZR_EMAIL')
96
 
        if bzr_email is not None:
97
 
            del os.environ['BZR_EMAIL']
98
 
        try:
99
 
            whoami = self.run_bzr("whoami", encoding='ascii')[0]
100
 
            self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
101
 
            whoami_email = self.run_bzr("whoami --email",
102
 
                                        encoding='ascii')[0]
103
 
            self.assertEquals('branch@identi.ty\n', whoami_email)
104
 
        finally:
105
 
            if bzr_email is not None:
106
 
                os.environ['BZR_EMAIL'] = bzr_email
 
88
        b = breezy.branch.Branch.open('.')
 
89
        self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
 
90
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
 
91
                          encoding='ascii')
 
92
        self.assertWhoAmI('branch@identi.ty', '--email',
 
93
                          encoding='ascii')
107
94
 
108
95
    def test_warning(self):
109
96
        """verify that a warning is displayed if no email is given."""
110
97
        self.make_branch_and_tree('.')
111
98
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
112
 
        self.assertEquals('"Branch Identity" does not seem to contain an '
 
99
        self.assertEqual('"Branch Identity" does not seem to contain an '
113
100
                          'email address.  This is allowed, but not '
114
101
                          'recommended.\n', display)
115
102
 
116
103
    def test_whoami_not_set(self):
117
 
        """Ensure whoami error if username is not set.
 
104
        """Ensure whoami error if username is not set and not inferred.
118
105
        """
119
 
        osutils.set_or_unset_env('EMAIL', None)
120
 
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
106
        self.overrideEnv('EMAIL', None)
 
107
        self.overrideEnv('BRZ_EMAIL', None)
 
108
        # Also, make sure that it's not inferred from mailname.
 
109
        self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
121
110
        out, err = self.run_bzr(['whoami'], 3)
122
111
        self.assertContainsRe(err, 'Unable to determine your name')
 
112
 
 
113
    def test_whoami_directory(self):
 
114
        """Test --directory option."""
 
115
        wt = self.make_branch_and_tree('subdir')
 
116
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
 
117
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
118
                          '--directory', 'subdir')
 
119
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
 
120
                      'Changed Identity <changed@identi.ty>'])
 
121
        # Refresh wt as 'whoami' modified it
 
122
        wt = wt.controldir.open_workingtree()
 
123
        c = wt.branch.get_config_stack()
 
124
        self.assertEqual('Changed Identity <changed@identi.ty>',
 
125
                          c.get('email'))
 
126
 
 
127
    def test_whoami_remote_directory(self):
 
128
        """Test --directory option with a remote directory."""
 
129
        wt = self.make_branch_and_tree('subdir')
 
130
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
 
131
        url = self.get_readonly_url() + '/subdir'
 
132
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
133
                          '--directory', url)
 
134
        url = self.get_url('subdir')
 
135
        self.run_bzr(['whoami', '--directory', url, '--branch',
 
136
                      'Changed Identity <changed@identi.ty>'])
 
137
        # The identity has been set in the branch config (but not the global
 
138
        # config)
 
139
        c = branch.Branch.open(url).get_config_stack()
 
140
        self.assertEqual('Changed Identity <changed@identi.ty>',
 
141
                          c.get('email'))
 
142
        # Ensuring that the value does not come from the breezy.conf file
 
143
        # itself requires some isolation setup
 
144
        self.overrideEnv('BRZ_EMAIL', None)
 
145
        self.overrideEnv('EMAIL', None)
 
146
        self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
 
147
        global_conf = config.GlobalStack()
 
148
        self.assertRaises(config.NoWhoami, global_conf.get, 'email')
 
149
 
 
150
    def test_whoami_nonbranch_directory(self):
 
151
        """Test --directory mentioning a non-branch directory."""
 
152
        wt = self.build_tree(['subdir/'])
 
153
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
 
154
        self.assertContainsRe(err, 'ERROR: Not a branch')