[PATCH RFC] pager: add test-pager.t
Brodie Rao
brodie at sf.io
Mon May 21 13:00:35 CDT 2012
# HG changeset patch
# User Brodie Rao <brodie at sf.io>
# Date 1337381249 25200
# Node ID ec4d8456345d187b625a21808930d2b01c73e133
# Parent d0b9ebba41e9a1733294d5fa1b497ada5eda93c8
pager: add test-pager.t
This test has some issues that should be addressed before being committed:
- It currently fails on Python 2.4 due to a bug in its version of subprocess.
See issue3226 for more information.
- For testing purposes, it adds two new internal/undocumented settings:
pager.assume-tty and pager.assume-stderr-tty. This logic should probably be
moved into ui._isatty(). (The same applies to test-progress.t and
progress.assume-tty.)
- It hasn't been tested on Windows. In theory, it should work under MinGW.
diff --git a/hgext/pager.py b/hgext/pager.py
--- a/hgext/pager.py
+++ b/hgext/pager.py
@@ -61,7 +61,7 @@ def _runpager(ui, p):
stdout = os.dup(sys.stdout.fileno())
stderr = os.dup(sys.stderr.fileno())
os.dup2(pager.stdin.fileno(), sys.stdout.fileno())
- if ui._isatty(sys.stderr):
+ if ui._isatty(sys.stderr) or ui.configbool('pager', 'assume-stderr-tty'):
os.dup2(pager.stdin.fileno(), sys.stderr.fileno())
@atexit.register
@@ -72,7 +72,9 @@ def _runpager(ui, p):
pager.wait()
def uisetup(ui):
- if '--debugger' in sys.argv or not ui.formatted():
+ if ('--debugger' in sys.argv or
+ (not ui.formatted() and
+ not ui.configbool('pager', 'assume-tty'))):
return
def pagecmd(orig, ui, options, cmd, cmdfunc):
diff --git a/tests/test-pager.t b/tests/test-pager.t
new file mode 100644
--- /dev/null
+++ b/tests/test-pager.t
@@ -0,0 +1,112 @@
+Create a dummy pager script:
+
+ $ cat > pager.py <<EOF
+ > #!/usr/bin/env python
+ > import sys
+ > for line in sys.stdin:
+ > sys.stdout.write('PAGED: %s' % line)
+ > EOF
+ $ chmod +x pager.py
+
+Enable the extension:
+
+ $ echo "[extensions]" >> $HGRCPATH
+ $ echo "pager = " >> $HGRCPATH
+
+Test not having a configured pager:
+
+ $ hg version -q --pager=yes
+ Mercurial Distributed SCM (version *) (glob)
+
+Test that the pager doesn't get enabled automatically:
+
+ $ PAGER=./pager.py hg version -q
+ Mercurial Distributed SCM (version *) (glob)
+
+Test that the pager doesn't work without a TTY:
+
+ $ PAGER=./pager.py hg version -q --pager=yes
+ Mercurial Distributed SCM (version *) (glob)
+
+Force having a TTY:
+
+ $ echo '[pager]' >> $HGRCPATH
+ $ echo 'assume-tty = true' >> $HGRCPATH
+
+Test enabling the pager in various ways:
+
+ $ PAGER=./pager.py hg version --pager=yes
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+ PAGED: (see http://mercurial.selenic.com for more information)
+ PAGED:
+ PAGED: Copyright (C) 2005-* Matt Mackall and others (glob)
+ PAGED: This is free software; see the source for copying conditions. There is NO
+ PAGED: warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ $ echo 'pager = ./pager.py' >> $HGRCPATH
+ $ hg --config pager.attend=version version -q
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+ $ hg --config pager.attend=version version -q --pager=auto
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+ $ hg --config pager.attend= version -q
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+ $ hg version -q --pager=yes
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+
+Test disabling the pager in various ways:
+
+ $ hg --config pager.attend= --config pager.ignore=help version -q
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+ $ hg --config pager.attend= --config pager.ignore=version version -q
+ Mercurial Distributed SCM (version *) (glob)
+ $ hg version -q --pager=no
+ Mercurial Distributed SCM (version *) (glob)
+
+Test that pager.pager takes precedence over PAGER:
+
+ $ PAGER=cat hg version -q --pager=yes
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+
+Test that exiting the pager early truncates output:
+
+ $ cat > one-line-pager.py <<EOF
+ > #!/usr/bin/env python
+ > import sys
+ > for line in sys.stdin:
+ > sys.stdout.write('PAGED: %s' % line)
+ > break
+ > EOF
+ $ chmod +x one-line-pager.py
+ $ hg --config pager.pager=./one-line-pager.py version --pager=yes
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+
+Test that interactive mode is disabled:
+
+ $ cat > prompt.py <<EOF
+ > def uisetup(ui):
+ > ui.prompt('is anyone out there?')
+ > EOF
+ $ hg --config extensions.prompt=prompt.py version -q --pager=yes
+ PAGED: is anyone out there? y
+ PAGED: Mercurial Distributed SCM (version *) (glob)
+
+Test that exit codes get preserved and stderr doesn't get paged if it isn't
+a TTY (issue2541, issue3226):
+
+ $ cat > exit.py <<EOF
+ > import sys
+ > from mercurial import commands
+ > def exit(ui):
+ > ui.write_err('writing to stderr\n')
+ > ui.write('writing to stdout\n')
+ > sys.exit(123)
+ > cmdtable = {'exit': (exit, [])}
+ > commands.norepo += ' exit'
+ > EOF
+ $ hg --config extensions.exit=exit.py exit --pager=yes
+ writing to stderr
+ PAGED: writing to stdout
+ [123]
+ $ hg --config extensions.exit=exit.py --config pager.assume-stderr-tty=1 exit --pager=yes
+ PAGED: writing to stderr
+ PAGED: writing to stdout
+ [123]
More information about the Mercurial-devel
mailing list