summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_script_helper.py
blob: 372d6a79ae9fea5436ed50026177f148e3d692d8 (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
"""Unittests for test.script_helper.  Who tests the test helper?"""

import subprocess
import sys
from test import script_helper
import unittest
from unittest import mock


class TestScriptHelper(unittest.TestCase):

    def test_assert_python_ok(self):
        t = script_helper.assert_python_ok('-c', 'import sys; sys.exit(0)')
        self.assertEqual(0, t[0], 'return code was not 0')

    def test_assert_python_failure(self):
        # I didn't import the sys module so this child will fail.
        rc, out, err = script_helper.assert_python_failure('-c', 'sys.exit(0)')
        self.assertNotEqual(0, rc, 'return code should not be 0')

    def test_assert_python_ok_raises(self):
        # I didn't import the sys module so this child will fail.
        with self.assertRaises(AssertionError) as error_context:
            script_helper.assert_python_ok('-c', 'sys.exit(0)')
        error_msg = str(error_context.exception)
        self.assertIn('command line was:', error_msg)
        self.assertIn('sys.exit(0)', error_msg, msg='unexpected command line')

    def test_assert_python_failure_raises(self):
        with self.assertRaises(AssertionError) as error_context:
            script_helper.assert_python_failure('-c', 'import sys; sys.exit(0)')
        error_msg = str(error_context.exception)
        self.assertIn('Process return code is 0,', error_msg)
        self.assertIn('import sys; sys.exit(0)', error_msg,
                      msg='unexpected command line.')

    @mock.patch('subprocess.Popen')
    def test_assert_python_isolated_when_env_not_required(self, mock_popen):
        with mock.patch.object(script_helper,
                               '_interpreter_requires_environment',
                               return_value=False) as mock_ire_func:
            mock_popen.side_effect = RuntimeError('bail out of unittest')
            try:
                script_helper._assert_python(True, '-c', 'None')
            except RuntimeError as err:
                self.assertEqual('bail out of unittest', err.args[0])
            self.assertEqual(1, mock_popen.call_count)
            self.assertEqual(1, mock_ire_func.call_count)
            popen_command = mock_popen.call_args[0][0]
            self.assertEqual(sys.executable, popen_command[0])
            self.assertIn('None', popen_command)
            self.assertIn('-I', popen_command)
            self.assertNotIn('-E', popen_command)  # -I overrides this

    @mock.patch('subprocess.Popen')
    def test_assert_python_not_isolated_when_env_is_required(self, mock_popen):
        """Ensure that -I is not passed when the environment is required."""
        with mock.patch.object(script_helper,
                               '_interpreter_requires_environment',
                               return_value=True) as mock_ire_func:
            mock_popen.side_effect = RuntimeError('bail out of unittest')
            try:
                script_helper._assert_python(True, '-c', 'None')
            except RuntimeError as err:
                self.assertEqual('bail out of unittest', err.args[0])
            popen_command = mock_popen.call_args[0][0]
            self.assertNotIn('-I', popen_command)
            self.assertNotIn('-E', popen_command)


class TestScriptHelperEnvironment(unittest.TestCase):
    """Code coverage for _interpreter_requires_environment()."""

    def setUp(self):
        self.assertTrue(
                hasattr(script_helper, '__cached_interp_requires_environment'))
        # Reset the private cached state.
        script_helper.__dict__['__cached_interp_requires_environment'] = None

    def tearDown(self):
        # Reset the private cached state.
        script_helper.__dict__['__cached_interp_requires_environment'] = None

    @mock.patch('subprocess.check_call')
    def test_interpreter_requires_environment_true(self, mock_check_call):
        mock_check_call.side_effect = subprocess.CalledProcessError('', '')
        self.assertTrue(script_helper._interpreter_requires_environment())
        self.assertTrue(script_helper._interpreter_requires_environment())
        self.assertEqual(1, mock_check_call.call_count)

    @mock.patch('subprocess.check_call')
    def test_interpreter_requires_environment_false(self, mock_check_call):
        # The mocked subprocess.check_call fakes a no-error process.
        script_helper._interpreter_requires_environment()
        self.assertFalse(script_helper._interpreter_requires_environment())
        self.assertEqual(1, mock_check_call.call_count)

    @mock.patch('subprocess.check_call')
    def test_interpreter_requires_environment_details(self, mock_check_call):
        script_helper._interpreter_requires_environment()
        self.assertFalse(script_helper._interpreter_requires_environment())
        self.assertFalse(script_helper._interpreter_requires_environment())
        self.assertEqual(1, mock_check_call.call_count)
        check_call_command = mock_check_call.call_args[0][0]
        self.assertEqual(sys.executable, check_call_command[0])
        self.assertIn('-E', check_call_command)


if __name__ == '__main__':
    unittest.main()
kwa"><div id="projectbrief">API Reference</div> </td> <td> <div id="MSearchBox" class="MSearchBoxInactive"> <span class="left"> <img id="MSearchSelect" src="search/mag_sel.svg" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" alt=""/> <input type="text" id="MSearchField" value="Search" accesskey="S" onfocus="searchBox.OnSearchFieldFocus(true)" onblur="searchBox.OnSearchFieldFocus(false)" onkeyup="searchBox.OnSearchFieldChange(event)"/> </span><span class="right"> <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a> </span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.9.1 --> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */ var searchBox = new SearchBox("searchBox", "search",false,'Search','.html'); /* @license-end */ </script> </div><!-- top --> <div id="side-nav" class="ui-resizable side-nav-resizable"> <div id="nav-tree"> <div id="nav-tree-contents"> <div id="nav-sync" class="sync"></div> </div> </div> <div id="splitbar" style="-moz-user-select:none;" class="ui-resizable-handle"> </div> </div> <script type="text/javascript"> /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */ $(document).ready(function(){initNavTree('classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html',''); initResizable(); }); /* @license-end */ </script> <div id="doc-content"> <!-- window showing the filter options --> <div id="MSearchSelectWindow" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()" onkeydown="return searchBox.OnSearchSelectKey(event)"> </div> <!-- iframe showing the search results (closed by default) --> <div id="MSearchResultsWindow"> <iframe src="javascript:void(0)" frameborder="0" name="MSearchResults" id="MSearchResults"> </iframe> </div> <div class="header"> <div class="summary"> <a href="#pub-attribs">Data Fields</a> </div> <div class="headertitle"> <div class="title">H5L_info_t Class Reference</div> </div> </div><!--header--> <div class="contents"> <a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2> <div class="textblock"><p>Information struct for link (for H5Lget_info/H5Lget_info_by_idx) </p> </div><div id="dynsection-0" onclick="return toggleVisibility(this)" class="dynheader closed" style="cursor:pointer;"> <img id="dynsection-0-trigger" src="closed.png" alt="+"/> Inheritance diagram for H5L_info_t:</div> <div id="dynsection-0-summary" class="dynsummary" style="display:block;"> </div> <div id="dynsection-0-content" class="dyncontent" style="display:none;"> <div class="center"><img src="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t__inherit__graph.png" border="0" usemap="#a_h5_l__info__t_inherit__map" alt="Inheritance graph"/></div> <map name="a_h5_l__info__t_inherit__map" id="a_h5_l__info__t_inherit__map"> <area shape="rect" title=" " alt="" coords="9,80,100,107"/> <area shape="rect" title=" " alt="" coords="5,5,104,32"/> </map> <center><span class="legend">[<a target="top" href="graph_legend.html">legend</a>]</span></center></div> <div id="dynsection-1" onclick="return toggleVisibility(this)" class="dynheader closed" style="cursor:pointer;"> <img id="dynsection-1-trigger" src="closed.png" alt="+"/> Collaboration diagram for H5L_info_t:</div> <div id="dynsection-1-summary" class="dynsummary" style="display:block;"> </div> <div id="dynsection-1-content" class="dyncontent" style="display:none;"> <div class="center"><img src="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t__coll__graph.png" border="0" usemap="#a_h5_l__info__t_coll__map" alt="Collaboration graph"/></div> <map name="a_h5_l__info__t_coll__map" id="a_h5_l__info__t_coll__map"> <area shape="rect" title=" " alt="" coords="15,171,105,197"/> <area shape="rect" title=" " alt="" coords="5,5,104,32"/> <area shape="rect" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_o__token__t.html" title=" " alt="" coords="77,81,182,108"/> </map> <center><span class="legend">[<a target="top" href="graph_legend.html">legend</a>]</span></center></div> <table class="memberdecls"> <tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-attribs"></a> Data Fields</h2></td></tr> <tr class="memitem:ac765329451135abec74c45e1897abf26"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#ac765329451135abec74c45e1897abf26">type</a></td></tr> <tr class="separator:ac765329451135abec74c45e1897abf26"><td class="memSeparator" colspan="2">&#160;</td></tr> <tr class="memitem:a2db01c8e5acea1cbd9b9d2c10e8137f5"><td class="memItemLeft" align="right" valign="top">boolean&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#a2db01c8e5acea1cbd9b9d2c10e8137f5">corder_valid</a></td></tr> <tr class="separator:a2db01c8e5acea1cbd9b9d2c10e8137f5"><td class="memSeparator" colspan="2">&#160;</td></tr> <tr class="memitem:a82bce9c2ae6ce254ef901706c6168f02"><td class="memItemLeft" align="right" valign="top">long&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#a82bce9c2ae6ce254ef901706c6168f02">corder</a></td></tr> <tr class="separator:a82bce9c2ae6ce254ef901706c6168f02"><td class="memSeparator" colspan="2">&#160;</td></tr> <tr class="memitem:a05f527d1e558af7d8c628cb1db6d7b52"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#a05f527d1e558af7d8c628cb1db6d7b52">cset</a></td></tr> <tr class="separator:a05f527d1e558af7d8c628cb1db6d7b52"><td class="memSeparator" colspan="2">&#160;</td></tr> <tr class="memitem:aec1bc6735ca478c420f5ff729bcb3888"><td class="memItemLeft" align="right" valign="top"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_o__token__t.html">H5O_token_t</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#aec1bc6735ca478c420f5ff729bcb3888">token</a></td></tr> <tr class="separator:aec1bc6735ca478c420f5ff729bcb3888"><td class="memSeparator" colspan="2">&#160;</td></tr> <tr class="memitem:a3807749802c1a6ec9438c7b872313fda"><td class="memItemLeft" align="right" valign="top">long&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html#a3807749802c1a6ec9438c7b872313fda">val_size</a></td></tr> <tr class="separator:a3807749802c1a6ec9438c7b872313fda"><td class="memSeparator" colspan="2">&#160;</td></tr> </table> <h2 class="groupheader">Field Documentation</h2> <a id="a82bce9c2ae6ce254ef901706c6168f02"></a> <h2 class="memtitle"><span class="permalink"><a href="#a82bce9c2ae6ce254ef901706c6168f02">&#9670;&nbsp;</a></span>corder</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">long corder</td> </tr> </table> </div><div class="memdoc"> <p>Creation order </p> </div> </div> <a id="a2db01c8e5acea1cbd9b9d2c10e8137f5"></a> <h2 class="memtitle"><span class="permalink"><a href="#a2db01c8e5acea1cbd9b9d2c10e8137f5">&#9670;&nbsp;</a></span>corder_valid</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">boolean corder_valid</td> </tr> </table> </div><div class="memdoc"> <p>Indicate if creation order is valid </p> </div> </div> <a id="a05f527d1e558af7d8c628cb1db6d7b52"></a> <h2 class="memtitle"><span class="permalink"><a href="#a05f527d1e558af7d8c628cb1db6d7b52">&#9670;&nbsp;</a></span>cset</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int cset</td> </tr> </table> </div><div class="memdoc"> <p>Character set of link name </p> </div> </div> <a id="aec1bc6735ca478c420f5ff729bcb3888"></a> <h2 class="memtitle"><span class="permalink"><a href="#aec1bc6735ca478c420f5ff729bcb3888">&#9670;&nbsp;</a></span>token</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_o__token__t.html">H5O_token_t</a> token</td> </tr> </table> </div><div class="memdoc"> <p>Character set of link name </p> </div> </div> <a id="ac765329451135abec74c45e1897abf26"></a> <h2 class="memtitle"><span class="permalink"><a href="#ac765329451135abec74c45e1897abf26">&#9670;&nbsp;</a></span>type</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">int type</td> </tr> </table> </div><div class="memdoc"> <p>Type of link </p> </div> </div> <a id="a3807749802c1a6ec9438c7b872313fda"></a> <h2 class="memtitle"><span class="permalink"><a href="#a3807749802c1a6ec9438c7b872313fda">&#9670;&nbsp;</a></span>val_size</h2> <div class="memitem"> <div class="memproto"> <table class="memname"> <tr> <td class="memname">long val_size</td> </tr> </table> </div><div class="memdoc"> <p>Size of a soft link or user-defined link value </p> </div> </div> <hr/>The documentation for this class was generated from the following file:<ul> <li>java/src/hdf/hdf5lib/structs/<a class="el" href="_h5_l__info__t_8java.html">H5L_info_t.java</a></li> </ul> </div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="navelem"><b>hdf</b></li><li class="navelem"><b>hdf5lib</b></li><li class="navelem"><a class="el" href="namespacehdf_1_1hdf5lib_1_1structs.html">structs</a></li><li class="navelem"><a class="el" href="classhdf_1_1hdf5lib_1_1structs_1_1_h5_l__info__t.html">H5L_info_t</a></li> <li class="footer">Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li> </ul> </div> </body> </html>