|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# |
| 4 | +# Program: 3D Slicer |
| 5 | +# |
| 6 | +# Copyright (c) Kitware Inc. |
| 7 | +# |
| 8 | +# See COPYRIGHT.txt |
| 9 | +# or http://www.slicer.org/copyright/copyright.txt for details. |
| 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 | +# |
| 17 | +# This file was originally developed by Jean-Christophe Fillion-Robin, Kitware Inc. |
| 18 | +# and was partially funded by NIH grant 1U24CA194354-01 |
| 19 | +# |
| 20 | + |
| 21 | +import os |
| 22 | +import sys |
| 23 | +import tempfile |
| 24 | + |
| 25 | +from SlicerAppTesting import * |
| 26 | + |
| 27 | +""" |
| 28 | +This test verifies that a python module can add/modify python variables |
| 29 | +which will be preserved after Slicer startup is completed. |
| 30 | +
|
| 31 | +This test was based on SlicerStartupCompletedTest.py and it relies on |
| 32 | +SlicerStartupModuleTestHelperModule.py and the SlicerStartupModuleTestHelperPackage |
| 33 | +python package |
| 34 | +
|
| 35 | +It uses an output file for communication because on Windows, |
| 36 | +standard output is not always enabled. |
| 37 | +
|
| 38 | +Usage: |
| 39 | + SlicerStartupModuleTest.py /path/to/Slicer |
| 40 | +""" |
| 41 | + |
| 42 | +if __name__ == "__main__": |
| 43 | + debug = False |
| 44 | + # Set to True to: |
| 45 | + # * display the path of the expected test output file |
| 46 | + # * avoid deleting the created temporary directory |
| 47 | + |
| 48 | + if len(sys.argv) != 2: |
| 49 | + print(os.path.basename(sys.argv[0]) + " /path/to/Slicer") |
| 50 | + exit(EXIT_FAILURE) |
| 51 | + |
| 52 | + temporaryModuleDirPath = tempfile.mkdtemp().replace("\\", "/") |
| 53 | + os.makedirs(temporaryModuleDirPath + "/SlicerStartupModuleTestHelperScript", exist_ok=True) |
| 54 | + try: |
| 55 | + # Copy helper module that creates a file when startup completed event is received |
| 56 | + currentDirPath = os.path.dirname(__file__).replace("\\", "/") |
| 57 | + from shutil import copyfile,copytree |
| 58 | + copyfile(currentDirPath + "/SlicerStartupModuleTestHelperModule.py", |
| 59 | + temporaryModuleDirPath + "/SlicerStartupModuleTestHelperModule.py") |
| 60 | + copytree(currentDirPath + "/SlicerStartupModuleTestHelperPackage", |
| 61 | + temporaryModuleDirPath + "/SlicerStartupModuleTestHelperPackage" ) |
| 62 | + copyfile(currentDirPath + "/SlicerStartupModuleTestHelperScript.py", |
| 63 | + temporaryModuleDirPath + "/SlicerStartupModuleTestHelperScript/SlicerStartupModuleTestHelperScript.py") |
| 64 | + |
| 65 | + slicer_executable = os.path.expanduser(sys.argv[1]) |
| 66 | + common_args = [ |
| 67 | + "--testing", |
| 68 | + "--disable-builtin-cli-modules", |
| 69 | + "--disable-builtin-loadable-modules", |
| 70 | + "--disable-builtin-scripted-loadable-modules", |
| 71 | + "--additional-module-path", temporaryModuleDirPath, |
| 72 | + "--python-script", temporaryModuleDirPath + "/SlicerStartupModuleTestHelperScript/SlicerStartupModuleTestHelperScript.py", |
| 73 | + ] |
| 74 | + |
| 75 | + test_output_file = temporaryModuleDirPath + "/StartupModuleTest.out" |
| 76 | + os.environ["SLICER_STARTUP_MODULE_TEST_OUTPUT"] = test_output_file |
| 77 | + if debug: |
| 78 | + print("SLICER_STARTUP_MODULE_TEST_OUTPUT=%s" % test_output_file) |
| 79 | + |
| 80 | + # Test startupCompleted with main window |
| 81 | + args = list(common_args) |
| 82 | + (returnCode, stdout, stderr) = runSlicer(slicer_executable, args) |
| 83 | + assert returnCode == EXIT_SUCCESS |
| 84 | + assert os.path.isfile(test_output_file) |
| 85 | + os.remove(test_output_file) |
| 86 | + print("Test startupCompleted with main window - passed\n") |
| 87 | + |
| 88 | + # Test startupCompleted without main window |
| 89 | + args = list(common_args) |
| 90 | + args.extend(["--no-main-window"]) |
| 91 | + (returnCode, stdout, stderr) = runSlicer(slicer_executable, args) |
| 92 | + assert os.path.isfile(test_output_file) |
| 93 | + assert returnCode == EXIT_SUCCESS |
| 94 | + print("Test startupCompleted without main window - passed\n") |
| 95 | + |
| 96 | + finally: |
| 97 | + if not debug: |
| 98 | + import shutil |
| 99 | + shutil.rmtree(temporaryModuleDirPath) |
0 commit comments