[tor-commits] [stem/master] Making system unit tests more platform independent

atagar at torproject.org atagar at torproject.org
Sun Apr 8 21:43:23 UTC 2012


commit d9dc5babf533460975895cca1d6cde14dcea8e41
Author: Damian Johnson <atagar at torproject.org>
Date:   Sun Apr 8 14:28:00 2012 -0700

    Making system unit tests more platform independent
    
    On Windows the alternate path separator caused the unit tests for expand_path()
    to fail. Switching the expand_path() and is_relative_path() to not make any
    effort to work on Windows and fixing the test to be platform independent. In
    the future we should make these funtions work on Windows but this isn't vital
    at the moment and a pita due to its '[A-Z]:\\' prefixes (I really don't wanna
    do a regex just to check if a path is absolute...).
---
 stem/util/system.py      |   42 ++++++++++++++++++++++++------------------
 test/unit/util/system.py |   13 +++++++++++--
 2 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/stem/util/system.py b/stem/util/system.py
index f54e346..16d1832 100644
--- a/stem/util/system.py
+++ b/stem/util/system.py
@@ -498,7 +498,10 @@ def is_relative_path(path):
     False otherwise
   """
   
-  return path and not path.startswith("/")
+  if platform.system() == "Windows":
+    return False # TODO: implement
+  else:
+    return path and not path.startswith("/")
 
 def expand_path(path, cwd = None):
   """
@@ -515,26 +518,29 @@ def expand_path(path, cwd = None):
     str of the path expanded to be an absolute path
   """
   
-  relative_path = path
-  
-  if not path or path[0] == "/":
-    # empty or already absolute - nothing to do
-    pass
-  elif path.startswith("~"):
-    # prefixed with a ~ or ~user entry
-    relative_path = os.path.expanduser(path)
+  if platform.system() == "Windows":
+    return path # TODO: implement
   else:
-    # relative path, expand with the cwd
-    if not cwd: cwd = os.getcwd()
+    relative_path = path
     
-    # we'll be dealing with both "my/path/" and "./my/path" entries, so
-    # cropping the later
-    if path.startswith("./"): path = path[2:]
-    elif path == ".": path = ""
+    if not path or path[0] == "/":
+      # empty or already absolute - nothing to do
+      pass
+    elif path.startswith("~"):
+      # prefixed with a ~ or ~user entry
+      relative_path = os.path.expanduser(path)
+    else:
+      # relative path, expand with the cwd
+      if not cwd: cwd = os.getcwd()
+      
+      # we'll be dealing with both "my/path/" and "./my/path" entries, so
+      # cropping the later
+      if path.startswith("./"): path = path[2:]
+      elif path == ".": path = ""
+      
+      relative_path = os.path.join(cwd, path)
     
-    relative_path = os.path.join(cwd, path)
-  
-  return relative_path.rstrip("/")
+    return relative_path.rstrip("/")
 
 def call(command, suppress_exc = True):
   """
diff --git a/test/unit/util/system.py b/test/unit/util/system.py
index 8ad63a0..b09dca7 100644
--- a/test/unit/util/system.py
+++ b/test/unit/util/system.py
@@ -5,6 +5,8 @@ these tests actually make system calls, use proc, or otherwise deal with the
 system running the tests.
 """
 
+import os
+import platform
 import functools
 import unittest
 import stem.util.proc
@@ -282,23 +284,28 @@ class TestSystem(unittest.TestCase):
       expected_response = 1 if test_input == "1111" else 0
       self.assertEquals(expected_response, system.get_bsd_jail_id(test_input))
   
-  def test_is_relative_path(self):
+  def test_is_relative_path_unix(self):
     """
     Tests the is_relative_path function.
     """
     
+    mocking.mock(platform.system, mocking.return_value("Linux"))
     self.assertTrue(system.is_relative_path("hello/world"))
     self.assertTrue(system.is_relative_path("~/hello/world"))
     self.assertTrue(system.is_relative_path("~user/hello/world"))
     self.assertFalse(system.is_relative_path("/tmp/hello/world"))
   
-  def test_expand_path(self):
+  def test_expand_path_unix(self):
     """
     Tests the expand_path function. This does not exercise home directory
     expansions since that deals with our environment (that's left to integ
     tests).
     """
     
+    mocking.mock(platform.system, mocking.return_value("Linux"))
+    original_path_sep = os.path.sep
+    os.path.sep = "/"
+    
     self.assertEquals("", system.expand_path(""))
     self.assertEquals("/tmp", system.expand_path("/tmp"))
     self.assertEquals("/tmp", system.expand_path("/tmp/"))
@@ -306,4 +313,6 @@ class TestSystem(unittest.TestCase):
     self.assertEquals("/tmp", system.expand_path("./", "/tmp"))
     self.assertEquals("/tmp/foo", system.expand_path("foo", "/tmp"))
     self.assertEquals("/tmp/foo", system.expand_path("./foo", "/tmp"))
+    
+    os.path.sep = original_path_sep
 





More information about the tor-commits mailing list