1import io
2import configparser
3
4from setuptools.command import setopt
5
6
7class TestEdit:
8    @staticmethod
9    def parse_config(filename):
10        parser = configparser.ConfigParser()
11        with io.open(filename, encoding='utf-8') as reader:
12            parser.read_file(reader)
13        return parser
14
15    @staticmethod
16    def write_text(file, content):
17        with io.open(file, 'wb') as strm:
18            strm.write(content.encode('utf-8'))
19
20    def test_utf8_encoding_retained(self, tmpdir):
21        """
22        When editing a file, non-ASCII characters encoded in
23        UTF-8 should be retained.
24        """
25        config = tmpdir.join('setup.cfg')
26        self.write_text(str(config), '[names]\njaraco=джарако')
27        setopt.edit_config(str(config), dict(names=dict(other='yes')))
28        parser = self.parse_config(str(config))
29        assert parser.get('names', 'jaraco') == 'джарако'
30        assert parser.get('names', 'other') == 'yes'
31
32    def test_case_retained(self, tmpdir):
33        """
34        When editing a file, case of keys should be retained.
35        """
36        config = tmpdir.join('setup.cfg')
37        self.write_text(str(config), '[names]\nFoO=bAr')
38        setopt.edit_config(str(config), dict(names=dict(oTher='yes')))
39        actual = config.read_text(encoding='ascii')
40        assert 'FoO' in actual
41        assert 'oTher' in actual
42