~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2009, 2010, 2011 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
"""Black-box tests for bzr whoami."""
19
19
 
20
 
import os
21
 
 
22
20
import bzrlib
23
 
from bzrlib.branch import Branch
24
 
from bzrlib.tests.blackbox import ExternalBase
25
 
 
26
 
 
27
 
class TestWhoami(ExternalBase):
 
21
from bzrlib import (
 
22
    config,
 
23
    )
 
24
from bzrlib.tests import TestCaseWithTransport
 
25
 
 
26
 
 
27
class TestWhoami(TestCaseWithTransport):
28
28
 
29
29
    def test_whoami(self):
30
30
        # this should always identify something, if only "john@localhost"
35
35
        out = self.run_bzr("whoami --email")[0]
36
36
        self.assertTrue(len(out) > 0)
37
37
        self.assertEquals(1, out.count('@'))
38
 
        
 
38
 
 
39
    def test_whoami_email_arg(self):
 
40
        # whoami --email is mutually exclusive with any arguments
 
41
        out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
 
42
        self.assertEquals("", out)
 
43
 
39
44
    def test_whoami_branch(self):
40
45
        """branch specific user identity works."""
41
46
        wt = self.make_branch_and_tree('.')
42
47
        b = bzrlib.branch.Branch.open('.')
43
48
        b.get_config().set_user_option('email',
44
49
                                       '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']
48
 
        try:
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)
 
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)
53
54
 
54
 
            # Verify that the environment variable overrides the value 
55
 
            # in the file
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']
62
 
        finally:
63
 
            if bzr_email is not None:
64
 
                os.environ['BZR_EMAIL'] = bzr_email
 
55
        # Verify that the environment variable overrides the value
 
56
        # in the file
 
57
        self.overrideEnv('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)
65
62
 
66
63
    def test_whoami_utf8(self):
67
64
        """verify that an identity can be in utf-8."""
68
65
        wt = self.make_branch_and_tree('.')
69
66
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
70
67
                     encoding='utf-8')
71
 
        bzr_email = os.environ.get('BZR_EMAIL')
72
 
        if bzr_email is not None:
73
 
            del os.environ['BZR_EMAIL']
74
 
        try:
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",
79
 
                                        encoding='utf-8')[0]
80
 
            self.assertEquals('branch@identi.ty\n', whoami_email)
81
 
        finally:
82
 
            if bzr_email is not None:
83
 
                os.environ['BZR_EMAIL'] = bzr_email
 
68
        whoami = self.run_bzr("whoami", encoding='utf-8')[0]
 
69
        self.assertEquals('Branch Identity \xe2\x82\xac ' +
 
70
                          '<branch@identi.ty>\n', whoami)
 
71
        whoami_email = self.run_bzr("whoami --email", encoding='utf-8')[0]
 
72
        self.assertEquals('branch@identi.ty\n', whoami_email)
84
73
 
85
74
    def test_whoami_ascii(self):
86
75
        """
91
80
        b = bzrlib.branch.Branch.open('.')
92
81
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
93
82
                                       '<branch@identi.ty>')
94
 
        bzr_email = os.environ.get('BZR_EMAIL')
95
 
        if bzr_email is not None:
96
 
            del os.environ['BZR_EMAIL']
97
 
        try:
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",
101
 
                                        encoding='ascii')[0]
102
 
            self.assertEquals('branch@identi.ty\n', whoami_email)
103
 
        finally:
104
 
            if bzr_email is not None:
105
 
                os.environ['BZR_EMAIL'] = bzr_email
 
83
        whoami = self.run_bzr("whoami", encoding='ascii')[0]
 
84
        self.assertEquals('Branch Identity ? <branch@identi.ty>\n', whoami)
 
85
        whoami_email = self.run_bzr("whoami --email", encoding='ascii')[0]
 
86
        self.assertEquals('branch@identi.ty\n', whoami_email)
106
87
 
107
88
    def test_warning(self):
108
89
        """verify that a warning is displayed if no email is given."""
111
92
        self.assertEquals('"Branch Identity" does not seem to contain an '
112
93
                          'email address.  This is allowed, but not '
113
94
                          'recommended.\n', display)
 
95
 
 
96
    def test_whoami_not_set(self):
 
97
        """Ensure whoami error if username is not set and not inferred.
 
98
        """
 
99
        self.overrideEnv('EMAIL', None)
 
100
        self.overrideEnv('BZR_EMAIL', None)
 
101
        # Also, make sure that it's not inferred from mailname.
 
102
        self.overrideAttr(config, '_auto_user_id',
 
103
            lambda: (None, None))
 
104
        out, err = self.run_bzr(['whoami'], 3)
 
105
        self.assertContainsRe(err, 'Unable to determine your name')
 
106
 
 
107
    def test_whoami_directory(self):
 
108
        """Test --directory option."""
 
109
        wt = self.make_branch_and_tree('subdir')
 
110
        c = wt.branch.get_config()
 
111
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
112
        out, err = self.run_bzr("whoami --directory subdir")
 
113
        self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
 
114
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
 
115
                      'Changed Identity <changed@identi.ty>'])
 
116
        self.assertEquals('Changed Identity <changed@identi.ty>',
 
117
                          c.get_user_option('email'))
 
118
 
 
119
    def test_whoami_remote_directory(self):
 
120
        """Test --directory option with a remote directory."""
 
121
        wt = self.make_branch_and_tree('subdir')
 
122
        c = wt.branch.get_config()
 
123
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
124
        url = self.get_readonly_url() + '/subdir'
 
125
        out, err = self.run_bzr(['whoami', '--directory', url])
 
126
        self.assertEquals('Branch Identity <branch@identi.ty>\n', out)
 
127
        url = self.get_url('subdir')
 
128
        self.run_bzr(['whoami', '--directory', url, '--branch',
 
129
                      'Changed Identity <changed@identi.ty>'])
 
130
        # The identity has been set in the branch config (but not the global
 
131
        # config)
 
132
        self.assertEquals('Changed Identity <changed@identi.ty>',
 
133
                          c.get_user_option('email'))
 
134
        global_conf = config.GlobalConfig()
 
135
        self.assertEquals(None, global_conf.get_user_option('email'))
 
136
 
 
137
    def test_whoami_nonbranch_directory(self):
 
138
        """Test --directory mentioning a non-branch directory."""
 
139
        wt = self.build_tree(['subdir/'])
 
140
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
 
141
        self.assertContainsRe(err, 'ERROR: Not a branch')