test_config.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. from pwman.util.config import find_config_dir
  25. from configparser import NoSectionError
  26. with open('testfile.conf', 'w') as f:
  27. f.write("""
  28. [Global]
  29. xsel = /usr/bin/xsel
  30. colors = yes
  31. umask = 0100
  32. cls_timeout = 5
  33. [Database]
  34. type = SQLite
  35. """)
  36. class TestFindConfigWindows(unittest.TestCase):
  37. def test_windows_platfrom(self):
  38. with unittest.mock.patch('platform.system') as pl:
  39. pl.return_value = 'Windows'
  40. os.environ['APPDATA'] = 'balls'
  41. cdir, data_dir = config.find_config_dir('zzzz')
  42. self.assertEqual(
  43. os.path.expandvars(os.path.join('$APPDATA', 'zzzz')),
  44. cdir)
  45. class TestFindConfigCompat(unittest.TestCase):
  46. c_path = os.path.expanduser("~/.zzzz_compat_posix")
  47. def setUp(self):
  48. if not os.path.exists(self.c_path):
  49. os.mkdir(self.c_path)
  50. def tearDown(self):
  51. os.rmdir(self.c_path)
  52. def test_compat(self):
  53. cdir, data_dir = config.find_config_dir('zzzz_compat_posix')
  54. self.assertEqual(self.c_path, cdir)
  55. class TestFindConfigXDG(unittest.TestCase):
  56. c_path = os.path.expanduser("~/.zzzz_posix")
  57. def setUp(self):
  58. if os.path.exists(self.c_path):
  59. os.rmdir(self.c_path)
  60. def test_new_scheme(self):
  61. # assert we get xdg_fine with Linux
  62. cdir, data_dir = config.find_config_dir('zzzz_posix')
  63. self.assertEqual(cdir, os.path.expanduser("~/.config/zzzz_posix"))
  64. class TestConfig(unittest.TestCase):
  65. @classmethod
  66. def tearDownClass(cls):
  67. for item in ('testfile.conf', 'wrong_conf.conf', 'dummy.cfg',
  68. 'import_file.csv'):
  69. try:
  70. os.unlink(item)
  71. except OSError:
  72. continue
  73. def setUp(self):
  74. self.conf = config.Config(filename='testfile.conf',
  75. defaults=config.default_config)
  76. def test_has_defaults(self):
  77. self.assertTrue(self.conf.parser.has_section('Readline'))
  78. def test_has_user_history(self):
  79. ddir = find_config_dir('pwman')[1]
  80. path = os.path.join(ddir, "history")
  81. config = self.conf.get_value('Readline', 'history')
  82. self.assertEqual(path, config)
  83. def test_has_user_db(self):
  84. self.assertNotEqual(os.path.join(config.find_config_dir("pwman")[1],'pwman.db'),
  85. self.conf.get_value('Database', 'filename'))
  86. def test_wrong_config(self):
  87. with open('wrong_conf.conf', 'w') as f:
  88. f.write("""
  89. [Encryption
  90. algorithm = Blowfish
  91. """)
  92. self.assertRaises(config.ConfigException, config.Config,
  93. 'wrong_conf.conf')
  94. def test_set_value(self):
  95. self.conf.set_value('Global', 'colors', 'no')
  96. self.assertEqual('no', self.conf.get_value('Global', 'colors'))
  97. def test_set_value_wrong(self):
  98. self.assertRaises(NoSectionError,
  99. self.conf.set_value, *('Error', 'colors', 'no'))
  100. def test_get_pass_conf(self):
  101. ans = config.get_pass_conf(self.conf)
  102. self.assertFalse(any(ans))
  103. def test_open_file(self):
  104. cfg = config.Config("DoesNotExist")
  105. cfg.save()
  106. self.assertTrue(os.path.exists("DoesNotExist"))
  107. os.unlink("DoesNotExist")
  108. if __name__ == '__main__':
  109. unittest.main(verbosity=2)