tests/test-trusted.py
changeset 3552 9b52239dc740
parent 3551 3b07e223534b
child 3677 1a0fa3914c46
equal deleted inserted replaced
3551:3b07e223534b 3552:9b52239dc740
     7 from mercurial import ui, util
     7 from mercurial import ui, util
     8 
     8 
     9 hgrc = os.environ['HGRCPATH']
     9 hgrc = os.environ['HGRCPATH']
    10 
    10 
    11 def testui(user='foo', group='bar', tusers=(), tgroups=(),
    11 def testui(user='foo', group='bar', tusers=(), tgroups=(),
    12            cuser='foo', cgroup='bar', debug=False):
    12            cuser='foo', cgroup='bar', debug=False, silent=False):
    13     # user, group => owners of the file
    13     # user, group => owners of the file
    14     # tusers, tgroups => trusted users/groups
    14     # tusers, tgroups => trusted users/groups
    15     # cuser, cgroup => user/group of the current process
    15     # cuser, cgroup => user/group of the current process
    16 
    16 
    17     # write a global hgrc with the list of trusted users/groups and
    17     # write a global hgrc with the list of trusted users/groups and
    54 
    54 
    55     parentui = ui.ui()
    55     parentui = ui.ui()
    56     parentui.updateopts(debug=debug)
    56     parentui.updateopts(debug=debug)
    57     u = ui.ui(parentui=parentui)
    57     u = ui.ui(parentui=parentui)
    58     u.readconfig('.hg/hgrc')
    58     u.readconfig('.hg/hgrc')
       
    59     if silent:
       
    60         return u
       
    61     print 'trusted'
    59     for name, path in u.configitems('paths'):
    62     for name, path in u.configitems('paths'):
    60         print '   ', name, '=', path
    63         print '   ', name, '=', path
       
    64     print 'untrusted'
       
    65     for name, path in u.configitems('paths', untrusted=True):
       
    66         print '.',
       
    67         u.config('paths', name) # warning with debug=True
       
    68         print '.',
       
    69         u.config('paths', name, untrusted=True) # no warnings
       
    70         print name, '=', path
    61     print
    71     print
    62 
    72 
    63     return u
    73     return u
    64 
    74 
    65 os.mkdir('repo')
    75 os.mkdir('repo')
    66 os.chdir('repo')
    76 os.chdir('repo')
    67 os.mkdir('.hg')
    77 os.mkdir('.hg')
    68 f = open('.hg/hgrc', 'w')
    78 f = open('.hg/hgrc', 'w')
    69 f.write('[paths]\n')
    79 f.write('[paths]\n')
    70 f.write('local = /another/path\n\n')
    80 f.write('local = /another/path\n\n')
       
    81 f.write('interpolated = %(global)s%(local)s\n\n')
    71 f.close()
    82 f.close()
    72 
    83 
    73 #print '# Everything is run by user foo, group bar\n'
    84 #print '# Everything is run by user foo, group bar\n'
    74 
    85 
    75 # same user, same group
    86 # same user, same group
   109 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
   120 testui(user='abc', group='def', tusers=['foo', 'xyz', 'bleh'],
   110        tgroups=['bar', 'def', 'baz', 'qux'])
   121        tgroups=['bar', 'def', 'baz', 'qux'])
   111 
   122 
   112 print "# Can't figure out the name of the user running this process"
   123 print "# Can't figure out the name of the user running this process"
   113 testui(user='abc', group='def', cuser=None)
   124 testui(user='abc', group='def', cuser=None)
       
   125 
       
   126 print "# prints debug warnings"
       
   127 u = testui(user='abc', group='def', cuser='foo', debug=True)
       
   128 
       
   129 print "# ui.readsections"
       
   130 filename = 'foobar'
       
   131 f = open(filename, 'w')
       
   132 f.write('[foobar]\n')
       
   133 f.write('baz = quux\n')
       
   134 f.close()
       
   135 u.readsections(filename, 'foobar')
       
   136 print u.config('foobar', 'baz')
       
   137 
       
   138 print
       
   139 print "# read trusted, untrusted, new ui, trusted"
       
   140 u = ui.ui()
       
   141 u.updateopts(debug=True)
       
   142 u.readconfig(filename)
       
   143 u2 = ui.ui(parentui=u)
       
   144 def username(uid=None):
       
   145     return 'foo'
       
   146 util.username = username
       
   147 u2.readconfig('.hg/hgrc')
       
   148 print 'trusted:'
       
   149 print u2.config('foobar', 'baz')
       
   150 print u2.config('paths', 'interpolated')
       
   151 print 'untrusted:'
       
   152 print u2.config('foobar', 'baz', untrusted=True)
       
   153 print u2.config('paths', 'interpolated', untrusted=True)
       
   154 
       
   155 print 
       
   156 print "# error handling"
       
   157 
       
   158 def assertraises(f, exc=util.Abort):
       
   159     try:
       
   160         f()
       
   161     except exc, inst:
       
   162         print 'raised', inst.__class__.__name__
       
   163     else:
       
   164         print 'no exception?!'
       
   165 
       
   166 print "# file doesn't exist"
       
   167 os.unlink('.hg/hgrc')
       
   168 assert not os.path.exists('.hg/hgrc')
       
   169 testui(debug=True, silent=True)
       
   170 testui(user='abc', group='def', debug=True, silent=True)
       
   171 
       
   172 print
       
   173 print "# parse error"
       
   174 f = open('.hg/hgrc', 'w')
       
   175 f.write('foo = bar')
       
   176 f.close()
       
   177 testui(user='abc', group='def', silent=True)
       
   178 assertraises(lambda: testui(debug=True, silent=True))
       
   179 
       
   180 print
       
   181 print "# interpolation error"
       
   182 f = open('.hg/hgrc', 'w')
       
   183 f.write('[foo]\n')
       
   184 f.write('bar = %(')
       
   185 f.close()
       
   186 u = testui(debug=True, silent=True)
       
   187 print '# regular config:'
       
   188 print '  trusted',
       
   189 assertraises(lambda: u.config('foo', 'bar'))
       
   190 print 'untrusted',
       
   191 assertraises(lambda: u.config('foo', 'bar', untrusted=True))
       
   192 
       
   193 u = testui(user='abc', group='def', debug=True, silent=True)
       
   194 print '  trusted ',
       
   195 print u.config('foo', 'bar')
       
   196 print 'untrusted',
       
   197 assertraises(lambda: u.config('foo', 'bar', untrusted=True))
       
   198 
       
   199 print '# configitems:'
       
   200 print '  trusted ',
       
   201 print u.configitems('foo')
       
   202 print 'untrusted',
       
   203 assertraises(lambda: u.configitems('foo', untrusted=True))
       
   204