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):
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):
30
37
# this should always identify something, if only "john@localhost"
31
38
out = self.run_bzr("whoami")[0]
32
39
self.assertTrue(len(out) > 0)
33
40
self.assertEquals(1, out.count('@'))
42
def test_whoami_email_no_args(self):
35
43
out = self.run_bzr("whoami --email")[0]
36
44
self.assertTrue(len(out) > 0)
37
45
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)
39
52
def test_whoami_branch(self):
40
53
"""branch specific user identity works."""
41
54
wt = self.make_branch_and_tree('.')
42
55
b = bzrlib.branch.Branch.open('.')
43
56
b.get_config().set_user_option('email',
44
57
'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)
58
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
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
61
# 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')
66
67
def test_whoami_utf8(self):
67
68
"""verify that an identity can be in utf-8."""
68
wt = self.make_branch_and_tree('.')
69
69
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
71
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
73
self.assertWhoAmI('branch@identi.ty', '--email')
85
75
def test_whoami_ascii(self):
91
81
b = bzrlib.branch.Branch.open('.')
92
82
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
93
83
'<branch@identi.ty>')
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
84
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
86
self.assertWhoAmI('branch@identi.ty', '--email',
107
89
def test_warning(self):
108
90
"""verify that a warning is displayed if no email is given."""
111
93
self.assertEquals('"Branch Identity" does not seem to contain an '
112
94
'email address. This is allowed, but not '
113
95
'recommended.\n', display)
97
def test_whoami_not_set(self):
98
"""Ensure whoami error if username is not set and not inferred.
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))
105
out, err = self.run_bzr(['whoami'], 3)
106
self.assertContainsRe(err, 'Unable to determine your name')
108
def test_whoami_directory(self):
109
"""Test --directory option."""
110
wt = self.make_branch_and_tree('subdir')
111
c = wt.branch.get_config()
112
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
113
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
'--directory', 'subdir')
115
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
'Changed Identity <changed@identi.ty>'])
117
c = wt.branch.get_config()
118
self.assertEquals('Changed Identity <changed@identi.ty>',
119
c.get_user_option('email'))
121
def test_whoami_remote_directory(self):
122
"""Test --directory option with a remote directory."""
123
wt = self.make_branch_and_tree('subdir')
124
c = wt.branch.get_config()
125
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
126
url = self.get_readonly_url() + '/subdir'
127
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
129
url = self.get_url('subdir')
130
self.run_bzr(['whoami', '--directory', url, '--branch',
131
'Changed Identity <changed@identi.ty>'])
132
# The identity has been set in the branch config (but not the global
134
c = wt.branch.get_config()
135
self.assertEquals('Changed Identity <changed@identi.ty>',
136
c.get_user_option('email'))
137
global_conf = config.GlobalConfig()
138
self.assertEquals(None, global_conf.get_user_option('email'))
140
def test_whoami_nonbranch_directory(self):
141
"""Test --directory mentioning a non-branch directory."""
142
wt = self.build_tree(['subdir/'])
143
out, err = self.run_bzr("whoami --directory subdir", retcode=3)
144
self.assertContainsRe(err, 'ERROR: Not a branch')