~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-05 14:26:58 UTC
  • mto: This revision was merged to the branch mainline in revision 6468.
  • Revision ID: v.ladeuil+lp@free.fr-20120105142658-vek3v6pzlxb751s2
Tests passing for a first rough version of a cached branch config store. The changes here are too invasive and several parallel proposals have been made. 

@only_raises is evil and gave a hard time since any exception during
save_changes() was swallowed.

Possible improvements: 

- add some needs_write_lock decorators to crucial
  methods (_set_config_location ?) but keep locking the branch at higher levels

- decorate branch.unlock to call stack.save if last_lock() it True
  outside of @only_raises scope (evil decorator)

- add @needs_write_lock to stack.set and stack.remove (will probably get
  rid of most testing issues) we probably need a specialized decorator
  that can relay to the store and from there to the branch or whatever is
  needed. This will also helps bzr config to get it right. The
  get_mutable_section trick should not be needed anymore either.

- decorate branch.unlock to call stack.save if last_lock() it True outside
  of @only_raises scope (evil decorator)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007, 2009, 2010, 2011 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
19
19
 
20
20
import bzrlib
21
21
from bzrlib import (
 
22
    branch,
22
23
    config,
 
24
    errors,
23
25
    tests,
24
26
    )
25
27
 
49
51
        out = self.run_bzr("whoami --email 'foo <foo@example.com>'", 3)[0]
50
52
        self.assertEquals("", out)
51
53
 
 
54
    def set_branch_email(self, b, email):
 
55
        b.lock_write()
 
56
        try:
 
57
            b.get_config_stack().set('email', email)
 
58
        finally:
 
59
            b.unlock()
 
60
 
52
61
    def test_whoami_branch(self):
53
62
        """branch specific user identity works."""
54
63
        wt = self.make_branch_and_tree('.')
55
64
        b = bzrlib.branch.Branch.open('.')
56
 
        b.get_config().set_user_option('email',
57
 
                                       'Branch Identity <branch@identi.ty>')
 
65
        self.set_branch_email(b, 'Branch Identity <branch@identi.ty>')
58
66
        self.assertWhoAmI('Branch Identity <branch@identi.ty>')
59
67
        self.assertWhoAmI('branch@identi.ty', '--email')
60
68
 
79
87
        """
80
88
        wt = self.make_branch_and_tree('.')
81
89
        b = bzrlib.branch.Branch.open('.')
82
 
        b.get_config().set_user_option('email', u'Branch Identity \u20ac ' +
83
 
                                       '<branch@identi.ty>')
 
90
        self.set_branch_email(b, u'Branch Identity \u20ac <branch@identi.ty>')
84
91
        self.assertWhoAmI('Branch Identity ? <branch@identi.ty>',
85
92
                          encoding='ascii')
86
93
        self.assertWhoAmI('branch@identi.ty', '--email',
100
107
        self.overrideEnv('EMAIL', None)
101
108
        self.overrideEnv('BZR_EMAIL', None)
102
109
        # Also, make sure that it's not inferred from mailname.
103
 
        self.overrideAttr(config, '_auto_user_id',
104
 
            lambda: (None, None))
 
110
        self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
105
111
        out, err = self.run_bzr(['whoami'], 3)
106
112
        self.assertContainsRe(err, 'Unable to determine your name')
107
113
 
108
114
    def test_whoami_directory(self):
109
115
        """Test --directory option."""
110
116
        wt = self.make_branch_and_tree('subdir')
111
 
        c = wt.branch.get_config()
112
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
117
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
113
118
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
114
119
                          '--directory', 'subdir')
115
120
        self.run_bzr(['whoami', '--directory', 'subdir', '--branch',
116
121
                      'Changed Identity <changed@identi.ty>'])
117
 
        c = wt.branch.get_config()
 
122
        c = branch.Branch.open('subdir').get_config_stack()
118
123
        self.assertEquals('Changed Identity <changed@identi.ty>',
119
 
                          c.get_user_option('email'))
 
124
                          c.get('email'))
120
125
 
121
126
    def test_whoami_remote_directory(self):
122
127
        """Test --directory option with a remote directory."""
123
128
        wt = self.make_branch_and_tree('subdir')
124
 
        c = wt.branch.get_config()
125
 
        c.set_user_option('email', 'Branch Identity <branch@identi.ty>')
 
129
        self.set_branch_email(wt.branch, 'Branch Identity <branch@identi.ty>')
126
130
        url = self.get_readonly_url() + '/subdir'
127
131
        self.assertWhoAmI('Branch Identity <branch@identi.ty>',
128
132
                          '--directory', url)
131
135
                      'Changed Identity <changed@identi.ty>'])
132
136
        # The identity has been set in the branch config (but not the global
133
137
        # config)
134
 
        c = wt.branch.get_config()
 
138
        c = branch.Branch.open(url).get_config_stack()
135
139
        self.assertEquals('Changed Identity <changed@identi.ty>',
136
 
                          c.get_user_option('email'))
137
 
        global_conf = config.GlobalConfig()
138
 
        self.assertEquals(None, global_conf.get_user_option('email'))
 
140
                          c.get('email'))
 
141
        # Ensuring that the value does not come from bazaar.conf requires some
 
142
        # isolation setup
 
143
        self.overrideEnv('BZR_EMAIL', None)
 
144
        self.overrideEnv('EMAIL', None)
 
145
        self.overrideAttr(config, '_auto_user_id', lambda: (None, None))
 
146
        global_conf = config.GlobalStack()
 
147
        self.assertRaises(errors.NoWhoami, global_conf.get, 'email')
139
148
 
140
149
    def test_whoami_nonbranch_directory(self):
141
150
        """Test --directory mentioning a non-branch directory."""