1
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
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
18
"""Black-box tests for bzr whoami."""
27
from bzrlib.tests import TestCaseWithTransport
30
class TestWhoami(TestCaseWithTransport):
32
def test_whoami(self):
33
# this should always identify something, if only "john@localhost"
34
out = self.run_bzr("whoami")[0]
35
self.assertTrue(len(out) > 0)
36
self.assertEquals(1, out.count('@'))
38
out = self.run_bzr("whoami --email")[0]
39
self.assertTrue(len(out) > 0)
40
self.assertEquals(1, out.count('@'))
42
def test_whoami_branch(self):
43
"""branch specific user identity works."""
44
wt = self.make_branch_and_tree('.')
45
b = bzrlib.branch.Branch.open('.')
46
b.get_config().set_user_option('email',
47
'Branch Identity <branch@identi.ty>')
48
whoami = self.run_bzr("whoami")[0]
49
self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
50
whoami_email = self.run_bzr("whoami --email")[0]
51
self.assertEquals('branch@identi.ty\n', whoami_email)
53
# Verify that the environment variable overrides the value
55
os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
56
whoami = self.run_bzr("whoami")[0]
57
self.assertEquals('Different ID <other@environ.ment>\n', whoami)
58
whoami_email = self.run_bzr("whoami --email")[0]
59
self.assertEquals('other@environ.ment\n', whoami_email)
61
def test_whoami_utf8(self):
62
"""verify that an identity can be in utf-8."""
63
wt = self.make_branch_and_tree('.')
64
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
66
whoami = self.run_bzr("whoami", encoding='utf-8')[0]
67
self.assertEquals('Branch Identity \xe2\x82\xac ' +
68
'<branch@identi.ty>\n', whoami)
69
whoami_email = self.run_bzr("whoami --email", encoding='utf-8')[0]
70
self.assertEquals('branch@identi.ty\n', whoami_email)
72
def test_whoami_ascii(self):
74
verify that whoami doesn't totally break when in utf-8, using an ascii
77
wt = self.make_branch_and_tree('.')
78
b = bzrlib.branch.Branch.open('.')
79
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
81
whoami = self.run_bzr("whoami", encoding='ascii')[0]
82
self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
83
whoami_email = self.run_bzr("whoami --email", encoding='ascii')[0]
84
self.assertEquals('branch@identi.ty\n', whoami_email)
86
def test_warning(self):
87
"""verify that a warning is displayed if no email is given."""
88
self.make_branch_and_tree('.')
89
display = self.run_bzr(['whoami', 'Branch Identity'])[1]
90
self.assertEquals('"Branch Identity" does not seem to contain an '
91
'email address. This is allowed, but not '
92
'recommended.\n', display)
94
def test_whoami_not_set(self):
95
"""Ensure whoami error if username is not set.
97
osutils.set_or_unset_env('EMAIL', None)
98
osutils.set_or_unset_env('BZR_EMAIL', None)
99
out, err = self.run_bzr(['whoami'], 3)
100
self.assertContainsRe(err, 'Unable to determine your name')
102
def test_whoami_directory(self):
103
"""Test --directory option."""
104
wt = self.make_branch_and_tree('subdir')
105
c = wt.branch.get_config()
106
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
107
out, err = self.run_bzr("whoami --directory subdir")
108
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
109
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
110
'Changed Identity <changed@identi.ty>'])
111
self.assertEquals('Changed Identity <changed@identi.ty>',
112
c.get_user_option('email'))
114
def test_whoami_remote_directory(self):
115
"""Test --directory option with a remote directory."""
116
wt = self.make_branch_and_tree('subdir')
117
c = wt.branch.get_config()
118
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
119
url = self.get_readonly_url() + '/subdir'
120
out, err = self.run_bzr(['whoami', '--directory', url])
121
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
122
url = self.get_url('subdir')
123
self.run_bzr(['whoami', '--directory', url, '--branch',
124
'Changed Identity <changed@identi.ty>'])
125
# The identity has been set in the branch config (but not the global
127
self.assertEquals('Changed Identity <changed@identi.ty>',
128
c.get_user_option('email'))
129
global_conf = config.GlobalConfig()
130
self.assertEquals(None, global_conf.get_user_option('email'))
132
def test_whoami_nonbranch_directory(self):
133
"""Test --directory mentioning a non-branch directory."""
134
wt = self.build_tree(['subdir/'])
135
out, err = self.run_bzr("whoami --directory subdir", retcode=3)
136
self.assertContainsRe(err, 'ERROR: Not a branch')