~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_whoami.py

  • Committer: Patch Queue Manager
  • Date: 2016-04-21 04:10:52 UTC
  • mfrom: (6616.1.1 fix-en-user-guide)
  • Revision ID: pqm@pqm.ubuntu.com-20160421041052-clcye7ns1qcl2n7w
(richard-wilbur) Ensure build of English use guide always uses English text
 even when user's locale specifies a different language. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009-2012, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
17
17
 
18
18
"""Black-box tests for bzr whoami."""
19
19
 
20
 
import os
21
 
 
22
20
import bzrlib
23
21
from bzrlib import (
24
 
    osutils,
 
22
    branch,
25
23
    config,
 
24
    errors,
 
25
    tests,
26
26
    )
27
 
from bzrlib.tests import TestCaseWithTransport
28
 
 
29
 
 
30
 
class TestWhoami(TestCaseWithTransport):
31
 
 
32
 
    def test_whoami(self):
 
27
 
 
28
 
 
29
class TestWhoami(tests.TestCaseWithTransport):
 
30
 
 
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())
 
37
 
 
38
    def test_whoami_no_args_no_conf(self):
33
39
        # this should always identify something, if only "john@localhost"
34
40
        out = self.run_bzr("whoami")[0]
35
41
        self.assertTrue(len(out) > 0)
36
 
        self.assertEquals(1, out.count('@'))
 
42
        self.assertEqual(1, out.count('@'))
37
43
 
 
44
    def test_whoami_email_no_args(self):
38
45
        out = self.run_bzr("whoami --email")[0]
39
46
        self.assertTrue(len(out) > 0)
40
 
        self.assertEquals(1, out.count('@'))
 
47
        self.assertEqual(1, out.count('@'))
 
48
 
 
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)
 
53
 
 
54
    def set_branch_email(self, b, email):
 
55
        b.get_config_stack().set('email', email)
41
56
 
42
57
    def test_whoami_branch(self):
43
58
        """branch specific user identity works."""
44
59
        wt = self.make_branch_and_tree('.')
45
60
        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)
 
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')
52
64
 
53
65
        # Verify that the environment variable overrides the value
54
66
        # in the file
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
        self.overrideEnv('BZR_EMAIL', 'Different ID <other@environ.ment>')
 
68
        self.assertWhoAmI('Different ID <other@environ.ment>')
 
69
        self.assertWhoAmI('other@environ.ment', '--email')
60
70
 
61
71
    def test_whoami_utf8(self):
62
72
        """verify that an identity can be in utf-8."""
63
 
        wt = self.make_branch_and_tree('.')
64
73
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
65
74
                     encoding='utf-8')
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
        self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
 
76
                          encoding='utf-8')
 
77
        self.assertWhoAmI('branch@identi.ty', '--email')
71
78
 
72
79
    def test_whoami_ascii(self):
73
80
        """
76
83
        """
77
84
        wt = self.make_branch_and_tree('.')
78
85
        b = bzrlib.branch.Branch.open('.')
79
 
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
80
 
                                       '<branch@identi.ty>')
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)
 
86
        self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
 
87
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
 
88
                          encoding='ascii')
 
89
        self.assertWhoAmI('branch@identi.ty', '--email',
 
90
                          encoding='ascii')
85
91
 
86
92
    def test_warning(self):
87
93
        """verify that a warning is displayed if no email is given."""
88
94
        self.make_branch_and_tree('.')
89
95
        display = self.run_bzr(['whoami', 'Branch Identity'])[1]
90
 
        self.assertEquals('"Branch Identity" does not seem to contain an '
 
96
        self.assertEqual('"Branch Identity" does not seem to contain an '
91
97
                          'email address.  This is allowed, but not '
92
98
                          'recommended.\n', display)
93
99
 
94
100
    def test_whoami_not_set(self):
95
 
        """Ensure whoami error if username is not set.
 
101
        """Ensure whoami error if username is not set and not inferred.
96
102
        """
97
 
        osutils.set_or_unset_env('EMAIL', None)
98
 
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
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))
99
107
        out, err = self.run_bzr(['whoami'], 3)
100
108
        self.assertContainsRe(err, 'Unable to determine your name')
101
109
 
102
110
    def test_whoami_directory(self):
103
111
        """Test --directory option."""
104
112
        wt = self.make_branch_and_tree('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)
 
113
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
 
114
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
115
                          '--directory', 'subdir')
109
116
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
110
117
                      'Changed Identity <changed@identi.ty>'])
111
 
        self.assertEquals('Changed Identity <changed@identi.ty>',
112
 
                          c.get_user_option('email'))
 
118
        # Refresh wt as 'whoami' modified it
 
119
        wt = wt.bzrdir.open_workingtree()
 
120
        c = wt.branch.get_config_stack()
 
121
        self.assertEqual('Changed Identity <changed@identi.ty>',
 
122
                          c.get('email'))
113
123
 
114
124
    def test_whoami_remote_directory(self):
115
125
        """Test --directory option with a remote directory."""
116
126
        wt = self.make_branch_and_tree('subdir')
117
 
        c = wt.branch.get_config()
118
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
127
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
119
128
        url = self.get_readonly_url() + '/subdir'
120
 
        out, err = self.run_bzr(['whoami', '--directory', url])
121
 
        self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
 
129
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
130
                          '--directory', url)
122
131
        url = self.get_url('subdir')
123
132
        self.run_bzr(['whoami', '--directory', url, '--branch',
124
133
                      'Changed Identity <changed@identi.ty>'])
125
134
        # The identity has been set in the branch config (but not the global
126
135
        # config)
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'))
 
136
        c = branch.Branch.open(url).get_config_stack()
 
137
        self.assertEqual('Changed Identity <changed@identi.ty>',
 
138
                          c.get('email'))
 
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')
131
146
 
132
147
    def test_whoami_nonbranch_directory(self):
133
148
        """Test --directory mentioning a non-branch directory."""