~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_whoami.py

Merge bzr.dev (and fix NEWS)

Show diffs side-by-side

added added

removed removed

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