| 1 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | # Copyright (C) 2011 - 2016 Satoru SATOH <ssato at redhat.com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | # | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | # pylint: disable=missing-docstring | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | import os.path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import tempfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import anyconfig.compat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | import anyconfig.mdicts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | from anyconfig.compat import OrderedDict | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | CNF_0 = dict(name="a", a=1, b=dict(b=[1, 2], c="C")) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | SCM_0 = {"type": "object", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |          "properties": { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |              "name": {"type": "string"}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |              "a": {"type": "integer"}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |              "b": {"type": "object", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |                    "properties": { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                        "b": {"type": "array", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                              "items": {"type": "integer"}}}}}} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | def selfdir(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     >>> os.path.exists(selfdir()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     return os.path.dirname(__file__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | def setup_workdir(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     >>> workdir = setup_workdir() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     >>> assert workdir != '.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     >>> assert workdir != '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     >>> os.path.exists(workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     >>> os.rmdir(workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     return tempfile.mkdtemp(dir="/tmp", prefix="python-anyconfig-tests-") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | def cleanup_workdir(workdir): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     FIXME: Danger! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     >>> workdir = setup_workdir() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     >>> os.path.exists(workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     >>> open(os.path.join(workdir, "workdir.stamp"), 'w').write("OK!\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     >>> cleanup_workdir(workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     >>> os.path.exists(workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     assert workdir != '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     assert workdir != '.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     os.system("rm -rf " + workdir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  | def dicts_equal(dic, ref, ordered=False): | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     """Compare (maybe nested) dicts. | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     """ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     fnc = list if ordered else len | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     if fnc(dic.keys()) != fnc(ref.keys()): | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         return False | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     for key in ref.keys(): | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         if key not in dic: | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             return False | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         (next_dic, next_ref) = (dic[key], ref[key]) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         if anyconfig.mdicts.is_dict_like(next_ref): | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             if not anyconfig.mdicts.is_dict_like(next_dic) or \ | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                     not dicts_equal(next_dic, next_ref): | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                 return False | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         else: | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |             if next_dic != next_ref: | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |                 return False | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |     return True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | def to_bytes(astr): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     Convert a string to bytes. Do nothing in python 2.6. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     """ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     return bytes(astr, 'utf-8') if anyconfig.compat.IS_PYTHON_3 else astr | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | class Test(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     def test_dicts_equal(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         dic0 = {'a': 1} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         dic1 = OrderedDict((('a', [1, 2, 3]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                             ('b', OrderedDict((('c', "CCC"), ))))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         dic2 = dic1.copy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         dic2["b"] = None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         dic3 = OrderedDict((('b', OrderedDict((('c', "CCC"), ))), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                             ('a', [1, 2, 3]))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         self.assertTrue(dicts_equal({}, {})) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         self.assertTrue(dicts_equal(dic0, dic0)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         self.assertTrue(dicts_equal(dic1, dic1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         self.assertTrue(dicts_equal(dic2, dic2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         self.assertTrue(dicts_equal(dic1, dic3)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         self.assertFalse(dicts_equal(dic0, {})) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         self.assertFalse(dicts_equal(dic0, dic1)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         self.assertFalse(dicts_equal(dic1, dic2)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         self.assertFalse(dicts_equal(dic1, dic3, ordered=True)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | # vim:sw=4:ts=4:et: | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |