test_win_chcp.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. Test win_chcp
  3. """
  4. from salt.exceptions import CodePageError
  5. from salt.utils import platform, win_chcp
  6. from tests.support.unit import TestCase, skipIf
  7. @skipIf(not platform.is_windows(), "Windows only tests!")
  8. class CHCPTest(TestCase):
  9. """
  10. Test case for salt.utils.win_chcp
  11. """
  12. @classmethod
  13. def setUpClass(cls):
  14. # Stores the initial code page for _reset_code_page()
  15. # Intentionally does not catch any exception, to find out which that would be.
  16. # That exception would then be added to cmdmod.py
  17. cls._chcp_code = win_chcp.get_codepage_id()
  18. @classmethod
  19. def tearDownClass(cls):
  20. cls._chcp_code = None
  21. def setUp(self):
  22. win_chcp.set_codepage_id(self._chcp_code)
  23. def tearDown(self):
  24. win_chcp.set_codepage_id(self._chcp_code)
  25. def test_get_and_set_code_page(self):
  26. for page in (20424, "20866", 437, 65001, "437"):
  27. self.assertEqual(win_chcp.set_codepage_id(page), int(page))
  28. self.assertEqual(win_chcp.get_codepage_id(), int(page))
  29. def test_bad_page_code(self):
  30. with win_chcp.chcp(437):
  31. self.assertEqual(win_chcp.get_codepage_id(), 437)
  32. bad_codes = ("0", "bad code", 1234, -34, "437 dogs", "(*&^(*^%&$%&")
  33. for page in bad_codes:
  34. self.assertEqual(win_chcp.set_codepage_id(page), -1)
  35. self.assertEqual(win_chcp.get_codepage_id(), 437)
  36. for page in bad_codes:
  37. self.assertRaises(CodePageError, win_chcp.set_codepage_id, page, True)