21
21
from bzrlib import (
27
class TestWhoami(tests.TestCaseWithTransport):
29
def assertWhoAmI(self, expected, *cmd_args, **kwargs):
30
out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
31
self.assertEquals('', err)
32
lines = out.splitlines()
33
self.assertLength(1, lines)
34
self.assertEquals(expected, lines[0].rstrip())
36
def test_whoami_no_args_no_conf(self):
24
from bzrlib.tests import TestCaseWithTransport
27
class TestWhoami(TestCaseWithTransport):
29
def test_whoami(self):
37
30
# this should always identify something, if only "john@localhost"
38
31
out = self.run_bzr("whoami")[0]
39
32
self.assertTrue(len(out) > 0)
40
33
self.assertEquals(1, out.count('@'))
42
def test_whoami_email_no_args(self):
43
35
out = self.run_bzr("whoami --email")[0]
44
36
self.assertTrue(len(out) > 0)
45
37
self.assertEquals(1, out.count('@'))
55
47
b = bzrlib.branch.Branch.open('.')
56
48
b.get_config().set_user_option('email',
57
49
'Branch Identity <branch@identi.ty>')
58
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
self.assertWhoAmI('branch@identi.ty', '--email')
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)
61
55
# Verify that the environment variable overrides the value
63
57
self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
64
self.assertWhoAmI('Different ID <other@environ.ment>')
65
self.assertWhoAmI('other@environ.ment', '--email')
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)
67
63
def test_whoami_utf8(self):
68
64
"""verify that an identity can be in utf-8."""
65
wt = self.make_branch_and_tree('.')
69
66
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
71
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
73
self.assertWhoAmI('branch@identi.ty', '--email')
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)
75
74
def test_whoami_ascii(self):
81
80
b = bzrlib.branch.Branch.open('.')
82
81
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
82
'<branch@identi.ty>')
84
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
86
self.assertWhoAmI('branch@identi.ty', '--email',
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)
89
88
def test_warning(self):
90
89
"""verify that a warning is displayed if no email is given."""
95
94
'recommended.\n', display)
97
96
def test_whoami_not_set(self):
98
"""Ensure whoami error if username is not set and not inferred.
97
"""Ensure whoami error if username is not set.
100
99
self.overrideEnv('EMAIL', None)
101
100
self.overrideEnv('BZR_EMAIL', None)
102
# Also, make sure that it's not inferred from mailname.
103
self.overrideAttr(config, '_auto_user_id',
104
lambda: (None, None))
105
101
out, err = self.run_bzr(['whoami'], 3)
106
102
self.assertContainsRe(err, 'Unable to determine your name')
110
106
wt = self.make_branch_and_tree('subdir')
111
107
c = wt.branch.get_config()
112
108
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
113
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
'--directory', 'subdir')
109
out, err = self.run_bzr("whoami --directory subdir")
110
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
115
111
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
112
'Changed Identity <changed@identi.ty>'])
117
c = wt.branch.get_config()
118
113
self.assertEquals('Changed Identity <changed@identi.ty>',
119
114
c.get_user_option('email'))
124
119
c = wt.branch.get_config()
125
120
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
126
121
url = self.get_readonly_url() + '/subdir'
127
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
122
out, err = self.run_bzr(['whoami', '--directory', url])
123
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
129
124
url = self.get_url('subdir')
130
125
self.run_bzr(['whoami', '--directory', url, '--branch',
131
126
'Changed Identity <changed@identi.ty>'])
132
127
# The identity has been set in the branch config (but not the global
134
c = wt.branch.get_config()
135
129
self.assertEquals('Changed Identity <changed@identi.ty>',
136
130
c.get_user_option('email'))
137
131
global_conf = config.GlobalConfig()