changeset 52023:d1b54c152673

extdiff: don't run gui programs when in a cli-only environment In order to provide a useful error message to override the behavior, we also need to slightly change the way that tool.gui is found in the config. Before, it had to be where tool.diffargs is located, which might not exist. Now, tool.isgui can exist on its own. A test is added for the new error message. We also need to force procutil.isgui() to return true, so we set $DISPLAY to a non-empty value before running any test expecting to have a gui.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 04 Feb 2019 23:32:20 -0800
parents 745409f94f0c
children e2ccc8cce975
files hgext/extdiff.py tests/hghave.py tests/test-extdiff.t
diffstat 3 files changed, 36 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/extdiff.py	Wed Dec 28 21:33:44 2022 -0800
+++ b/hgext/extdiff.py	Mon Feb 04 23:32:20 2019 -0800
@@ -729,16 +729,24 @@
     to its parent.
     """
 
-    def __init__(self, path, cmdline, isgui):
+    def __init__(self, cmd, path, cmdline, isgui):
         # We can't pass non-ASCII through docstrings (and path is
         # in an unknown encoding anyway), but avoid double separators on
         # Windows
         docpath = stringutil.escapestr(path).replace(b'\\\\', b'\\')
         self.__doc__ %= {'path': pycompat.sysstr(stringutil.uirepr(docpath))}
+        self._name = cmd
         self._cmdline = cmdline
         self._isgui = isgui
 
     def __call__(self, ui, repo, *pats, **opts):
+        if self._isgui and not procutil.gui():
+            msg = _(b"tool '%s' requires a GUI") % self._name
+            hint = (
+                _(b"to override, use: --config diff-tools.%s.gui=False")
+                % self._name
+            )
+            raise error.Abort(msg, hint=hint)
         opts = pycompat.byteskwargs(opts)
         options = b' '.join(map(procutil.shellquote, opts[b'option']))
         if options:
@@ -798,9 +806,15 @@
             args = ui.config(section, key)
             if args:
                 cmdline += b' ' + args
-                if isgui is None:
-                    isgui = ui.configbool(section, cmd + b'.gui') or False
                 break
+    if isgui is None:
+        key = cmd + b'.gui'
+        for section in (b'diff-tools', b'merge-tools'):
+            isgui = ui.configbool(section, key)
+            if isgui is not None:
+                break
+    if isgui is None:
+        isgui = False
     return cmd, path, cmdline, isgui
 
 
@@ -815,7 +829,7 @@
             _(b'hg %s [OPTION]... [FILE]...') % cmd,
             helpcategory=command.CATEGORY_FILE_CONTENTS,
             inferrepo=True,
-        )(savedcmd(path, cmdline, isgui))
+        )(savedcmd(cmd, path, cmdline, isgui))
 
 
 # tell hggettext to extract docstrings from these functions:
--- a/tests/hghave.py	Wed Dec 28 21:33:44 2022 -0800
+++ b/tests/hghave.py	Mon Feb 04 23:32:20 2019 -0800
@@ -738,6 +738,13 @@
         return False
 
 
+@check("gui", "whether a gui environment is available or not")
+def has_gui():
+    from mercurial.utils import procutil
+
+    return procutil.gui()
+
+
 @check("test-repo", "running tests from repository")
 def has_test_repo():
     t = os.environ["TESTDIR"]
--- a/tests/test-extdiff.t	Wed Dec 28 21:33:44 2022 -0800
+++ b/tests/test-extdiff.t	Mon Feb 04 23:32:20 2019 -0800
@@ -158,9 +158,18 @@
   diffing */extdiff.*/a.46c0e4daeb72/b a.81906f2b98ac/b (glob) (no-windows !)
   [1]
 
+#if no-gui
+Test gui tool error:
+
+  $ hg --config extdiff.gui.alabalaf=True alabalaf
+  abort: tool 'alabalaf' requires a GUI
+  (to override, use: --config diff-tools.alabalaf.gui=False)
+  [255]
+#endif
+
 Test --per-file option for gui tool:
 
-  $ hg --config extdiff.gui.alabalaf=True alabalaf -c 6 --per-file --debug
+  $ DISPLAY=fake hg --config extdiff.gui.alabalaf=True alabalaf -c 6 --per-file --debug
   diffing */extdiff.*/a.46c0e4daeb72/* a.81906f2b98ac/* (glob)
   diffing */extdiff.*/a.46c0e4daeb72/* a.81906f2b98ac/* (glob)
   making snapshot of 2 files from rev 46c0e4daeb72
@@ -176,7 +185,7 @@
 
 Test --per-file option for gui tool again:
 
-  $ hg --config merge-tools.alabalaf.gui=True alabalaf -c 6 --per-file --debug
+  $ DISPLAY=fake hg --config merge-tools.alabalaf.gui=True alabalaf -c 6 --per-file --debug
   diffing */extdiff.*/a.46c0e4daeb72/* a.81906f2b98ac/* (glob)
   diffing */extdiff.*/a.46c0e4daeb72/* a.81906f2b98ac/* (glob)
   making snapshot of 2 files from rev 46c0e4daeb72