~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Jelmer Vernooij
  • Date: 2012-01-24 13:14:06 UTC
  • mto: (6445.4.5 nested-trees-spec)
  • mto: This revision was merged to the branch mainline in revision 6518.
  • Revision ID: jelmer@samba.org-20120124131406-wedftkorbpv37bm0
Import nested tree doc from devnotes.

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 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,
25
22
    config,
 
23
    errors,
 
24
    tests,
26
25
    )
27
 
from bzrlib.tests import TestCaseWithTransport
28
 
 
29
 
 
30
 
class TestWhoami(TestCaseWithTransport):
31
 
 
32
 
    def test_whoami(self):
 
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):
33
38
        # this should always identify something, if only "john@localhost"
34
39
        out = self.run_bzr("whoami")[0]
35
40
        self.assertTrue(len(out) > 0)
36
41
        self.assertEquals(1, out.count('@'))
37
42
 
 
43
    def test_whoami_email_no_args(self):
38
44
        out = self.run_bzr("whoami --email")[0]
39
45
        self.assertTrue(len(out) > 0)
40
46
        self.assertEquals(1, out.count('@'))
41
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
 
42
53
    def test_whoami_branch(self):
43
54
        """branch specific user identity works."""
44
55
        wt = self.make_branch_and_tree('.')
45
56
        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)
 
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')
52
60
 
53
61
        # Verify that the environment variable overrides the value
54
62
        # 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)
 
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')
60
66
 
61
67
    def test_whoami_utf8(self):
62
68
        """verify that an identity can be in utf-8."""
63
 
        wt = self.make_branch_and_tree('.')
64
69
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
65
70
                     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)
 
71
        self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
 
72
                          encoding='utf-8')
 
73
        self.assertWhoAmI('branch@identi.ty', '--email')
71
74
 
72
75
    def test_whoami_ascii(self):
73
76
        """
76
79
        """
77
80
        wt = self.make_branch_and_tree('.')
78
81
        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)
 
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')
85
88
 
86
89
    def test_warning(self):
87
90
        """verify that a warning is displayed if no email is given."""
92
95
                          'recommended.\n', display)
93
96
 
94
97
    def test_whoami_not_set(self):
95
 
        """Ensure whoami error if username is not set.
 
98
        """Ensure whoami error if username is not set and not inferred.
96
99
        """
97
 
        osutils.set_or_unset_env('EMAIL', None)
98
 
        osutils.set_or_unset_env('BZR_EMAIL', None)
 
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))
99
105
        out, err = self.run_bzr(['whoami'], 3)
100
106
        self.assertContainsRe(err, 'Unable to determine your name')
101
107
 
102
108
    def test_whoami_directory(self):
103
109
        """Test --directory option."""
104
110
        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)
 
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')
109
115
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
110
116
                      'Changed Identity <changed@identi.ty>'])
 
117
        c = wt.branch.get_config_stack()
111
118
        self.assertEquals('Changed Identity <changed@identi.ty>',
112
 
                          c.get_user_option('email'))
 
119
                          c.get('email'))
113
120
 
114
121
    def test_whoami_remote_directory(self):
115
122
        """Test --directory option with a remote directory."""
116
123
        wt = self.make_branch_and_tree('subdir')
117
 
        c = wt.branch.get_config()
118
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
124
        c = wt.branch.get_config_stack()
 
125
        c.set('email', 'Branch Identity <branch@identi.ty>')
119
126
        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)
 
127
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
128
                          '--directory', url)
122
129
        url = self.get_url('subdir')
123
130
        self.run_bzr(['whoami', '--directory', url, '--branch',
124
131
                      'Changed Identity <changed@identi.ty>'])
125
132
        # The identity has been set in the branch config (but not the global
126
133
        # config)
 
134
        c = wt.branch.get_config_stack()
127
135
        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.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')
131
144
 
132
145
    def test_whoami_nonbranch_directory(self):
133
146
        """Test --directory mentioning a non-branch directory."""