Commits

John Chandler committed bf74a8d

Tweaks to hierarchy to improve testability, though it feels a bit messy

  • Participants
  • Parent commits edab8c5

Comments (0)

Files changed (2)

File qadpid/hierarchy.py

 """
 import os
 
+
+def _read_file( filepath ):
+    """ Reads a file, or returns an empty string if there was a problem. This hides something I feel bad about """
+    try:
+        file_object = open( filepath, "r" )
+        file_text = file_object.read()
+        file_object.close()
+        return file_object
+    
+    except IOError:
+        # It's Quick and Dirty, if we can't open a file we don't care at the moment
+        # also means I can test without a real directory structure with files
+        return ""
+
+
 class PHPHierarchyCreator( object ):
     """ Hierarchy Creator for PHP files
         Not entirely sure about this - needs a redesign. Also, 'Hierarchy' doesn't really make sense.
     """
-    def __init__( self ):
+    def __init__( self, parser ):
         self.hierarchy = {}
+        self.parser    = parser
 
     def add( self, directory ):
         """ Takes a (dirpath, dirs, files) tuple (e.g. as returned by os.walk)
             Adds .php files to the hierarchy
         """
         dirpath, _, files = directory
-        for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:
-            self.hierarchy[ filepath ] = None
+        for filepath in [ os.path.join( dirpath, x ) for x in files if x.endswith(".php") ]:    
+            self.hierarchy[ filepath ] = self.parser.parse( _read_file(filepath) )

File tests/test_hierarchy.py

 from qadpid.hierarchy import PHPHierarchyCreator
 
 
+class FakeParser( object ):
+    def parse( self, _ ):
+        return None
+
+
 class TestHierarchyCreator( unittest.TestCase ):
     """ Tests the HierarchyCreator """
     def setUp( self ):
-        self.creator = PHPHierarchyCreator()
+        self.creator = PHPHierarchyCreator( parser=FakeParser() )
     
     def test_no_hierarchy( self ):
         """ Tests result when we have nothing to do """