Skip to content

Commit

Permalink
Fix for run_command tests now that it returns native strings
Browse files Browse the repository at this point in the history
  • Loading branch information
abadger committed Oct 4, 2016
1 parent 125a8d3 commit 08a58ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions lib/ansible/module_utils/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# to the complete work.
#
# Copyright (c), Michael DeHaan <[email protected]>, 2012-2013
# Copyright (c), Toshio Kuratomi <[email protected]> 2016
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
Expand Down Expand Up @@ -2240,6 +2241,10 @@ def run_command(self, args, check_rc=False, close_fds=True, executable=None, dat
# if we're checking for prompts, do it now
if prompt_re:
if prompt_re.search(stdout) and not data:
if encoding:
stdout = to_native(stdout, encoding=encoding, errors=errors)
else:
stdout = stdout
return (257, stdout, "A prompt was encountered while running a command, but no input data was specified")
# only break out if no pipes are left to read or
# the pipes are completely read and
Expand Down
18 changes: 15 additions & 3 deletions test/units/module_utils/basic/test_run_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import time
from io import BytesIO, StringIO

from ansible.compat.six import PY3
from ansible.compat.tests import unittest
from ansible.compat.tests.mock import call, MagicMock, Mock, patch, sentinel

Expand Down Expand Up @@ -173,13 +174,24 @@ def test_ascii_stdout(self):
self.cmd_out[sentinel.stdout] = BytesIO(b'hello')
(rc, stdout, stderr) = self.module.run_command('/bin/cat hello.txt')
self.assertEqual(rc, 0)
self.assertEqual(stdout, b'hello')
# module_utils function. On py3 it returns text and py2 it returns
# bytes because it's returning native strings
if PY3:
self.assertEqual(stdout, u'hello')
else:
self.assertEqual(stdout, b'hello')

def test_utf8_output(self):
self.cmd_out[sentinel.stdout] = BytesIO(u'Žarn§'.encode('utf-8'))
self.cmd_out[sentinel.stderr] = BytesIO(u'لرئيسية'.encode('utf-8'))
(rc, stdout, stderr) = self.module.run_command('/bin/something_ugly')
self.assertEqual(rc, 0)
self.assertEqual(stdout.decode('utf-8'), u'Žarn§')
self.assertEqual(stderr.decode('utf-8'), u'لرئيسية')
# module_utils function. On py3 it returns text and py2 it returns
# bytes because it's returning native strings
if PY3:
self.assertEqual(stdout, u'Žarn§')
self.assertEqual(stderr, u'لرئيسية')
else:
self.assertEqual(stdout.decode('utf-8'), u'Žarn§')
self.assertEqual(stderr.decode('utf-8'), u'لرئيسية')

2 changes: 1 addition & 1 deletion test/units/module_utils/test_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class TestSunOSVirtual(BaseTestFactsPlatform):

BIND_MOUNTS = ['/not/a/real/bind_mount']

FINDMNT_OUTPUT = b"""
FINDMNT_OUTPUT = u"""
/sys sysfs sysfs rw,nosuid,nodev,noexec,relatime,seclabel
/proc proc proc rw,nosuid,nodev,noexec,relatime
/dev devtmpfs devtmpfs rw,nosuid,seclabel,size=8044400k,nr_inodes=2011100,mode=755
Expand Down

0 comments on commit 08a58ae

Please sign in to comment.