15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Black-box tests for bzr whoami."""
23
from bzrlib import osutils
24
from bzrlib.branch import Branch
25
from bzrlib.tests.blackbox import ExternalBase
28
class TestWhoami(ExternalBase):
30
def test_whoami(self):
18
"""Black-box tests for brz whoami."""
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.assertEqual('', err)
34
lines = out.splitlines()
35
self.assertLength(1, lines)
36
self.assertEqual(expected, lines[0].rstrip())
38
def test_whoami_no_args_no_conf(self):
31
39
# this should always identify something, if only "john@localhost"
32
40
out = self.run_bzr("whoami")[0]
33
41
self.assertTrue(len(out) > 0)
34
self.assertEquals(1, out.count('@'))
42
self.assertEqual(1, out.count('@'))
44
def test_whoami_email_no_args(self):
36
45
out = self.run_bzr("whoami --email")[0]
37
46
self.assertTrue(len(out) > 0)
38
self.assertEquals(1, out.count('@'))
47
self.assertEqual(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.assertEqual("", out)
54
def set_branch_email(self, b, email):
55
b.get_config_stack().set('email', email)
40
57
def test_whoami_branch(self):
41
58
"""branch specific user identity works."""
42
59
wt = self.make_branch_and_tree('.')
43
b = bzrlib.branch.Branch.open('.')
44
b.get_config().set_user_option('email',
45
'Branch Identity <branch@identi.ty>')
46
bzr_email = os.environ.get('BZR_EMAIL')
47
if bzr_email is not None:
48
del os.environ['BZR_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)
60
b = breezy.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')
55
# Verify that the environment variable overrides the value
57
os.environ['BZR_EMAIL'] = 'Different ID <other@environ.ment>'
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)
62
del os.environ['BZR_EMAIL']
64
if bzr_email is not None:
65
os.environ['BZR_EMAIL'] = bzr_email
65
# Verify that the environment variable overrides the value
67
self.overrideEnv('BRZ_EMAIL', 'Different ID <other@environ.ment>')
68
self.assertWhoAmI('Different ID <other@environ.ment>')
69
self.assertWhoAmI('other@environ.ment', '--email')
67
71
def test_whoami_utf8(self):
68
72
"""verify that an identity can be in utf-8."""
69
wt = self.make_branch_and_tree('.')
70
73
self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
72
bzr_email = os.environ.get('BZR_EMAIL')
73
if bzr_email is not None:
74
del os.environ['BZR_EMAIL']
76
whoami = self.run_bzr("whoami", encoding='utf-8')[0]
77
self.assertEquals('Branch Identity \xe2\x82\xac ' +
78
'<branch@identi.ty>\n', whoami)
79
whoami_email = self.run_bzr("whoami --email",
81
self.assertEquals('branch@identi.ty\n', whoami_email)
83
if bzr_email is not None:
84
os.environ['BZR_EMAIL'] = bzr_email
75
self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
77
self.assertWhoAmI('branch@identi.ty', '--email')
86
79
def test_whoami_ascii(self):
91
84
wt = self.make_branch_and_tree('.')
92
b = bzrlib.branch.Branch.open('.')
93
b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
95
bzr_email = os.environ.get('BZR_EMAIL')
96
if bzr_email is not None:
97
del os.environ['BZR_EMAIL']
99
whoami = self.run_bzr("whoami", encoding='ascii')[0]
100
self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
101
whoami_email = self.run_bzr("whoami --email",
103
self.assertEquals('branch@identi.ty\n', whoami_email)
105
if bzr_email is not None:
106
os.environ['BZR_EMAIL'] = bzr_email
85
b = breezy.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',
108
92
def test_warning(self):
109
93
"""verify that a warning is displayed if no email is given."""
110
94
self.make_branch_and_tree('.')
111
95
display = self.run_bzr(['whoami', 'Branch Identity'])[1]
112
self.assertEquals('"Branch Identity" does not seem to contain an '
96
self.assertEqual('"Branch Identity" does not seem to contain an '
113
97
'email address. This is allowed, but not '
114
98
'recommended.\n', display)
116
100
def test_whoami_not_set(self):
117
"""Ensure whoami error if username is not set.
101
"""Ensure whoami error if username is not set and not inferred.
119
osutils.set_or_unset_env('EMAIL', None)
120
osutils.set_or_unset_env('BZR_EMAIL', None)
103
self.overrideEnv('EMAIL', None)
104
self.overrideEnv('BRZ_EMAIL', None)
105
# Also, make sure that it's not inferred from mailname.
106
self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
121
107
out, err = self.run_bzr(['whoami'], 3)
122
108
self.assertContainsRe(err, 'Unable to determine your name')
110
def test_whoami_directory(self):
111
"""Test --directory option."""
112
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')
116
self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
117
'Changed Identity <changed@identi.ty>'])
118
# Refresh wt as 'whoami' modified it
119
wt = wt.controldir.open_workingtree()
120
c = wt.branch.get_config_stack()
121
self.assertEqual('Changed Identity <changed@identi.ty>',
124
def test_whoami_remote_directory(self):
125
"""Test --directory option with a remote directory."""
126
wt = self.make_branch_and_tree('subdir')
127
self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
128
url = self.get_readonly_url() + '/subdir'
129
self.assertWhoAmI('Branch Identity <branch@identi.ty>',
131
url = self.get_url('subdir')
132
self.run_bzr(['whoami', '--directory', url, '--branch',
133
'Changed Identity <changed@identi.ty>'])
134
# The identity has been set in the branch config (but not the global
136
c = branch.Branch.open(url).get_config_stack()
137
self.assertEqual('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('BRZ_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(config.NoWhoami, global_conf.get, 'email')
147
def test_whoami_nonbranch_directory(self):
148
"""Test --directory mentioning a non-branch directory."""
149
wt = self.build_tree(['subdir/'])
150
out, err = self.run_bzr("whoami --directory subdir", retcode=3)
151
self.assertContainsRe(err, 'ERROR: Not a branch')