test_setopt.py 1.3 KB

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