sapling/eden/scm/tests/test-ui-color.py
Jun Wu 3e0b781197 py3: only use binary stdin/stdout/stderr
Summary:
Drop stdoutbytes/stdinbytes. They make things unnecessarily complicated
(especially for chg / Rust dispatch entry point).

The new idea is IO are using bytes. Text are written in utf-8 (Python 3) or
local encoding (Python 2). To make stdout behave reasonably on systems not
using utf-8 locale (ex. Windows), we might add a Rust binding to Rust's stdout,
which does the right thing:
- When writing to stdout console, expect text to be utf-8 encoded and do proper decoding.
- Wehn writing to stdout file, write the raw bytes without translation.

Note Python's `sys.stdout.buffer` does not do translation when writing to stdout console
like Rust's stdout.

For now, my main motivation of this change is to fix chg on Python 3.

Reviewed By: xavierd

Differential Revision: D19702533

fbshipit-source-id: 74704c83e1b200ff66fb3a2d23d97ff21c7239c8
2020-02-03 18:26:57 -08:00

39 lines
946 B
Python

from __future__ import absolute_import, print_function
import os
from edenscm.mercurial import dispatch, ui as uimod
from hghave import require
# ensure errors aren't buffered
testui = uimod.ui()
testui.pushbuffer()
testui.write(("buffered\n"))
testui.warn(("warning\n"))
testui.write_err("error\n")
print(repr(testui.popbuffer()))
# test dispatch.dispatch with the same ui object
hgrc = open(os.environ["HGRCPATH"], "w")
hgrc.write("[extensions]\n")
hgrc.write("color=\n")
hgrc.close()
ui_ = uimod.ui.load()
ui_.setconfig("ui", "formatted", "True")
# we're not interested in the output, so write that to devnull
ui_.fout = open(os.devnull, "wb")
# call some arbitrary command just so we go through
# color's wrapped _runcommand twice.
def runcmd():
dispatch.dispatch(dispatch.request(["version", "-q"], ui_))
runcmd()
print("colored? %s" % (ui_._colormode is not None))
runcmd()
print("colored? %s" % (ui_._colormode is not None))