##// END OF EJS Templates
posix: always seek to EOF when opening a file in append mode...
posix: always seek to EOF when opening a file in append mode Python 3 already does this, so skip it there. Consider the program: #include <stdio.h> int main() { FILE *f = fopen("narf", "w"); fprintf(f, "narf\n"); fclose(f); f = fopen("narf", "a"); printf("%ld\n", ftell(f)); fprintf(f, "troz\n"); printf("%ld\n", ftell(f)); return 0; } on macOS, FreeBSD, and Linux with glibc, this program prints 5 10 but on musl libc (Alpine Linux and probably others) this prints 0 10 By my reading of https://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html this is technically correct, specifically: > Opening a file with append mode (a as the first character in the > mode argument) shall cause all subsequent writes to the file to be > forced to the then current end-of-file, regardless of intervening > calls to fseek(). in other words, the file position doesn't really matter in append-mode files, and we can't depend on it being at all meaningful unless we perform a seek() before tell() after open(..., 'a'). Experimentally after a .write() we can do a .tell() and it'll always be reasonable, but I'm unclear from reading the specification if that's a smart thing to rely on. This matches what we do on Windows and what Python 3 does for free, so let's just be consistent. Thanks to Yuya for the idea.

File last commit:

r41494:30dd20a5 default
r42778:97ada9b8 5.0.2 stable
Show More
test-hgweb-auth.py
160 lines | 5.3 KiB | text/x-python | PythonLexer
/ tests / test-hgweb-auth.py
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 from __future__ import absolute_import, print_function
Robert Stanca
py3: use absolute_import in test-hgweb-auth.py
r28747
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 from mercurial import demandimport; demandimport.enable()
Robert Stanca
py3: use absolute_import in test-hgweb-auth.py
r28747 from mercurial import (
Yuya Nishihara
test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
r28808 error,
Augie Fackler
tests: use stringutil.pprint instead of custom dumper in test-hgweb-auth.py...
r37959 pycompat,
Yuya Nishihara
test-hgweb-auth: alias ui as uimod
r28807 ui as uimod,
Robert Stanca
py3: use absolute_import in test-hgweb-auth.py
r28747 url,
util,
)
Augie Fackler
tests: use stringutil.pprint instead of custom dumper in test-hgweb-auth.py...
r37959 from mercurial.utils import (
stringutil,
)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 urlerr = util.urlerr
urlreq = util.urlreq
Yuya Nishihara
test-hgweb-auth: alias ui as uimod
r28807 class myui(uimod.ui):
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 def interactive(self):
return False
Yuya Nishihara
ui: factor out ui.load() to create a ui without loading configs (API)...
r30559 origui = myui.load()
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
def writeauth(items):
ui = origui.copy()
Pulkit Goyal
py3: use dict.items() instead of dict.iteritems() in tests...
r36345 for name, value in items.items():
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 ui.setconfig(b'auth', name, value)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 return ui
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 def _stringifyauthinfo(ai):
if ai is None:
return ai
realm, authuris, user, passwd = ai
return (pycompat.strurl(realm),
[pycompat.strurl(u) for u in authuris],
pycompat.strurl(user),
pycompat.strurl(passwd),
)
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 def test(auth, urls=None):
Yuya Nishihara
stringutil: flip the default of pprint() to bprefix=False...
r37961 print('CFG:', pycompat.sysstr(stringutil.pprint(auth, bprefix=True)))
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 prefixes = set()
for k in auth:
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 prefixes.add(k.split(b'.', 1)[0])
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 for p in prefixes:
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 for name in (b'.username', b'.password'):
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 if (p + name) not in auth:
auth[p + name] = p
Pulkit Goyal
py3: use dict.items() instead of dict.iteritems() in tests...
r36345 auth = dict((k, v) for k, v in auth.items() if v is not None)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
ui = writeauth(auth)
def _test(uri):
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 print('URI:', pycompat.strurl(uri))
Sune Foldager
allow http authentication information to be specified in the configuration
r8333 try:
liscju
url: extract password database from password manager...
r29377 pm = url.passwordmgr(ui, urlreq.httppasswordmgrwithdefaultrealm())
Patrick Mezard
http: pass user to readauthforuri() (fix 4a43e23b8c55)...
r15025 u, authinfo = util.url(uri).authinfo()
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 if authinfo is not None:
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 pm.add_password(*_stringifyauthinfo(authinfo))
print(' ', tuple(pycompat.strurl(a) for a in
pm.find_user_password('test',
pycompat.strurl(u))))
Yuya Nishihara
test-hgweb-auth: stop direct symbol import of mercurial.error.Abort
r28808 except error.Abort:
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print(' ','abort')
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 if not urls:
urls = [
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 b'http://example.org/foo',
b'http://example.org/foo/bar',
b'http://example.org/bar',
b'https://example.org/foo',
b'https://example.org/foo/bar',
b'https://example.org/bar',
b'https://x@example.org/bar',
b'https://y@example.org/bar',
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005 ]
for u in urls:
_test(u)
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('\n*** Test in-uri schemes\n')
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'x.prefix': b'http://example.org'})
test({b'x.prefix': b'https://example.org'})
test({b'x.prefix': b'http://example.org', b'x.schemes': b'https'})
test({b'x.prefix': b'https://example.org', b'x.schemes': b'http'})
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('\n*** Test separately configured schemes\n')
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'x.prefix': b'example.org', b'x.schemes': b'http'})
test({b'x.prefix': b'example.org', b'x.schemes': b'https'})
test({b'x.prefix': b'example.org', b'x.schemes': b'http https'})
Sune Foldager
allow http authentication information to be specified in the configuration
r8333
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('\n*** Test prefix matching\n')
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'x.prefix': b'http://example.org/foo',
b'y.prefix': b'http://example.org/bar'})
test({b'x.prefix': b'http://example.org/foo',
b'y.prefix': b'http://example.org/foo/bar'})
test({b'x.prefix': b'*', b'y.prefix': b'https://example.org/bar'})
Patrick Mezard
hgweb: do not ignore [auth] if url has a username (issue2822)...
r15005
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('\n*** Test user matching\n')
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'x.prefix': b'http://example.org/foo',
b'x.username': None,
b'x.password': b'xpassword'},
urls=[b'http://y@example.org/foo'])
test({b'x.prefix': b'http://example.org/foo',
b'x.username': None,
b'x.password': b'xpassword',
b'y.prefix': b'http://example.org/foo',
b'y.username': b'y',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
test({b'x.prefix': b'http://example.org/foo/bar',
b'x.username': None,
b'x.password': b'xpassword',
b'y.prefix': b'http://example.org/foo',
b'y.username': b'y',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo/bar'])
Patrick Mezard
http: strip credentials from urllib2 manager URIs (issue2885)...
r15024
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 print('\n*** Test user matching with name in prefix\n')
# prefix, username and URL have the same user
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'x.prefix': b'https://example.org/foo',
b'x.username': None,
b'x.password': b'xpassword',
b'y.prefix': b'http://y@example.org/foo',
b'y.username': b'y',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 # Prefix has a different user from username and URL
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'y.prefix': b'http://z@example.org/foo',
b'y.username': b'y',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 # Prefix has a different user from URL; no username
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'y.prefix': b'http://z@example.org/foo',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 # Prefix and URL have same user, but doesn't match username
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'y.prefix': b'http://y@example.org/foo',
b'y.username': b'z',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 # Prefix and URL have the same user; no username
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'y.prefix': b'http://y@example.org/foo',
b'y.password': b'ypassword'},
urls=[b'http://y@example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699 # Prefix user, but no URL user or username
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 test({b'y.prefix': b'http://y@example.org/foo',
b'y.password': b'ypassword'},
urls=[b'http://example.org/foo'])
Matt Harbison
http: allow 'auth.prefix' to have a username consistent with the URI...
r40699
Patrick Mezard
http: strip credentials from urllib2 manager URIs (issue2885)...
r15024 def testauthinfo(fullurl, authurl):
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('URIs:', fullurl, authurl)
timeless
pycompat: switch to util.urlreq/util.urlerr for py3 compat
r28883 pm = urlreq.httppasswordmgrwithdefaultrealm()
Augie Fackler
tests: port test-hgweb-auth.py to Python 3...
r41494 ai = _stringifyauthinfo(util.url(pycompat.bytesurl(fullurl)).authinfo()[1])
pm.add_password(*ai)
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print(pm.find_user_password('test', authurl))
Patrick Mezard
http: strip credentials from urllib2 manager URIs (issue2885)...
r15024
Robert Stanca
py3: use print_function in test-hgweb-auth.py
r28748 print('\n*** Test urllib2 and util.url\n')
Patrick Mezard
http: strip credentials from urllib2 manager URIs (issue2885)...
r15024 testauthinfo('http://user@example.com:8080/foo', 'http://example.com:8080/foo')