18
18
"""Black-box tests for bzr whoami."""
23
from bzrlib.branch import Branch
24
from bzrlib.tests.blackbox import ExternalBase
27
class TestWhoami(ExternalBase):
29
def test_whoami(self):
29
class TestWhoami(tests.TestCaseWithTransport):
31
def assertWhoAmI(self, expected, *cmd_args, **kwargs):
32
out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
33
self.assertEquals('', err)
34
lines = out.splitlines()
35
self.assertLength(1, lines)
36
self.assertEquals(expected, lines[0].rstrip())
38
def test_whoami_no_args_no_conf(self):
30
39
# this should always identify something, if only "john@localhost"
31
40
out = self.run_bzr("whoami")[0]
32
41
self.assertTrue(len(out) > 0)
33
42
self.assertEquals(1, out.count('@'))
44
def test_whoami_email_no_args(self):
35
45
out = self.run_bzr("whoami --email")[0]
36
46
self.assertTrue(len(out) > 0)
37
47
self.assertEquals(1, out.count('@'))
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.assertEquals("", out)
54
def set_branch_email(self, b, email):
57
b.get_config_stack().set('email', email)
39
61
def test_whoami_branch(self):
40
62
"""branch specific user identity works."""
41
63
wt = self.make_branch_and_tree('.')
42
64
b = bzrlib.branch.Branch.open('.')
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']
49
whoami = self.run_bzr("whoami")[0]
50
self.assertEquals('Branch Identity <branch@identi.ty>\n', whoami)
51
whoami_email = self.run_bzr("whoami --email")[0]
52
self.assertEquals('branch@identi.ty\n', whoami_email)
65
self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
66
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
67
self.assertWhoAmI('branch@identi.ty', '--email')
54
# Verify that the environment variable overrides the value
56
os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
57
whoami = self.run_bzr("whoami")[0]
58
self.assertEquals('Different ID <other@environ.ment>\n', whoami)
59
whoami_email = self.run_bzr("whoami --email")[0]
60
self.assertEquals('other@environ.ment\n', whoami_email)
61
del os.environ['BZR_EMAIL']
63
if bzr_email is not None:
64
os.environ['BZR_EMAIL'] = bzr_email
69
# Verify that the environment variable overrides the value
71
self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
72
self.assertWhoAmI('Different ID <other@environ.ment>')
73
self.assertWhoAmI('other@environ.ment', '--email')
66
75
def test_whoami_utf8(self):
67
76
"""verify that an identity can be in utf-8."""
68
wt = self.make_branch_and_tree('.')
69
77
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
71
bzr_email = os.environ.get('BZR_EMAIL')
72
if bzr_email is not None:
73
del os.environ['BZR_EMAIL']
75
whoami = self.run_bzr("whoami", encoding='utf-8')[0]
76
self.assertEquals('Branch Identity \xe2\x82\xac ' +
77
'<branch@identi.ty>\n', whoami)
78
whoami_email = self.run_bzr("whoami --email",
80
self.assertEquals('branch@identi.ty\n', whoami_email)
82
if bzr_email is not None:
83
os.environ['BZR_EMAIL'] = bzr_email
79
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
81
self.assertWhoAmI('branch@identi.ty', '--email')
85
83
def test_whoami_ascii(self):
90
88
wt = self.make_branch_and_tree('.')
91
89
b = bzrlib.branch.Branch.open('.')
92
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
94
bzr_email = os.environ.get('BZR_EMAIL')
95
if bzr_email is not None:
96
del os.environ['BZR_EMAIL']
98
whoami = self.run_bzr("whoami", encoding='ascii')[0]
99
self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
100
whoami_email = self.run_bzr("whoami --email",
102
self.assertEquals('branch@identi.ty\n', whoami_email)
104
if bzr_email is not None:
105
os.environ['BZR_EMAIL'] = bzr_email
90
self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
91
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
93
self.assertWhoAmI('branch@identi.ty', '--email',
107
96
def test_warning(self):
108
97
"""verify that a warning is displayed if no email is given."""
111
100
self.assertEquals('"Branch Identity" does not seem to contain an '
112
101
'email address. This is allowed, but not '
113
102
'recommended.\n', display)
104
def test_whoami_not_set(self):
105
"""Ensure whoami error if username is not set and not inferred.
107
self.overrideEnv('EMAIL', None)
108
self.overrideEnv('BZR_EMAIL', None)
109
# Also, make sure that it's not inferred from mailname.
110
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
111
out, err = self.run_bzr(['whoami'], 3)
112
self.assertContainsRe(err, 'Unable to determine your name')
114
def test_whoami_directory(self):
115
"""Test --directory option."""
116
wt = self.make_branch_and_tree('subdir')
117
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
118
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
119
'--directory', 'subdir')
120
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
121
'Changed Identity <changed@identi.ty>'])
122
# Refresh wt as 'whoami' modified it
123
wt = wt.bzrdir.open_workingtree()
124
c = wt.branch.get_config_stack()
125
self.assertEquals('Changed Identity <changed@identi.ty>',
128
def test_whoami_remote_directory(self):
129
"""Test --directory option with a remote directory."""
130
wt = self.make_branch_and_tree('subdir')
131
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
132
url = self.get_readonly_url() + '/subdir'
133
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
135
url = self.get_url('subdir')
136
self.run_bzr(['whoami', '--directory', url, '--branch',
137
'Changed Identity <changed@identi.ty>'])
138
# The identity has been set in the branch config (but not the global
140
c = branch.Branch.open(url).get_config_stack()
141
self.assertEquals('Changed Identity <changed@identi.ty>',
143
# Ensuring that the value does not come from the bazaar.conf file
144
# itself requires some isolation setup
145
self.overrideEnv('BZR_EMAIL', None)
146
self.overrideEnv('EMAIL', None)
147
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
148
global_conf = config.GlobalStack()
149
self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
151
def test_whoami_nonbranch_directory(self):
152
"""Test --directory mentioning a non-branch directory."""
153
wt = self.build_tree(['subdir/'])
154
out, err = self.run_bzr("whoami --directory subdir", retcode=3)
155
self.assertContainsRe(err, 'ERROR: Not a branch')