1#! /usr/bin/env vpython3 2# Copyright 2020 The Chromium Authors 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5"""Tests for ini.py.""" 6 7 8import os 9import sys 10import textwrap 11import unittest 12 13sys.path.append( 14 os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) 15from pylib.local.emulator import ini 16 17 18class IniTest(unittest.TestCase): 19 def testLoadsBasic(self): 20 ini_str = textwrap.dedent("""\ 21 foo.bar = 1 22 foo.baz= example 23 bar.bad =/path/to/thing 24 25 [section_1] 26 foo.bar = 1 27 foo.baz= example 28 29 [section_2] 30 foo.baz= example 31 bar.bad =/path/to/thing 32 33 [section_1] 34 bar.bad =/path/to/thing 35 """) 36 expected = { 37 'foo.bar': '1', 38 'foo.baz': 'example', 39 'bar.bad': '/path/to/thing', 40 'section_1': { 41 'foo.bar': '1', 42 'foo.baz': 'example', 43 'bar.bad': '/path/to/thing', 44 }, 45 'section_2': { 46 'foo.baz': 'example', 47 'bar.bad': '/path/to/thing', 48 }, 49 } 50 self.assertEqual(expected, ini.loads(ini_str)) 51 52 def testLoadsDuplicatedKeysStrictFailure(self): 53 ini_str = textwrap.dedent("""\ 54 foo.bar = 1 55 foo.baz = example 56 bar.bad = /path/to/thing 57 foo.bar = duplicate 58 """) 59 with self.assertRaises(ValueError): 60 ini.loads(ini_str, strict=True) 61 62 def testLoadsDuplicatedKeysInSectionStrictFailure(self): 63 ini_str = textwrap.dedent("""\ 64 [section_1] 65 foo.bar = 1 66 foo.baz = example 67 bar.bad = /path/to/thing 68 foo.bar = duplicate 69 """) 70 with self.assertRaises(ValueError): 71 ini.loads(ini_str, strict=True) 72 73 def testLoadsPermissive(self): 74 ini_str = textwrap.dedent("""\ 75 foo.bar = 1 76 foo.baz = example 77 bar.bad = /path/to/thing 78 foo.bar = duplicate 79 80 [section_1] 81 foo.bar = 1 82 foo.baz = example 83 bar.bad = /path/to/thing 84 foo.bar = duplicate 85 """) 86 expected = { 87 'foo.bar': 'duplicate', 88 'foo.baz': 'example', 89 'bar.bad': '/path/to/thing', 90 'section_1': { 91 'foo.bar': 'duplicate', 92 'foo.baz': 'example', 93 'bar.bad': '/path/to/thing', 94 }, 95 } 96 self.assertEqual(expected, ini.loads(ini_str, strict=False)) 97 98 def testDumpsBasic(self): 99 ini_contents = { 100 'foo.bar': '1', 101 'foo.baz': 'example', 102 'bar.bad': '/path/to/thing', 103 'section_2': { 104 'foo.baz': 'example', 105 'bar.bad': '/path/to/thing', 106 }, 107 'section_1': { 108 'foo.bar': '1', 109 'foo.baz': 'example', 110 }, 111 } 112 # ini.dumps is expected to dump to string alphabetically 113 # by key and section name. 114 expected = textwrap.dedent("""\ 115 bar.bad = /path/to/thing 116 foo.bar = 1 117 foo.baz = example 118 119 [section_1] 120 foo.bar = 1 121 foo.baz = example 122 123 [section_2] 124 bar.bad = /path/to/thing 125 foo.baz = example 126 """) 127 self.assertEqual(expected, ini.dumps(ini_contents)) 128 129 def testDumpsSections(self): 130 ini_contents = { 131 'section_2': { 132 'foo.baz': 'example', 133 'bar.bad': '/path/to/thing', 134 }, 135 'section_1': { 136 'foo.bar': '1', 137 'foo.baz': 'example', 138 }, 139 } 140 # ini.dumps is expected to dump to string alphabetically 141 # by key first, and then by section and the associated keys 142 expected = textwrap.dedent("""\ 143 [section_1] 144 foo.bar = 1 145 foo.baz = example 146 147 [section_2] 148 bar.bad = /path/to/thing 149 foo.baz = example 150 """) 151 self.assertEqual(expected, ini.dumps(ini_contents)) 152 153 154if __name__ == '__main__': 155 unittest.main() 156