Patchwork [08,of,10] hg: when testing, warn if repo.ui is passed to a new peer or localrepo

login
register
mail settings
Submitter Simon Heimberg
Date March 22, 2013, 1:20 a.m.
Message ID <bc3341ad23a61fa0ad27.1363915258@lapsi.heimberg.home>
Download mbox | patch
Permalink /patch/1161/
State Changes Requested, archived
Headers show

Comments

Simon Heimberg - March 22, 2013, 1:20 a.m.
# HG changeset patch
# User Simon Heimberg <simohe@besonet.ch>
# Date 1349372803 -7200
# Node ID bc3341ad23a61fa0ad272be98cdc00cf11d4b8eb
# Parent  edcf5f72fdb5621bf6c1c8074ea66428932f0f96
hg: when testing, warn if repo.ui is passed to a new peer or localrepo

For not getting the configuration of another repo, baseui has to be used when
crating a new repository (or a new peer). Therefore we warn the programmer if
the ui of a repo is passed.
For not warning normal users, the warning is only printed when we are running
in run-tests.py (when TESTTMP is in env), when unittest is loaded (running in a
test suite of an other project) or when debugging is enabled.

Patch

diff -r edcf5f72fdb5 -r bc3341ad23a6 mercurial/hg.py
--- a/mercurial/hg.py	Don Mär 21 18:16:49 2013 +0100
+++ b/mercurial/hg.py	Don Okt 04 19:46:43 2012 +0200
@@ -609,6 +609,8 @@ 
         src = src.ui # copy target options from repo
     else: # assume it's a global ui object
         dst = src.copy() # keep all global options
+        if not src.checkglobal('pass repo (not repo.ui) to hg.peer'):
+            dst.isglobal = True # do not repeat warning
 
     # copy ssh-specific options
     for o in 'ssh', 'remotecmd':
diff -r edcf5f72fdb5 -r bc3341ad23a6 mercurial/localrepo.py
--- a/mercurial/localrepo.py	Don Mär 21 18:16:49 2013 +0100
+++ b/mercurial/localrepo.py	Don Okt 04 19:46:43 2012 +0200
@@ -161,8 +161,10 @@ 
         self.auditor = scmutil.pathauditor(self.root, self._checknested)
         self.vfs = scmutil.vfs(self.path)
         self.opener = self.vfs
+        baseui.checkglobal('pass a baseui (not repo.ui) when creating a repo')
         self.baseui = baseui
         self.ui = baseui.copy()
+        self.ui.isglobal = False
         # A list of callback to shape the phase if no data were found.
         # Callback are in the form: func(repo, roots) --> processed root.
         # This list it to be filled by extension during repo setup
diff -r edcf5f72fdb5 -r bc3341ad23a6 mercurial/ui.py
--- a/mercurial/ui.py	Don Mär 21 18:16:49 2013 +0100
+++ b/mercurial/ui.py	Don Okt 04 19:46:43 2012 +0200
@@ -10,6 +10,8 @@ 
 import config, scmutil, util, error, formatter
 
 class ui(object):
+    isglobal = True # this is unset by the repo for its ui
+
     def __init__(self, src=None):
         self._buffers = []
         self.quiet = self.verbose = self.debugflag = self.tracebackflag = False
@@ -34,6 +36,8 @@ 
             self.environ = src.environ
             self.callhooks = src.callhooks
             self.fixconfig()
+            if not src.isglobal:
+                self.isglobal = False
         else:
             self.fout = sys.stdout
             self.ferr = sys.stderr
@@ -766,3 +770,21 @@ 
         ui.write(ui.label(s, 'label')).
         '''
         return msg
+
+    def checkglobal(self, warnmsg):
+        '''in test run, print a warning if this ui is inside a repo
+
+        When a repo or a peer is created, it needs its own configuration. But
+        when it gets the ui object of another repo, the configuration from
+        there is copied into the new ui object.
+        '''
+        if self.isglobal:
+            return True # passed
+        if os.environ.get('TESTTMP') or 'unittest' in sys.modules or \
+           self.debugflag:
+            # running in a test, warn the programmer
+            #  TESTTMP is set in mercurial test suite
+            #  unittest is a test suite used in many projects
+            #  debugflag is set when hg --debug was called
+            self.write_err('PROGRAMMER WARNING: %s\n' % warnmsg)
+        return False