| 1 |  |  | import os | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | import unittest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import tempfile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | from coalib.output.ConfWriter import ConfWriter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | from coalib.parsing.ConfParser import ConfParser | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ConfWriterTest(unittest.TestCase): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     example_file = ("to be ignored \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |                     "    save=true\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |                     "    a_default, another = val \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |                     "    TEST = tobeignored  # thats a comment \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |                     "    test = push \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |                     "    t = \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |                     "    [MakeFiles] \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |                     "     j  , ANother = a \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |                     "                   multiline \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |                     "                   value \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |                     "    ; just a omment \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |                     "    ; just a omment \n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |                     "    key\\ space = value space\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                     "    key\\=equal = value=equal\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |                     "    key\\\\backslash = value\\\\backslash\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                     "    key\\,comma = value,comma\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                     "    key\\#hash = value\\#hash\n" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                     "    key\\.dot = value.dot\n") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     def setUp(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         self.file = os.path.join(tempfile.gettempdir(), "ConfParserTestFile") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         with open(self.file, "w", encoding='utf-8') as file: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             file.write(self.example_file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         self.conf_parser = ConfParser() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         self.write_file_name = os.path.join(tempfile.gettempdir(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                                             "ConfWriterTestFile") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         self.uut = ConfWriter(self.write_file_name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     def tearDown(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         self.uut.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         os.remove(self.file) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         os.remove(self.write_file_name) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     def test_exceptions(self): | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         self.assertRaises(TypeError, self.uut.write_section, 5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     def test_write(self): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         result_file = ["[Default]\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                        "save = true\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                        "a_default, another = val\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                        "# thats a comment\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                        "test = push\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                        "t = \n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                        "\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                        "[MakeFiles]\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                        "j, ANother = a\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                        "multiline\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                        "value\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                        "; just a omment\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                        "; just a omment\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                        "key\\ space = value space\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                        "key\\=equal = value=equal\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                        "key\\\\backslash = value\\\\backslash\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                        "key\\,comma = value,comma\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                        "key\\#hash = value\\#hash\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                        "key\\.dot = value.dot\n"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         self.uut.write_sections(self.conf_parser.parse(self.file)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         self.uut.close() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         with open(self.write_file_name, "r") as f: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             lines = f.readlines() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 73 |  |  |         self.assertEqual(result_file, lines) | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  |