Commits

Kumar McMillan  committed 5aaf47b

Fixes bug where nose crashes if imported from a zip archive. Specifically, uses the proper method to get the contents of nose/usage.txt now.

  • Participants
  • Parent commits 3aeafbb

Comments (0)

Files changed (2)

File nose/core.py

                 print
             
     def usage(cls):
+        import nose
+        if hasattr(nose, '__loader__'):
+            ld = nose.__loader__
+            if hasattr(ld, 'zipfile'):
+                # nose was imported from a zipfile
+                return ld.get_data(
+                        os.path.join(ld.prefix, 'nose', 'usage.txt'))
         return open(os.path.join(
                 os.path.dirname(__file__), 'usage.txt'), 'r').read()
     usage = classmethod(usage)

File unit_tests/test_core.py

         print "Done running"
         stdout_after = sys.stdout
         self.assertEqual(stdout, stdout_after)
+
+class Undefined(object):
+    pass
+
+class TestUsage(unittest.TestCase):
+    
+    def test_from_directory(self):
+        usage_txt = nose.core.TestProgram.usage()
+        assert usage_txt.startswith('nose collects tests automatically'), (
+                "Unexpected usage: '%s...'" % usage_txt[0:50].replace("\n", '\n'))
+    
+    def test_from_zip(self):
+        requested_data = []
+        
+        # simulates importing nose from a zip archive
+        # with a zipimport.zipimporter instance
+        class fake_zipimporter(object):
+            
+            prefix = ''
+            zipfile = '<fake zipfile>'
+            
+            def get_data(self, path):
+                requested_data.append(path)
+                return "<usage>"
+                    
+        existing_loader = getattr(nose, '__loader__', Undefined)
+        try:
+            nose.__loader__ = fake_zipimporter()
+            usage_txt = nose.core.TestProgram.usage()
+            self.assertEqual(usage_txt, '<usage>')
+            self.assertEqual(requested_data, ['nose/usage.txt'])
+        finally:
+            if existing_loader is not Undefined:
+                nose.__loader__ = existing_loader
+            else:
+                del nose.__loader__
+    
+    def test_from_zip_with_prefix(self):
+        requested_data = []
+        
+        # simulates importing nose from a zip archive
+        # with a zipimport.zipimporter instance
+        class fake_zipimporter(object):
+            
+            prefix = 'PREFIX'
+            zipfile = '<fake zipfile>'
+            
+            def get_data(self, path):
+                requested_data.append(path)
+                return "<usage>"
+                
+        existing_loader = getattr(nose, '__loader__', Undefined)
+        try:            
+            nose.__loader__ = fake_zipimporter()
+            usage_txt = nose.core.TestProgram.usage()
+            self.assertEqual(usage_txt, '<usage>')
+            self.assertEqual(requested_data, ['PREFIX/nose/usage.txt'])
+        finally:
+            if existing_loader is not Undefined:
+                nose.__loader__ = existing_loader
+            else:
+                del nose.__loader__
         
 if __name__ == '__main__':
     unittest.main()