You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_source_file.py 3.7 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Test SourceFile class."""
  16. import os
  17. import shutil
  18. import stat
  19. import tempfile
  20. import pytest
  21. from mindinsight.wizard.base.source_file import SourceFile
  22. from tests.ut.wizard.utils import generate_file
  23. class TestSourceFile:
  24. """Test SourceFile"""
  25. def setup_method(self):
  26. """Setup before call test method."""
  27. self._input_dir = tempfile.mkdtemp()
  28. self._output_dir = tempfile.mkdtemp()
  29. def teardown_method(self):
  30. """Tear down after call test method."""
  31. self._remove_dirs()
  32. self._input_dir = None
  33. self._output_dir = None
  34. def _remove_dirs(self):
  35. """Recursively delete a directory tree."""
  36. for temp_dir in [self._input_dir, self._output_dir]:
  37. if temp_dir and os.path.exists(temp_dir):
  38. shutil.rmtree(temp_dir)
  39. @staticmethod
  40. def _generate_file(file, stat_mode):
  41. """Create a file and write content."""
  42. generate_file(file, "template file.", stat_mode)
  43. @pytest.mark.parametrize('params', [{
  44. 'file_relative_path': 'src/config.py',
  45. 'template_file_path': 'src/config.py-tpl'
  46. }, {
  47. 'file_relative_path': 'src/lenet.py',
  48. 'template_file_path': 'src/lenet.py-tpl'
  49. }, {
  50. 'file_relative_path': 'README.md',
  51. 'template_file_path': 'README.md-tpl'
  52. }, {
  53. 'file_relative_path': 'train.py',
  54. 'template_file_path': 'train.py-tpl'
  55. }])
  56. def test_write_py(self, params):
  57. """Test write python script file"""
  58. source_file = SourceFile()
  59. source_file.file_relative_path = params['file_relative_path']
  60. source_file.template_file_path = os.path.join(self._input_dir, params['template_file_path'])
  61. self._generate_file(source_file.template_file_path, stat.S_IRUSR)
  62. # start write
  63. source_file.write(self._output_dir)
  64. output_file_path = os.path.join(self._output_dir, source_file.file_relative_path)
  65. assert os.access(output_file_path, os.F_OK | os.R_OK | os.W_OK)
  66. assert stat.filemode(os.stat(output_file_path).st_mode) == '-rw-------'
  67. @pytest.mark.parametrize('params', [{
  68. 'file_relative_path': 'scripts/run_eval.sh',
  69. 'template_file_path': 'scripts/run_eval.sh-tpl'
  70. }, {
  71. 'file_relative_path': 'run_distribute_train.sh',
  72. 'template_file_path': 'run_distribute_train.sh-tpl'
  73. }])
  74. def test_write_sh(self, params):
  75. """Test write shell script file"""
  76. source_file = SourceFile()
  77. source_file.file_relative_path = params['file_relative_path']
  78. source_file.template_file_path = os.path.join(self._input_dir, params['template_file_path'])
  79. self._generate_file(source_file.template_file_path, stat.S_IRUSR)
  80. # start write
  81. source_file.write(self._output_dir)
  82. output_file_path = os.path.join(self._output_dir, source_file.file_relative_path)
  83. assert os.access(output_file_path, os.F_OK | os.R_OK | os.W_OK | os.X_OK)
  84. assert stat.filemode(os.stat(output_file_path).st_mode) == '-rwx------'