make smart help formatter test deterministic

Set up the formatter to always use a width=80 so that the terminal width
of the developer's system does not cause the test to fail. Trying to
mock os.environ failed, but there is an arg to HelpFormatter to set the
width explicitly. Unfortunately, there is no way to do that through the
parser, so we have to replace the parser's formatter_class attribute
with a partial() that passes width to the original class.

Change-Id: I2ab035b5b188f1a028e1776ba9f09c71dc316c11
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-06-07 16:51:50 -04:00 committed by Rajath Agasthya
parent 7279f4b096
commit 0a7157de64
1 changed files with 14 additions and 0 deletions

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import functools
from cliff import command
from cliff.tests import base
@ -113,4 +115,16 @@ class TestHelp(base.TestBase):
def test_smart_help_formatter(self):
cmd = TestCommand(None, None)
parser = cmd.get_parser('NAME')
# Set up the formatter to always use a width=80 so that the
# terminal width of the developer's system does not cause the
# test to fail. Trying to mock os.environ failed, but there is
# an arg to HelpFormatter to set the width
# explicitly. Unfortunately, there is no way to do that
# through the parser, so we have to replace the parser's
# formatter_class attribute with a partial() that passes width
# to the original class.
parser.formatter_class = functools.partial(
parser.formatter_class,
width=78,
)
self.assertIn(expected_help_message, parser.format_help())