~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-08-01 20:07:49 UTC
  • mfrom: (1711.2.112 jam-integration)
  • Revision ID: pqm@pqm.ubuntu.com-20060801200749-e0bd496331bb4d2b
(bialix) Create a nice win32 installer

Show diffs side-by-side

added added

removed removed

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