21
21
from bzrlib import (
24
from bzrlib.tests import TestCaseWithTransport
27
class TestWhoami(TestCaseWithTransport):
29
def test_whoami(self):
28
class TestWhoami(tests.TestCaseWithTransport):
30
def assertWhoAmI(self, expected, *cmd_args, **kwargs):
31
out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
32
self.assertEquals('', err)
33
lines = out.splitlines()
34
self.assertLength(1, lines)
35
self.assertEquals(expected, lines[0].rstrip())
37
def test_whoami_no_args_no_conf(self):
30
38
# this should always identify something, if only "john@localhost"
31
39
out = self.run_bzr("whoami")[0]
32
40
self.assertTrue(len(out) > 0)
33
41
self.assertEquals(1, out.count('@'))
43
def test_whoami_email_no_args(self):
35
44
out = self.run_bzr("whoami --email")[0]
36
45
self.assertTrue(len(out) > 0)
37
46
self.assertEquals(1, out.count('@'))
45
54
"""branch specific user identity works."""
46
55
wt = self.make_branch_and_tree('.')
47
56
b = bzrlib.branch.Branch.open('.')
48
b.get_config().set_user_option('email',
49
'Branch Identity <branch@identi.ty>')
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)
57
b.get_config_stack().set('email', 'Branch Identity <branch@identi.ty>')
58
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
self.assertWhoAmI('branch@identi.ty', '--email')
55
61
# Verify that the environment variable overrides the value
57
63
self.overrideEnv('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)
64
self.assertWhoAmI('Different ID <other@environ.ment>')
65
self.assertWhoAmI('other@environ.ment', '--email')
63
67
def test_whoami_utf8(self):
64
68
"""verify that an identity can be in utf-8."""
65
wt = self.make_branch_and_tree('.')
66
69
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
68
whoami = self.run_bzr("whoami", encoding='utf-8')[0]
69
self.assertEquals('Branch Identity \xe2\x82\xac ' +
70
'<branch@identi.ty>\n', whoami)
71
whoami_email = self.run_bzr("whoami --email", encoding='utf-8')[0]
72
self.assertEquals('branch@identi.ty\n', whoami_email)
71
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
73
self.assertWhoAmI('branch@identi.ty', '--email')
74
75
def test_whoami_ascii(self):
79
80
wt = self.make_branch_and_tree('.')
80
81
b = bzrlib.branch.Branch.open('.')
81
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
whoami = self.run_bzr("whoami", encoding='ascii')[0]
84
self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
85
whoami_email = self.run_bzr("whoami --email", encoding='ascii')[0]
86
self.assertEquals('branch@identi.ty\n', whoami_email)
82
b.get_config_stack().set(
83
'email', u'Branch Identity \u20ac ' + '<branch@identi.ty>')
84
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
86
self.assertWhoAmI('branch@identi.ty', '--email',
88
89
def test_warning(self):
89
90
"""verify that a warning is displayed if no email is given."""
107
108
def test_whoami_directory(self):
108
109
"""Test --directory option."""
109
110
wt = self.make_branch_and_tree('subdir')
110
c = wt.branch.get_config()
111
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
112
out, err = self.run_bzr("whoami --directory subdir")
113
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
111
c = wt.branch.get_config_stack()
112
c.set('email', 'Branch Identity <branch@identi.ty>')
113
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
'--directory', 'subdir')
114
115
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
115
116
'Changed Identity <changed@identi.ty>'])
117
c = wt.branch.get_config_stack()
116
118
self.assertEquals('Changed Identity <changed@identi.ty>',
117
c.get_user_option('email'))
119
121
def test_whoami_remote_directory(self):
120
122
"""Test --directory option with a remote directory."""
121
123
wt = self.make_branch_and_tree('subdir')
122
c = wt.branch.get_config()
123
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
124
c = wt.branch.get_config_stack()
125
c.set('email', 'Branch Identity <branch@identi.ty>')
124
126
url = self.get_readonly_url() + '/subdir'
125
out, err = self.run_bzr(['whoami', '--directory', url])
126
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
127
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
127
129
url = self.get_url('subdir')
128
130
self.run_bzr(['whoami', '--directory', url, '--branch',
129
131
'Changed Identity <changed@identi.ty>'])
130
132
# The identity has been set in the branch config (but not the global
134
c = wt.branch.get_config_stack()
132
135
self.assertEquals('Changed Identity <changed@identi.ty>',
133
c.get_user_option('email'))
134
global_conf = config.GlobalConfig()
135
self.assertEquals(None, global_conf.get_user_option('email'))
137
# Ensuring that the value does not come from the bazaar.conf file
138
# itself requires some isolation setup
139
self.overrideEnv('BZR_EMAIL', None)
140
self.overrideEnv('EMAIL', None)
141
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
142
global_conf = config.GlobalStack()
143
self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
137
145
def test_whoami_nonbranch_directory(self):
138
146
"""Test --directory mentioning a non-branch directory."""