[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] add has_encoding_man() to stem.util.system



commit 7932884d0e942fffad7f1deb80143fe5b57dcbea
Author: Edmund Wong <ewongbb@xxxxxxxxxxx>
Date:   Fri Sep 8 20:21:20 2017 +0800

    add has_encoding_man() to stem.util.system
---
 stem/util/system.py | 22 ++++++++++++++++++++--
 test/unit/manual.py | 17 +++++++++--------
 2 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/stem/util/system.py b/stem/util/system.py
index 47527795..53c06ac8 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -24,6 +24,8 @@ best-effort, providing **None** if the lookup fails.
   is_bsd - checks if we're running on the bsd family of operating systems
   is_slackware - checks if we're running on slackware
 
+  has_encoding_man - checks if the system's man command has --encoding=ascii available
+
   is_available - determines if a command is available on this system
   is_running - determines if a given process is running
   size_of - provides the memory usage of an object
@@ -311,6 +313,18 @@ class DaemonTask(object):
       conn.close()
 
 
+def has_encoding_man():
+  """
+  Checks if --encoding=ascii is available for man
+  """
+  retval = True 
+  if is_available('man'):
+    result = call('man --encoding=ascii man', [], error_return=True)
+    if 'unrecognized option' in result:
+      retval = False
+  return retval
+ 
+
 def is_windows():
   """
   Checks if we are running on Windows.
@@ -1224,7 +1238,8 @@ def files_with_suffix(base_path, suffix):
           yield os.path.join(root, filename)
 
 
-def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None, cwd = None, env = None):
+def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = None,
+         cwd = None, env = None, error_return = False):
   """
   call(command, default = UNDEFINED, ignore_exit_status = False)
 
@@ -1296,7 +1311,10 @@ def call(command, default = UNDEFINED, ignore_exit_status = False, timeout = Non
     exit_status = process.poll()
 
     if not ignore_exit_status and exit_status != 0:
-      raise OSError('%s returned exit status %i' % (command, exit_status))
+      if error_return:
+         return stderr
+      else:
+         OSError('%s returned exit status %i' % (command, exit_status))
 
     if stdout:
       return stdout.decode('utf-8', 'replace').splitlines()
diff --git a/test/unit/manual.py b/test/unit/manual.py
index cd850af9..9850e827 100644
--- a/test/unit/manual.py
+++ b/test/unit/manual.py
@@ -153,8 +153,8 @@ class TestManual(unittest.TestCase):
     expand our example (or add another).
     """
 
-    if stem.util.system.is_mac() or stem.util.system.is_bsd() or stem.util.system.is_slackware():
-      self.skipTest('(man lacks --encoding arg on OSX and BSD, #18660)')
+    if not stem.util.system.has_encoding_man():
+      self.skipTest('(man lacks --encoding arg on OSX, BSD, and Slackware #18660)')
       return
 
     manual = stem.manual.Manual.from_man(EXAMPLE_MAN_PATH)
@@ -174,7 +174,7 @@ class TestManual(unittest.TestCase):
     options. Unlike most other tests this doesn't require network access.
     """
 
-    if stem.util.system.is_mac() or stem.util.system.is_bsd() or stem.util.system.is_slackware():
+    if not stem.util.system.has_encoding_man():
       self.skipTest('(man lacks --encoding arg on OSX and BSD and Slackware, #18660)')
       return
 
@@ -202,8 +202,8 @@ class TestManual(unittest.TestCase):
     Check that we can save and reload manuals as a config.
     """
 
-    if stem.util.system.is_mac() or stem.util.system.is_bsd():
-      self.skipTest('(man lacks --encoding arg on OSX and BSD, #18660)')
+    if not stem.util.system.has_encoding_man():
+      self.skipTest('(man lacks --encoding arg on OSX, BSD and Slackware, #18660)')
       return
 
     manual = stem.manual.Manual.from_man(EXAMPLE_MAN_PATH)
@@ -219,8 +219,8 @@ class TestManual(unittest.TestCase):
     Check that we can save and reload manuals as sqlite.
     """
 
-    if stem.util.system.is_mac() or stem.util.system.is_bsd():
-      self.skipTest('(man lacks --encoding arg on OSX and BSD, #18660)')
+    if not stem.util.system.has_encoding_man():
+      self.skipTest('(man lacks --encoding arg on OSX, BSD, and Slackware #18660)')
       return
 
     manual = stem.manual.Manual.from_man(EXAMPLE_MAN_PATH)
@@ -297,8 +297,9 @@ class TestManual(unittest.TestCase):
     self.assertEqual(b'a2x output', output.getvalue())
     call_mock.assert_called_once_with('a2x -f manpage /no/such/path/tor.1.txt')
 
-  @patch('stem.util.system.is_mac', Mock(return_value = False))
+  @patch('stem.util.system.has_encoding_man', Mock(return_value = True))
   @patch('stem.util.system.is_bsd', Mock(return_value = False))
+  @patch('stem.util.system.is_mac', Mock(return_value = False))
   @patch('stem.util.system.is_slackware', Mock(return_value = False))
   @patch('stem.util.system.call', Mock(side_effect = OSError('man --encoding=ascii -P cat tor returned exit status 16')))
   def test_from_man_when_manual_is_unavailable(self):



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits