18
18
"""Black-box tests for bzr whoami."""
21
23
from bzrlib import (
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):
27
from bzrlib.tests import TestCaseWithTransport
30
class TestWhoami(TestCaseWithTransport):
32
def test_whoami(self):
39
33
# this should always identify something, if only "john@localhost"
40
34
out = self.run_bzr("whoami")[0]
41
35
self.assertTrue(len(out) > 0)
42
36
self.assertEquals(1, out.count('@'))
44
def test_whoami_email_no_args(self):
45
38
out = self.run_bzr("whoami --email")[0]
46
39
self.assertTrue(len(out) > 0)
47
40
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):
55
b.get_config_stack().set('email', email)
57
42
def test_whoami_branch(self):
58
43
"""branch specific user identity works."""
59
44
wt = self.make_branch_and_tree('.')
60
45
b = bzrlib.branch.Branch.open('.')
61
self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
62
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
63
self.assertWhoAmI('branch@identi.ty', '--email')
46
b.get_config().set_user_option('email',
47
'Branch Identity <branch@identi.ty>')
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)
65
53
# Verify that the environment variable overrides the value
67
self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
68
self.assertWhoAmI('Different ID <other@environ.ment>')
69
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)
71
61
def test_whoami_utf8(self):
72
62
"""verify that an identity can be in utf-8."""
63
wt = self.make_branch_and_tree('.')
73
64
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
75
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
77
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)
79
72
def test_whoami_ascii(self):
84
77
wt = self.make_branch_and_tree('.')
85
78
b = bzrlib.branch.Branch.open('.')
86
self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
87
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
89
self.assertWhoAmI('branch@identi.ty', '--email',
79
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
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)
92
86
def test_warning(self):
93
87
"""verify that a warning is displayed if no email is given."""
98
92
'recommended.\n', display)
100
94
def test_whoami_not_set(self):
101
"""Ensure whoami error if username is not set and not inferred.
95
"""Ensure whoami error if username is not set.
103
self.overrideEnv('EMAIL', None)
104
self.overrideEnv('BZR_EMAIL', None)
105
# Also, make sure that it's not inferred from mailname.
106
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
97
osutils.set_or_unset_env('EMAIL', None)
98
osutils.set_or_unset_env('BZR_EMAIL', None)
107
99
out, err = self.run_bzr(['whoami'], 3)
108
100
self.assertContainsRe(err, 'Unable to determine your name')
110
102
def test_whoami_directory(self):
111
103
"""Test --directory option."""
112
104
wt = self.make_branch_and_tree('subdir')
113
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
114
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
115
'--directory', 'subdir')
105
c = wt.branch.get_config()
106
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
107
out, err = self.run_bzr("whoami --directory subdir")
108
self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
116
109
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
117
110
'Changed Identity <changed@identi.ty>'])
118
# Refresh wt as 'whoami' modified it
119
wt = wt.bzrdir.open_workingtree()
120
c = wt.branch.get_config_stack()
121
111
self.assertEquals('Changed Identity <changed@identi.ty>',
112
c.get_user_option('email'))
124
114
def test_whoami_remote_directory(self):
125
115
"""Test --directory option with a remote directory."""
126
116
wt = self.make_branch_and_tree('subdir')
127
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
117
c = wt.branch.get_config()
118
c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
128
119
url = self.get_readonly_url() + '/subdir'
129
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)
131
122
url = self.get_url('subdir')
132
123
self.run_bzr(['whoami', '--directory', url, '--branch',
133
124
'Changed Identity <changed@identi.ty>'])
134
125
# The identity has been set in the branch config (but not the global
136
c = branch.Branch.open(url).get_config_stack()
137
127
self.assertEquals('Changed Identity <changed@identi.ty>',
139
# Ensuring that the value does not come from the bazaar.conf file
140
# itself requires some isolation setup
141
self.overrideEnv('BZR_EMAIL', None)
142
self.overrideEnv('EMAIL', None)
143
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
144
global_conf = config.GlobalStack()
145
self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
128
c.get_user_option('email'))
129
global_conf = config.GlobalConfig()
130
self.assertEquals(None, global_conf.get_user_option('email'))
147
132
def test_whoami_nonbranch_directory(self):
148
133
"""Test --directory mentioning a non-branch directory."""