init.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. parser = argparse.ArgumentParser()
  64. parser.add_argument('--property', '-p',
  65. action=qubes.tools.PropertyAction)
  66. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  67. args = parser.parse_args(['-p', 'testprop=testvalue'])
  68. self.assertDictContainsSubset(
  69. {'testprop': 'testvalue'},
  70. args.properties)
  71. class TC_01_SinglePropertyAction(qubes.tests.QubesTestCase):
  72. def test_000_help(self):
  73. parser = argparse.ArgumentParser()
  74. action = parser.add_argument('--testprop', '-T',
  75. action=qubes.tools.SinglePropertyAction)
  76. self.assertIn('testprop', action.help)
  77. def test_001_help_const(self):
  78. parser = argparse.ArgumentParser()
  79. action = parser.add_argument('--testprop', '-T',
  80. action=qubes.tools.SinglePropertyAction,
  81. const='testvalue')
  82. self.assertIn('testvalue', action.help)
  83. def test_100_default(self):
  84. parser = argparse.ArgumentParser()
  85. parser.add_argument('--testprop', '-T',
  86. action=qubes.tools.SinglePropertyAction)
  87. parser.set_defaults(properties={'testprop': 'defaultvalue'})
  88. args = parser.parse_args([])
  89. self.assertDictContainsSubset(
  90. {'testprop': 'defaultvalue'}, args.properties)
  91. def test_101_set_prop(self):
  92. parser = argparse.ArgumentParser()
  93. parser.add_argument('--testprop', '-T',
  94. action=qubes.tools.SinglePropertyAction)
  95. args = parser.parse_args(['-T', 'testvalue'])
  96. self.assertDictContainsSubset(
  97. {'testprop': 'testvalue'}, args.properties)
  98. def test_102_set_prop_dest(self):
  99. parser = argparse.ArgumentParser()
  100. parser.add_argument('--testprop', '-T', dest='otherprop',
  101. action=qubes.tools.SinglePropertyAction)
  102. args = parser.parse_args(['-T', 'testvalue'])
  103. self.assertDictContainsSubset(
  104. {'otherprop': 'testvalue'}, args.properties)
  105. def test_103_set_prop_const(self):
  106. parser = argparse.ArgumentParser()
  107. parser.add_argument('--testprop', '-T',
  108. action=qubes.tools.SinglePropertyAction,
  109. const='testvalue')
  110. args = parser.parse_args(['-T'])
  111. self.assertDictContainsSubset(
  112. {'testprop': 'testvalue'}, args.properties)
  113. def test_104_set_prop_positional(self):
  114. parser = argparse.ArgumentParser()
  115. parser.add_argument('testprop',
  116. action=qubes.tools.SinglePropertyAction)
  117. args = parser.parse_args(['testvalue'])
  118. self.assertDictContainsSubset(
  119. {'testprop': 'testvalue'}, args.properties)