init.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. import argparse
  24. import qubes
  25. import qubes.tools
  26. import qubes.tests
  27. class TC_00_PropertyAction(qubes.tests.QubesTestCase):
  28. def test_000_default(self):
  29. parser = argparse.ArgumentParser()
  30. parser.add_argument('--property', '-p',
  31. action=qubes.tools.PropertyAction)
  32. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  33. args = parser.parse_args([])
  34. self.assertDictContainsSubset(
  35. {'defaultprop': 'defaultvalue'}, args.properties)
  36. def test_001_set_prop(self):
  37. parser = argparse.ArgumentParser()
  38. parser.add_argument('--property', '-p',
  39. action=qubes.tools.PropertyAction)
  40. args = parser.parse_args(['-p', 'testprop=testvalue'])
  41. self.assertDictContainsSubset(
  42. {'testprop': 'testvalue'}, args.properties)
  43. def test_002_set_prop_2(self):
  44. parser = argparse.ArgumentParser()
  45. parser.add_argument('--property', '-p',
  46. action=qubes.tools.PropertyAction)
  47. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  48. args = parser.parse_args(
  49. ['-p', 'testprop=testvalue', '-p', 'testprop2=testvalue2'])
  50. self.assertDictContainsSubset(
  51. {'testprop': 'testvalue', 'testprop2': 'testvalue2'},
  52. args.properties)
  53. def test_003_set_prop_with_default(self):
  54. parser = argparse.ArgumentParser()
  55. parser.add_argument('--property', '-p',
  56. action=qubes.tools.PropertyAction)
  57. parser.set_defaults(properties={'defaultprop': 'defaultvalue'})
  58. args = parser.parse_args(['-p', 'testprop=testvalue'])
  59. self.assertDictContainsSubset(
  60. {'testprop': 'testvalue', 'defaultprop': 'defaultvalue'},
  61. args.properties)
  62. def test_003_set_prop_override_default(self):
  63. # pylint: disable=invalid-name
  64. parser = argparse.ArgumentParser()
  65. parser.add_argument('--property', '-p',
  66. action=qubes.tools.PropertyAction)
  67. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  68. args = parser.parse_args(['-p', 'testprop=testvalue'])
  69. self.assertDictContainsSubset(
  70. {'testprop': 'testvalue'},
  71. args.properties)
  72. class TC_01_SinglePropertyAction(qubes.tests.QubesTestCase):
  73. def test_000_help(self):
  74. parser = argparse.ArgumentParser()
  75. action = parser.add_argument('--testprop', '-T',
  76. action=qubes.tools.SinglePropertyAction)
  77. self.assertIn('testprop', action.help)
  78. def test_001_help_const(self):
  79. parser = argparse.ArgumentParser()
  80. action = parser.add_argument('--testprop', '-T',
  81. action=qubes.tools.SinglePropertyAction,
  82. const='testvalue')
  83. self.assertIn('testvalue', action.help)
  84. def test_100_default(self):
  85. parser = argparse.ArgumentParser()
  86. parser.add_argument('--testprop', '-T',
  87. action=qubes.tools.SinglePropertyAction)
  88. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  89. args = parser.parse_args([])
  90. self.assertDictContainsSubset(
  91. {'testprop': 'defaultvalue'}, args.properties)
  92. def test_101_set_prop(self):
  93. parser = argparse.ArgumentParser()
  94. parser.add_argument('--testprop', '-T',
  95. action=qubes.tools.SinglePropertyAction)
  96. args = parser.parse_args(['-T', 'testvalue'])
  97. self.assertDictContainsSubset(
  98. {'testprop': 'testvalue'}, args.properties)
  99. def test_102_set_prop_dest(self):
  100. parser = argparse.ArgumentParser()
  101. parser.add_argument('--testprop', '-T', dest='otherprop',
  102. action=qubes.tools.SinglePropertyAction)
  103. args = parser.parse_args(['-T', 'testvalue'])
  104. self.assertDictContainsSubset(
  105. {'otherprop': 'testvalue'}, args.properties)
  106. def test_103_set_prop_const(self):
  107. parser = argparse.ArgumentParser()
  108. parser.add_argument('--testprop', '-T',
  109. action=qubes.tools.SinglePropertyAction,
  110. const='testvalue')
  111. args = parser.parse_args(['-T'])
  112. self.assertDictContainsSubset(
  113. {'testprop': 'testvalue'}, args.properties)
  114. def test_104_set_prop_positional(self):
  115. parser = argparse.ArgumentParser()
  116. parser.add_argument('testprop',
  117. action=qubes.tools.SinglePropertyAction)
  118. args = parser.parse_args(['testvalue'])
  119. self.assertDictContainsSubset(
  120. {'testprop': 'testvalue'}, args.properties)