~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

Merge cleanup into texinfo

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 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
20
20
import os
21
21
 
22
22
import bzrlib
23
 
from bzrlib.branch import Branch
24
 
from bzrlib.tests.blackbox import ExternalBase
25
 
 
26
 
 
27
 
class TestWhoami(ExternalBase):
 
23
from bzrlib import (
 
24
    osutils,
 
25
    config,
 
26
    )
 
27
from bzrlib.tests import TestCaseWithTransport
 
28
 
 
29
 
 
30
class TestWhoami(TestCaseWithTransport):
28
31
 
29
32
    def test_whoami(self):
30
33
        # this should always identify something, if only "john@localhost"
42
45
        b = bzrlib.branch.Branch.open('.')
43
46
        b.get_config().set_user_option('email',
44
47
                                       '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)
 
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)
53
52
 
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
 
53
        # Verify that the environment variable overrides the value
 
54
        # 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)
65
60
 
66
61
    def test_whoami_utf8(self):
67
62
        """verify that an identity can be in utf-8."""
68
63
        wt = self.make_branch_and_tree('.')
69
64
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
70
65
                     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
 
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)
84
71
 
85
72
    def test_whoami_ascii(self):
86
73
        """
91
78
        b = bzrlib.branch.Branch.open('.')
92
79
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
93
80
                                       '<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
 
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)
106
85
 
107
86
    def test_warning(self):
108
87
        """verify that a warning is displayed if no email is given."""
111
90
        self.assertEquals('"Branch Identity" does not seem to contain an '
112
91
                          'email address.  This is allowed, but not '
113
92
                          'recommended.\n', display)
 
93
 
 
94
    def test_whoami_not_set(self):
 
95
        """Ensure whoami error if username is not set.
 
96
        """
 
97
        osutils.set_or_unset_env('EMAIL', None)
 
98
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
99
        out, err = self.run_bzr(['whoami'], 3)
 
100
        self.assertContainsRe(err, 'Unable to determine your name')
 
101
 
 
102
    def test_whoami_directory(self):
 
103
        """Test --directory option."""
 
104
        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)
 
109
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
 
110
                      'Changed Identity <changed@identi.ty>'])
 
111
        self.assertEquals('Changed Identity <changed@identi.ty>',
 
112
                          c.get_user_option('email'))
 
113
 
 
114
    def test_whoami_remote_directory(self):
 
115
        """Test --directory option with a remote directory."""
 
116
        wt = self.make_branch_and_tree('subdir')
 
117
        c = wt.branch.get_config()
 
118
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
119
        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)
 
122
        url = self.get_url('subdir')
 
123
        self.run_bzr(['whoami', '--directory', url, '--branch',
 
124
                      'Changed Identity <changed@identi.ty>'])
 
125
        # The identity has been set in the branch config (but not the global
 
126
        # 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'))
 
131
 
 
132
    def test_whoami_nonbranch_directory(self):
 
133
        """Test --directory mentioning a non-branch directory."""
 
134
        wt = self.build_tree(['subdir/'])
 
135
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
 
136
        self.assertContainsRe(err, 'ERROR: Not a branch')