init.py 5.3 KB

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