18
18
"""Black-box tests for bzr whoami."""
21
23
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):
27
from bzrlib.tests import TestCaseWithTransport
30
class TestWhoami(TestCaseWithTransport):
32
def test_whoami(self):
37
33
# this should always identify something, if only "john@localhost"
38
34
out = self.run_bzr("whoami")[0]
39
35
self.assertTrue(len(out) > 0)
40
36
self.assertEquals(1, out.count('@'))
42
def test_whoami_email_no_args(self):
43
38
out = self.run_bzr("whoami --email")[0]
44
39
self.assertTrue(len(out) > 0)
45
40
self.assertEquals(1, out.count('@'))
47
def test_whoami_email_arg(self):
48
# whoami --email is mutually exclusive with any arguments
49
out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
50
self.assertEquals("", out)
52
42
def test_whoami_branch(self):
53
43
"""branch specific user identity works."""
54
44
wt = self.make_branch_and_tree('.')
55
45
b = bzrlib.branch.Branch.open('.')
56
46
b.get_config().set_user_option('email',
57
47
'Branch Identity <branch@identi.ty>')
58
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
self.assertWhoAmI('branch@identi.ty', '--email')
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)
61
53
# Verify that the environment variable overrides the value
63
self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
64
self.assertWhoAmI('Different ID <other@environ.ment>')
65
self.assertWhoAmI('other@environ.ment', '--email')
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)
67
61
def test_whoami_utf8(self):
68
62
"""verify that an identity can be in utf-8."""
63
wt = self.make_branch_and_tree('.')
69
64
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')
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)
75
72
def test_whoami_ascii(self):
81
78
b = bzrlib.branch.Branch.open('.')
82
79
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
80
'<branch@identi.ty>')
84
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
86
self.assertWhoAmI('branch@identi.ty', '--email',
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)
89
86
def test_warning(self):
90
87
"""verify that a warning is displayed if no email is given."""
95
92
'recommended.\n', display)
97
94
def test_whoami_not_set(self):
98
"""Ensure whoami error if username is not set and not inferred.
95
"""Ensure whoami error if username is not set.
100
self.overrideEnv('EMAIL', None)
101
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))
97
osutils.set_or_unset_env('EMAIL', None)
98
osutils.set_or_unset_env('BZR_EMAIL', None)
105
99
out, err = self.run_bzr(['whoami'], 3)
106
100
self.assertContainsRe(err, 'Unable to determine your name')
110
104
wt = self.make_branch_and_tree('subdir')
111
105
c = wt.branch.get_config()
112
106
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
113
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
'--directory', 'subdir')
107
out, err = self.run_bzr("whoami --directory subdir")
108
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
115
109
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
110
'Changed Identity <changed@identi.ty>'])
117
c = wt.branch.get_config()
118
111
self.assertEquals('Changed Identity <changed@identi.ty>',
119
112
c.get_user_option('email'))
124
117
c = wt.branch.get_config()
125
118
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
126
119
url = self.get_readonly_url() + '/subdir'
127
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
120
out, err = self.run_bzr(['whoami', '--directory', url])
121
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
129
122
url = self.get_url('subdir')
130
123
self.run_bzr(['whoami', '--directory', url, '--branch',
131
124
'Changed Identity <changed@identi.ty>'])
132
125
# The identity has been set in the branch config (but not the global
134
c = wt.branch.get_config()
135
127
self.assertEquals('Changed Identity <changed@identi.ty>',
136
128
c.get_user_option('email'))
137
129
global_conf = config.GlobalConfig()