summaryrefslogtreecommitdiff
path: root/silx/utils/test/test_launcher.py
blob: c64ac9a57a26bca52f0c7b388fd95ce77004a008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""Tests for html module"""

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "17/01/2018"


import sys
import unittest
from silx.utils.testutils import ParametricTestCase
from .. import launcher


class CallbackMock():

    def __init__(self, result=None):
        self._execute_count = 0
        self._execute_argv = None
        self._result = result

    def execute(self, argv):
        self._execute_count = self._execute_count + 1
        self._execute_argv = argv
        return self._result

    def __call__(self, argv):
        return self.execute(argv)


class TestLauncherCommand(unittest.TestCase):
    """Tests for launcher class."""

    def testEnv(self):
        command = launcher.LauncherCommand("foo")
        old = sys.argv
        params = ["foo", "bar"]
        with command.get_env(params):
            self.assertEqual(params, sys.argv)
        self.assertEqual(sys.argv, old)

    def testEnvWhileException(self):
        command = launcher.LauncherCommand("foo")
        old = sys.argv
        params = ["foo", "bar"]
        try:
            with command.get_env(params):
                raise RuntimeError()
        except RuntimeError:
            pass
        self.assertEqual(sys.argv, old)

    def testExecute(self):
        params = ["foo", "bar"]
        callback = CallbackMock(result=42)
        command = launcher.LauncherCommand("foo", function=callback)
        status = command.execute(params)
        self.assertEqual(callback._execute_count, 1)
        self.assertEqual(callback._execute_argv, params)
        self.assertEqual(status, 42)


class TestModuleCommand(ParametricTestCase):

    def setUp(self):
        module_name = "silx.utils.test.test_launcher_command"
        command = launcher.LauncherCommand("foo", module_name=module_name)
        self.command = command

    def testHelp(self):
        status = self.command.execute(["--help"])
        self.assertEqual(status, 0)

    def testException(self):
        try:
            self.command.execute(["exception"])
            self.fail()
        except RuntimeError:
            pass

    def testCall(self):
        status = self.command.execute([])
        self.assertEqual(status, 0)

    def testError(self):
        status = self.command.execute(["error"])
        self.assertEqual(status, -1)


class TestLauncher(ParametricTestCase):
    """Tests for launcher class."""

    def testCallCommand(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "foo", "param1", "param2"])
        self.assertEqual(status, 42)
        self.assertEqual(callback._execute_argv, ["prog foo", "param1", "param2"])
        self.assertEqual(callback._execute_count, 1)

    def testAddCommand(self):
        runner = launcher.Launcher(prog="prog")
        module_name = "silx.utils.test.test_launcher_command"
        runner.add_command("foo", module_name=module_name)
        status = runner.execute(["prog", "foo"])
        self.assertEqual(status, 0)

    def testCallHelpOnCommand(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "--help", "foo"])
        self.assertEqual(status, 42)
        self.assertEqual(callback._execute_argv, ["prog foo", "--help"])
        self.assertEqual(callback._execute_count, 1)

    def testCallHelpOnCommand2(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "help", "foo"])
        self.assertEqual(status, 42)
        self.assertEqual(callback._execute_argv, ["prog foo", "--help"])
        self.assertEqual(callback._execute_count, 1)

    def testCallHelpOnUnknownCommand(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "help", "foo2"])
        self.assertEqual(status, -1)

    def testNotAvailableCommand(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "foo2", "param1", "param2"])
        self.assertEqual(status, -1)
        self.assertEqual(callback._execute_count, 0)

    def testCallHelp(self):
        callback = CallbackMock(result=42)
        runner = launcher.Launcher(prog="prog")
        command = launcher.LauncherCommand("foo", function=callback)
        runner.add_command(command=command)
        status = runner.execute(["prog", "help"])
        self.assertEqual(status, 0)
        self.assertEqual(callback._execute_count, 0)

    def testCommonCommands(self):
        runner = launcher.Launcher()
        tests = [
            ["prog"],
            ["prog", "--help"],
            ["prog", "--version"],
            ["prog", "help", "--help"],
            ["prog", "help", "help"],
        ]
        for arguments in tests:
            with self.subTest(args=tests):
                status = runner.execute(arguments)
                self.assertEqual(status, 0)


def suite():
    loader = unittest.defaultTestLoader.loadTestsFromTestCase
    test_suite = unittest.TestSuite()
    test_suite.addTest(loader(TestLauncherCommand))
    test_suite.addTest(loader(TestLauncher))
    test_suite.addTest(loader(TestModuleCommand))
    return test_suite


if __name__ == '__main__':
    unittest.main(defaultTest='suite')