15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Black-box tests for brz whoami."""
18
"""Black-box tests for bzr whoami."""
26
from breezy.sixish import PY3
28
from ..test_bedding import override_whoami
31
class TestWhoami(tests.TestCaseWithTransport):
33
def assertWhoAmI(self, expected, *cmd_args, **kwargs):
34
out, err = self.run_bzr(('whoami',) + cmd_args, **kwargs)
35
self.assertEqual('', err)
36
lines = out.splitlines()
37
self.assertLength(1, lines)
38
if PY3 and isinstance(expected, bytes):
39
expected = expected.decode(kwargs.get('encoding', 'ascii'))
40
self.assertEqual(expected, lines[0].rstrip())
42
def test_whoami_no_args_no_conf(self):
27
from bzrlib.tests import TestCaseWithTransport
30
class TestWhoami(TestCaseWithTransport):
32
def test_whoami(self):
43
33
# this should always identify something, if only "john@localhost"
44
34
out = self.run_bzr("whoami")[0]
45
35
self.assertTrue(len(out) > 0)
46
self.assertEqual(1, out.count('@'))
36
self.assertEquals(1, out.count('@'))
48
def test_whoami_email_no_args(self):
49
38
out = self.run_bzr("whoami --email")[0]
50
39
self.assertTrue(len(out) > 0)
51
self.assertEqual(1, out.count('@'))
53
def test_whoami_email_arg(self):
54
# whoami --email is mutually exclusive with any arguments
55
out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
56
self.assertEqual("", out)
58
def set_branch_email(self, b, email):
59
b.get_config_stack().set('email', email)
40
self.assertEquals(1, out.count('@'))
61
42
def test_whoami_branch(self):
62
43
"""branch specific user identity works."""
63
44
wt = self.make_branch_and_tree('.')
64
b = branch.Branch.open('.')
65
self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
66
self.assertWhoAmI('Branch Identity <branch@identi.ty>')
67
self.assertWhoAmI('branch@identi.ty', '--email')
45
b = bzrlib.branch.Branch.open('.')
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)
69
53
# Verify that the environment variable overrides the value
71
self.overrideEnv('BRZ_EMAIL', 'Different ID <other@environ.ment>')
72
self.assertWhoAmI('Different ID <other@environ.ment>')
73
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)
75
61
def test_whoami_utf8(self):
76
62
"""verify that an identity can be in utf-8."""
63
wt = self.make_branch_and_tree('.')
77
64
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
79
self.assertWhoAmI(b'Branch Identity \xe2\x82\xac <branch@identi.ty>',
81
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)
83
72
def test_whoami_ascii(self):
88
77
wt = self.make_branch_and_tree('.')
89
b = branch.Branch.open('.')
90
self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
91
self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
93
self.assertWhoAmI('branch@identi.ty', '--email',
78
b = bzrlib.branch.Branch.open('.')
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)
96
86
def test_warning(self):
97
87
"""verify that a warning is displayed if no email is given."""
98
88
self.make_branch_and_tree('.')
99
89
display = self.run_bzr(['whoami', 'Branch Identity'])[1]
100
self.assertEqual('"Branch Identity" does not seem to contain an '
101
'email address. This is allowed, but not '
102
'recommended.\n', display)
90
self.assertEquals('"Branch Identity" does not seem to contain an '
91
'email address. This is allowed, but not '
92
'recommended.\n', display)
104
94
def test_whoami_not_set(self):
105
"""Ensure whoami error if username is not set and not inferred.
95
"""Ensure whoami error if username is not set.
107
override_whoami(self)
97
osutils.set_or_unset_env('EMAIL', None)
98
osutils.set_or_unset_env('BZR_EMAIL', None)
108
99
out, err = self.run_bzr(['whoami'], 3)
109
100
self.assertContainsRe(err, 'Unable to determine your name')
111
102
def test_whoami_directory(self):
112
103
"""Test --directory option."""
113
104
wt = self.make_branch_and_tree('subdir')
114
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
115
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
116
'--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)
117
109
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
118
110
'Changed Identity <changed@identi.ty>'])
119
# Refresh wt as 'whoami' modified it
120
wt = wt.controldir.open_workingtree()
121
c = wt.branch.get_config_stack()
122
self.assertEqual('Changed Identity <changed@identi.ty>',
111
self.assertEquals('Changed Identity <changed@identi.ty>',
112
c.get_user_option('email'))
125
114
def test_whoami_remote_directory(self):
126
115
"""Test --directory option with a remote directory."""
127
116
wt = self.make_branch_and_tree('subdir')
128
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>')
129
119
url = self.get_readonly_url() + '/subdir'
130
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)
132
122
url = self.get_url('subdir')
133
123
self.run_bzr(['whoami', '--directory', url, '--branch',
134
124
'Changed Identity <changed@identi.ty>'])
135
125
# The identity has been set in the branch config (but not the global
137
c = branch.Branch.open(url).get_config_stack()
138
self.assertEqual('Changed Identity <changed@identi.ty>',
140
# Ensuring that the value does not come from the breezy.conf file
141
# itself requires some isolation setup
142
override_whoami(self)
143
global_conf = config.GlobalStack()
144
self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
127
self.assertEquals('Changed Identity <changed@identi.ty>',
128
c.get_user_option('email'))
129
global_conf = config.GlobalConfig()
130
self.assertEquals(None, global_conf.get_user_option('email'))
146
132
def test_whoami_nonbranch_directory(self):
147
133
"""Test --directory mentioning a non-branch directory."""