test_idf_py.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2019 Espressif Systems (Shanghai) CO LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import os
  17. import subprocess
  18. import sys
  19. import unittest
  20. try:
  21. from StringIO import StringIO
  22. except ImportError:
  23. from io import StringIO
  24. try:
  25. import idf
  26. except ImportError:
  27. sys.path.append('..')
  28. import idf
  29. current_dir = os.path.dirname(os.path.realpath(__file__))
  30. idf_py_path = os.path.join(current_dir, '..', 'idf.py')
  31. extension_path = os.path.join(current_dir, 'test_idf_extensions', 'test_ext')
  32. link_path = os.path.join(current_dir, '..', 'idf_py_actions', 'test_ext')
  33. class TestExtensions(unittest.TestCase):
  34. def test_extension_loading(self):
  35. try:
  36. os.symlink(extension_path, link_path)
  37. os.environ['IDF_EXTRA_ACTIONS_PATH'] = os.path.join(current_dir, 'extra_path')
  38. output = subprocess.check_output([sys.executable, idf_py_path, '--help'],
  39. env=os.environ).decode('utf-8', 'ignore')
  40. self.assertIn('--test-extension-option', output)
  41. self.assertIn('test_subcommand', output)
  42. self.assertIn('--some-extension-option', output)
  43. self.assertIn('extra_subcommand', output)
  44. finally:
  45. os.remove(link_path)
  46. def test_extension_execution(self):
  47. try:
  48. os.symlink(extension_path, link_path)
  49. os.environ['IDF_EXTRA_ACTIONS_PATH'] = ';'.join([os.path.join(current_dir, 'extra_path')])
  50. output = subprocess.check_output(
  51. [sys.executable, idf_py_path, '--some-extension-option=awesome', 'test_subcommand', 'extra_subcommand'],
  52. env=os.environ).decode('utf-8', 'ignore')
  53. self.assertIn('!!! From some global callback: awesome', output)
  54. self.assertIn('!!! From some subcommand', output)
  55. self.assertIn('!!! From test global callback: test', output)
  56. self.assertIn('!!! From some subcommand', output)
  57. finally:
  58. os.remove(link_path)
  59. def test_hidden_commands(self):
  60. try:
  61. os.symlink(extension_path, link_path)
  62. os.environ['IDF_EXTRA_ACTIONS_PATH'] = ';'.join([os.path.join(current_dir, 'extra_path')])
  63. output = subprocess.check_output([sys.executable, idf_py_path, '--help'],
  64. env=os.environ).decode('utf-8', 'ignore')
  65. self.assertIn('test_subcommand', output)
  66. self.assertNotIn('hidden_one', output)
  67. finally:
  68. os.remove(link_path)
  69. class TestDependencyManagement(unittest.TestCase):
  70. def test_dependencies(self):
  71. result = idf.init_cli()(
  72. args=['--dry-run', 'flash'],
  73. standalone_mode=False,
  74. )
  75. self.assertEqual(['flash'], list(result.keys()))
  76. def test_order_only_dependencies(self):
  77. result = idf.init_cli()(
  78. args=['--dry-run', 'build', 'fullclean', 'all'],
  79. standalone_mode=False,
  80. )
  81. self.assertEqual(['fullclean', 'all'], list(result.keys()))
  82. def test_repeated_dependencies(self):
  83. result = idf.init_cli()(
  84. args=['--dry-run', 'fullclean', 'app', 'fullclean', 'fullclean'],
  85. standalone_mode=False,
  86. )
  87. self.assertEqual(['fullclean', 'app'], list(result.keys()))
  88. def test_complex_case(self):
  89. result = idf.init_cli()(
  90. args=['--dry-run', 'clean', 'monitor', 'clean', 'fullclean', 'flash'],
  91. standalone_mode=False,
  92. )
  93. self.assertEqual(['fullclean', 'clean', 'flash', 'monitor'], list(result.keys()))
  94. def test_dupplicated_commands_warning(self):
  95. capturedOutput = StringIO()
  96. sys.stderr = capturedOutput
  97. idf.init_cli()(
  98. args=['--dry-run', 'clean', 'monitor', 'build', 'clean', 'fullclean', 'all'],
  99. standalone_mode=False,
  100. )
  101. sys.stderr = sys.__stderr__
  102. self.assertIn(
  103. 'WARNING: Commands "all", "clean" are found in the list of commands more than once.',
  104. capturedOutput.getvalue())
  105. sys.stderr = capturedOutput
  106. idf.init_cli()(
  107. args=['--dry-run', 'clean', 'clean'],
  108. standalone_mode=False,
  109. )
  110. sys.stderr = sys.__stderr__
  111. self.assertIn(
  112. 'WARNING: Command "clean" is found in the list of commands more than once.', capturedOutput.getvalue())
  113. class TestVerboseFlag(unittest.TestCase):
  114. def test_verbose_messages(self):
  115. output = subprocess.check_output(
  116. [
  117. sys.executable,
  118. idf_py_path,
  119. '-C%s' % current_dir,
  120. '-v',
  121. 'test-verbose',
  122. ], env=os.environ).decode('utf-8', 'ignore')
  123. self.assertIn('Verbose mode on', output)
  124. def test_verbose_messages_not_shown_by_default(self):
  125. output = subprocess.check_output(
  126. [
  127. sys.executable,
  128. idf_py_path,
  129. '-C%s' % current_dir,
  130. 'test-verbose',
  131. ], env=os.environ).decode('utf-8', 'ignore')
  132. self.assertIn('Output from test-verbose', output)
  133. self.assertNotIn('Verbose mode on', output)
  134. class TestGlobalAndSubcommandParameters(unittest.TestCase):
  135. def test_set_twice_same_value(self):
  136. """Can set -D twice: globally and for subcommand if values are the same"""
  137. idf.init_cli()(
  138. args=['--dry-run', '-DAAA=BBB', '-DCCC=EEE', 'build', '-DAAA=BBB', '-DCCC=EEE'],
  139. standalone_mode=False,
  140. )
  141. def test_set_twice_different_values(self):
  142. """Cannot set -D twice: for command and subcommand of idf.py (with different values)"""
  143. with self.assertRaises(idf.FatalError):
  144. idf.init_cli()(
  145. args=['--dry-run', '-DAAA=BBB', 'build', '-DAAA=EEE', '-DCCC=EEE'],
  146. standalone_mode=False,
  147. )
  148. class TestDeprecations(unittest.TestCase):
  149. def test_exit_with_error_for_subcommand(self):
  150. try:
  151. subprocess.check_output([sys.executable, idf_py_path, '-C%s' % current_dir, 'test-2'], env=os.environ,
  152. stderr=subprocess.STDOUT)
  153. except subprocess.CalledProcessError as e:
  154. self.assertIn('Error: Command "test-2" is deprecated and was removed.', e.output.decode('utf-8', 'ignore'))
  155. def test_exit_with_error_for_option(self):
  156. try:
  157. subprocess.check_output([sys.executable, idf_py_path, '-C%s' % current_dir, '--test-5=asdf'],
  158. env=os.environ, stderr=subprocess.STDOUT)
  159. except subprocess.CalledProcessError as e:
  160. self.assertIn('Error: Option "test_5" is deprecated since v2.0 and was removed in v3.0.',
  161. e.output.decode('utf-8', 'ignore'))
  162. def test_deprecation_messages(self):
  163. output = subprocess.check_output(
  164. [
  165. sys.executable,
  166. idf_py_path,
  167. '-C%s' % current_dir,
  168. '--test-0=a',
  169. '--test-1=b',
  170. '--test-2=c',
  171. '--test-3=d',
  172. 'test-0',
  173. '--test-sub-0=sa',
  174. '--test-sub-1=sb',
  175. 'ta',
  176. 'test-1',
  177. ],
  178. env=os.environ, stderr=subprocess.STDOUT).decode('utf-8', 'ignore')
  179. self.assertIn('Warning: Option "test_sub_1" is deprecated and will be removed in future versions.', output)
  180. self.assertIn(
  181. 'Warning: Command "test-1" is deprecated and will be removed in future versions. '
  182. 'Please use alternative command.', output)
  183. self.assertIn('Warning: Option "test_1" is deprecated and will be removed in future versions.', output)
  184. self.assertIn(
  185. 'Warning: Option "test_2" is deprecated and will be removed in future versions. '
  186. 'Please update your parameters.', output)
  187. self.assertIn('Warning: Option "test_3" is deprecated and will be removed in future versions.', output)
  188. self.assertNotIn('"test-0" is deprecated', output)
  189. self.assertNotIn('"test_0" is deprecated', output)
  190. if __name__ == '__main__':
  191. unittest.main()