test_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. import os
  20. import sys
  21. import unittest
  22. import unittest.mock
  23. from pwman.util import config
  24. if sys.version_info.major > 2:
  25. from configparser import NoSectionError
  26. else:
  27. from ConfigParser import NoSectionError
  28. with open('testfile.conf', 'w') as f:
  29. f.write("""
  30. [Global]
  31. xsel = /usr/bin/xsel
  32. colors = yes
  33. umask = 0100
  34. cls_timeout = 5
  35. [Database]
  36. type = SQLite
  37. """)
  38. class TestFindConfigWindows(unittest.TestCase):
  39. def test_windows_platfrom(self):
  40. with unittest.mock.patch('platform.system') as pl:
  41. pl.return_value = 'Windows'
  42. os.environ['APPDATA'] = 'balls'
  43. cdir = config.find_config_dir('zzzz')
  44. self.assertEqual(
  45. os.path.expandvars(os.path.join('$APPDATA', 'zzzz')),
  46. cdir)
  47. class TestFindConfigCompat(unittest.TestCase):
  48. c_path = os.path.expanduser("~/.zzzz_compat_posix")
  49. def setUp(self):
  50. if not os.path.exists(self.c_path):
  51. os.mkdir(self.c_path)
  52. def tearDown(self):
  53. os.rmdir(self.c_path)
  54. def test_compat(self):
  55. cdir = config.find_config_dir('zzzz_compat_posix')
  56. self.assertEqual(self.c_path, cdir)
  57. class TestFindConfigXDG(unittest.TestCase):
  58. c_path = os.path.expanduser("~/.zzzz_posix")
  59. def setUp(self):
  60. if os.path.exists(self.c_path):
  61. os.rmdir(self.c_path)
  62. def test_new_scheme(self):
  63. # assert we get xdg_fine with Linux
  64. cdir = config.find_config_dir('zzzz_posix')
  65. self.assertEqual(cdir, os.path.expanduser("~/.config/zzzz_posix"))
  66. class TestConfig(unittest.TestCase):
  67. @classmethod
  68. def tearDownClass(cls):
  69. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  70. 'import_file.csv'):
  71. try:
  72. os.unlink(item)
  73. except OSError:
  74. continue
  75. def setUp(self):
  76. self.conf = config.Config(filename='testfile.conf',
  77. defaults=config.default_config)
  78. def test_has_defaults(self):
  79. self.assertTrue(self.conf.parser.has_section('Readline'))
  80. def test_has_user_history(self):
  81. path = os.path.expanduser(os.path.join("~/.pwman", "history"))
  82. config = self.conf.get_value('Readline', 'history')
  83. self.assertEqual(path, config)
  84. def test_has_user_db(self):
  85. self.assertNotEqual(os.path.expanduser('~/.pwman/pwman.db'),
  86. self.conf.get_value('Database', 'filename'))
  87. def test_wrong_config(self):
  88. with open('wrong_conf.conf', 'w') as f:
  89. f.write("""
  90. [Encryption
  91. algorithm = Blowfish
  92. """)
  93. self.assertRaises(config.ConfigException, config.Config,
  94. 'wrong_conf.conf')
  95. def test_set_value(self):
  96. self.conf.set_value('Global', 'colors', 'no')
  97. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  98. def test_set_value_wrong(self):
  99. self.assertRaises(NoSectionError,
  100. self.conf.set_value, *('Error', 'colors', 'no'))
  101. def test_get_pass_conf(self):
  102. ans = config.get_pass_conf(self.conf)
  103. self.assertFalse(any(ans))
  104. def test_open_file(self):
  105. cfg = config.Config("DoesNotExist")
  106. cfg.save()
  107. self.assertTrue(os.path.exists("DoesNotExist"))
  108. os.unlink("DoesNotExist")
  109. if __name__ == '__main__':
  110. unittest.main(verbosity=2)