~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2012-01-18 14:09:19 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120118140919-rlvdrhpc0nq1lbwi
Change set/remove to require a lock for the branch config files.

This means that tests (or any plugin for that matter) do not requires an
explicit lock on the branch anymore to change a single option. This also
means the optimisation becomes "opt-in" and as such won't be as
spectacular as it may be and/or harder to get right (nothing fails
anymore).

This reduces the diff by ~300 lines.

Code/tests that were updating more than one config option is still taking
a lock to at least avoid some IOs and demonstrate the benefits through
the decreased number of hpss calls.

The duplication between BranchStack and BranchOnlyStack will be removed
once the same sharing is in place for local config files, at which point
the Stack class itself may be able to host the changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 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
 
from bzrlib.branch import Branch
24
 
from bzrlib.tests.blackbox import ExternalBase
25
 
 
26
 
 
27
 
class TestWhoami(ExternalBase):
28
 
 
29
 
    def test_whoami(self):
 
21
from bzrlib import (
 
22
    branch,
 
23
    config,
 
24
    errors,
 
25
    tests,
 
26
    )
 
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.assertEquals('', err)
 
34
        lines = out.splitlines()
 
35
        self.assertLength(1, lines)
 
36
        self.assertEquals(expected, lines[0].rstrip())
 
37
 
 
38
    def test_whoami_no_args_no_conf(self):
30
39
        # this should always identify something, if only "john@localhost"
31
40
        out = self.run_bzr("whoami")[0]
32
41
        self.assertTrue(len(out) > 0)
33
42
        self.assertEquals(1, out.count('@'))
34
43
 
 
44
    def test_whoami_email_no_args(self):
35
45
        out = self.run_bzr("whoami --email")[0]
36
46
        self.assertTrue(len(out) > 0)
37
47
        self.assertEquals(1, out.count('@'))
38
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.assertEquals("", out)
 
53
 
 
54
    def set_branch_email(self, b, email):
 
55
        b.get_config_stack().set('email', email)
 
56
 
39
57
    def test_whoami_branch(self):
40
58
        """branch specific user identity works."""
41
59
        wt = self.make_branch_and_tree('.')
42
60
        b = bzrlib.branch.Branch.open('.')
43
 
        b.get_config().set_user_option('email',
44
 
                                       '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)
 
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')
53
64
 
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
 
65
        # Verify that the environment variable overrides the value
 
66
        # in the file
 
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')
65
70
 
66
71
    def test_whoami_utf8(self):
67
72
        """verify that an identity can be in utf-8."""
68
 
        wt = self.make_branch_and_tree('.')
69
73
        self.run_bzr(['whoami', u'Branch Identity \u20ac <branch@identi.ty>'],
70
74
                     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
 
75
        self.assertWhoAmI('Branch Identity \xe2\x82\xac <branch@identi.ty>',
 
76
                          encoding='utf-8')
 
77
        self.assertWhoAmI('branch@identi.ty', '--email')
84
78
 
85
79
    def test_whoami_ascii(self):
86
80
        """
89
83
        """
90
84
        wt = self.make_branch_and_tree('.')
91
85
        b = bzrlib.branch.Branch.open('.')
92
 
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
93
 
                                       '<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
 
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')
106
91
 
107
92
    def test_warning(self):
108
93
        """verify that a warning is displayed if no email is given."""
111
96
        self.assertEquals('"Branch Identity" does not seem to contain an '
112
97
                          'email address.  This is allowed, but not '
113
98
                          'recommended.\n', display)
 
99
 
 
100
    def test_whoami_not_set(self):
 
101
        """Ensure whoami error if username is not set and not inferred.
 
102
        """
 
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))
 
107
        out, err = self.run_bzr(['whoami'], 3)
 
108
        self.assertContainsRe(err, 'Unable to determine your name')
 
109
 
 
110
    def test_whoami_directory(self):
 
111
        """Test --directory option."""
 
112
        wt = self.make_branch_and_tree('subdir')
 
113
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
 
114
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
115
                          '--directory', 'subdir')
 
116
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
 
117
                      'Changed Identity <changed@identi.ty>'])
 
118
        # Refresh wt as 'whoami' modified it
 
119
        wt = wt.bzrdir.open_workingtree()
 
120
        c = wt.branch.get_config_stack()
 
121
        self.assertEquals('Changed Identity <changed@identi.ty>',
 
122
                          c.get('email'))
 
123
 
 
124
    def test_whoami_remote_directory(self):
 
125
        """Test --directory option with a remote directory."""
 
126
        wt = self.make_branch_and_tree('subdir')
 
127
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
 
128
        url = self.get_readonly_url() + '/subdir'
 
129
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
 
130
                          '--directory', url)
 
131
        url = self.get_url('subdir')
 
132
        self.run_bzr(['whoami', '--directory', url, '--branch',
 
133
                      'Changed Identity <changed@identi.ty>'])
 
134
        # The identity has been set in the branch config (but not the global
 
135
        # config)
 
136
        c = branch.Branch.open(url).get_config_stack()
 
137
        self.assertEquals('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')
 
146
 
 
147
    def test_whoami_nonbranch_directory(self):
 
148
        """Test --directory mentioning a non-branch directory."""
 
149
        wt = self.build_tree(['subdir/'])
 
150
        out, err = self.run_bzr("whoami --directory subdir", retcode=3)
 
151
        self.assertContainsRe(err, 'ERROR: Not a branch')