Best Python code snippet using tox_python
test_ShellEnvironment.py
Source:test_ShellEnvironment.py
1## @file test_ShellEnvironment.py2# Unit test suite for the ShellEnvironment class.3#4##5# Copyright (c) 2019, Microsoft Corporation6#7# All rights reserved.8# Redistribution and use in source and binary forms, with or without9# modification, are permitted provided that the following conditions are met:10# 1. Redistributions of source code must retain the above copyright notice,11# this list of conditions and the following disclaimer.12# 2. Redistributions in binary form must reproduce the above copyright notice,13# this list of conditions and the following disclaimer in the documentation14# and/or other materials provided with the distribution.15#16# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED18# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.19# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,20# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,21# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF23# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE24# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF25# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26##27import os28import sys29import unittest30from MuEnvironment import ShellEnvironment as SE31class TestShellEnvironmentAssumptions(unittest.TestCase):32 def test_shell_should_be_a_singleton(self):33 shell_a = SE.ShellEnvironment()34 shell_b = SE.ShellEnvironment()35 self.assertIs(shell_a, shell_b, "two instances of ShellEnvironment should be identical")36 def test_shell_tests_need_to_be_able_to_clear_singleton(self):37 # This is not currently achievable, and may never be achievable.38 pass39 def test_shell_should_always_have_an_initial_checkpoint(self):40 shell_env = SE.ShellEnvironment()41 self.assertTrue((len(shell_env.checkpoints) > 0),42 "a new instance of ShellEnvironment should have at least one checkpoint")43class TestBasicEnvironmentManipulation(unittest.TestCase):44 def test_can_set_os_vars(self):45 shell_env = SE.ShellEnvironment()46 # Remove the test var, if it exists.47 os.environ.pop("SE-TEST-VAR-1", None)48 # Set a new value and get it directly from the environment.49 new_value = 'Dummy'50 shell_env.set_shell_var('SE-TEST-VAR-1', new_value)51 self.assertEqual(os.environ['SE-TEST-VAR-1'], new_value)52 def test_can_get_os_vars(self):53 shell_env = SE.ShellEnvironment()54 new_value = 'Dummy2'55 shell_env.set_shell_var('SE-TEST-VAR-2', new_value)56 self.assertEqual(shell_env.get_shell_var('SE-TEST-VAR-2'), new_value)57 def test_set_path_string(self):58 shell_env = SE.ShellEnvironment()59 # Test pass 1.60 testpath_elems = ['MYPATH']61 testpath_string = os.pathsep.join(testpath_elems)62 shell_env.set_path(testpath_string)63 self.assertEqual(os.environ['PATH'], testpath_string, "the final string should be correct")64 for elem in testpath_elems:65 self.assertIn(elem, shell_env.active_path, "the active path should contain all elements")66 # Test pass 2.67 testpath_elems = ['/bin/bash', 'new_path', '/root']68 testpath_string = os.pathsep.join(testpath_elems)69 shell_env.set_path(testpath_string)70 self.assertEqual(os.environ['PATH'], testpath_string, "the final string should be correct")71 for elem in testpath_elems:72 self.assertIn(elem, shell_env.active_path, "the active path should contain all elements")73 def test_set_path_elements(self):74 shell_env = SE.ShellEnvironment()75 # Test pass 1.76 testpath_elems = ['MYPATH']77 testpath_string = os.pathsep.join(testpath_elems)78 shell_env.set_path(testpath_elems)79 self.assertEqual(os.environ['PATH'], testpath_string, "the final string should be correct")80 for elem in testpath_elems:81 self.assertIn(elem, shell_env.active_path, "the active path should contain all elements")82 # Test pass 2.83 testpath_elems = ['/bin/bash', 'new_path', '/root']84 testpath_string = os.pathsep.join(testpath_elems)85 shell_env.set_path(testpath_elems)86 self.assertEqual(os.environ['PATH'], testpath_string, "the final string should be correct")87 for elem in testpath_elems:88 self.assertIn(elem, shell_env.active_path, "the active path should contain all elements")89 def test_set_pypath_string(self):90 shell_env = SE.ShellEnvironment()91 # Test pass 1.92 testpath_elems = ['MYPATH']93 testpath_string = os.pathsep.join(testpath_elems)94 shell_env.set_pypath(testpath_string)95 self.assertEqual(os.environ['PYTHONPATH'], testpath_string, "the final string should be correct")96 for elem in testpath_elems:97 self.assertIn(elem, shell_env.active_pypath, "the active path should contain all elements")98 self.assertIn(elem, sys.path, "the sys path should contain all elements")99 # Test pass 2.100 testpath_elems = ['/bin/bash', 'new_path', '/root']101 testpath_string = os.pathsep.join(testpath_elems)102 shell_env.set_pypath(testpath_string)103 self.assertEqual(os.environ['PYTHONPATH'], testpath_string, "the final string should be correct")104 for elem in testpath_elems:105 self.assertIn(elem, shell_env.active_pypath, "the active path should contain all elements")106 self.assertIn(elem, sys.path, "the sys path should contain all elements")107 def test_set_pypath_elements(self):108 shell_env = SE.ShellEnvironment()109 # Test pass 1.110 testpath_elems = ['MYPATH']111 testpath_string = os.pathsep.join(testpath_elems)112 shell_env.set_pypath(testpath_elems)113 self.assertEqual(os.environ['PYTHONPATH'], testpath_string, "the final string should be correct")114 for elem in testpath_elems:115 self.assertIn(elem, shell_env.active_pypath, "the active path should contain all elements")116 self.assertIn(elem, sys.path, "the sys path should contain all elements")117 # Test pass 2.118 testpath_elems = ['/bin/bash', 'new_path', '/root']119 testpath_string = os.pathsep.join(testpath_elems)120 shell_env.set_pypath(testpath_elems)121 self.assertEqual(os.environ['PYTHONPATH'], testpath_string, "the final string should be correct")122 for elem in testpath_elems:123 self.assertIn(elem, shell_env.active_pypath, "the active path should contain all elements")124 self.assertIn(elem, sys.path, "the sys path should contain all elements")125 def test_insert_append_remove_replace_path(self):126 shell_env = SE.ShellEnvironment()127 # Start with a known PATH128 mid_elem = 'MIDDLEPATH'129 shell_env.set_path(mid_elem)130 self.assertEqual(1, len(shell_env.active_path))131 self.assertIn(mid_elem, shell_env.active_path)132 # Add an element to the end.133 end_elem = 'ENDPATH'134 shell_env.append_path(end_elem)135 # Add an element to the beginning.136 start_elem = 'STARTPATH'137 shell_env.insert_path(start_elem)138 # Test for the realities.139 self.assertEqual(3, len(shell_env.active_path))140 self.assertEqual(shell_env.active_path[0], start_elem)141 self.assertEqual(shell_env.active_path[1], mid_elem)142 self.assertEqual(shell_env.active_path[2], end_elem)143 for elem in (start_elem, mid_elem, end_elem):144 self.assertIn(elem, os.environ["PATH"])145 # Test replacing an element on the path146 new_mid_elem = 'NEWMIDDLEPATH'147 shell_env.replace_path_element(mid_elem, new_mid_elem)148 self.assertEqual(shell_env.active_path[1], new_mid_elem)149 # Test replacing an element that doesn't exist150 old_path = shell_env.active_path151 shell_env.replace_path_element("PATH1", "PATH2")152 self.assertEqual(old_path, shell_env.active_path)153 # Test that removing an element works as expected154 shell_env.remove_path_element(new_mid_elem)155 self.assertNotIn(new_mid_elem, shell_env.active_path)156 def test_insert_append_remove_replace_pypath(self):157 shell_env = SE.ShellEnvironment()158 # Start with a known PATH159 mid_elem = 'MIDDLEPATH'160 shell_env.set_pypath(mid_elem)161 self.assertEqual(1, len(shell_env.active_pypath))162 self.assertIn(mid_elem, shell_env.active_pypath)163 # Add an element to the end.164 end_elem = 'ENDPATH'165 shell_env.append_pypath(end_elem)166 # Add an element to the beginning.167 start_elem = 'STARTPATH'168 shell_env.insert_pypath(start_elem)169 # Test for the realities.170 self.assertEqual(3, len(shell_env.active_pypath))171 self.assertEqual(shell_env.active_pypath[0], start_elem)172 self.assertEqual(shell_env.active_pypath[1], mid_elem)173 self.assertEqual(shell_env.active_pypath[2], end_elem)174 for elem in (start_elem, mid_elem, end_elem):175 self.assertIn(elem, os.environ["PYTHONPATH"])176 self.assertIn(elem, sys.path)177 # Test replacing an element on the pypath178 new_mid_elem = 'NEWMIDDLEPATH'179 shell_env.replace_pypath_element(mid_elem, new_mid_elem)180 self.assertEqual(shell_env.active_pypath[1], new_mid_elem)181 # Test replacing an element that doesn't exist182 old_pypath = shell_env.active_pypath183 shell_env.replace_pypath_element("PATH1", "PATH2")184 self.assertEqual(old_pypath, shell_env.active_pypath)185 # Test that removing an element works as expected186 shell_env.remove_pypath_element(new_mid_elem)187 self.assertNotIn(new_mid_elem, shell_env.active_pypath)188 def test_can_set_and_get_build_vars(self):189 shell_env = SE.ShellEnvironment()190 var_name = 'SE-TEST-VAR-3'191 var_data = 'Dummy3'192 # Make sure it doesn't exist beforehand.193 self.assertIs(shell_env.get_build_var(var_name), None, "test var should not exist before creation")194 shell_env.set_build_var(var_name, var_data)195 self.assertEqual(shell_env.get_build_var(var_name), var_data, "get var data should match set var data")196 def test_set_build_vars_should_default_overrideable(self):197 shell_env = SE.ShellEnvironment()198 var_name = 'SE_TEST_VAR_4'199 var_data = 'NewData1'200 var_data2 = 'NewerData1'201 self.assertIs(shell_env.get_build_var(var_name), None, "test var should not exist before creation")202 shell_env.set_build_var(var_name, var_data)203 shell_env.set_build_var(var_name, var_data2)204 self.assertEqual(shell_env.get_build_var(var_name), var_data2)205class TestShellEnvironmenCheckpoints(unittest.TestCase):206 def setUp(self):207 # Grab the singleton and restore the initial checkpoint.208 shell_env = SE.ShellEnvironment()209 shell_env.restore_initial_checkpoint()210 # For testing, purge all checkpoints each time.211 shell_env.checkpoints = [shell_env.checkpoints[SE.ShellEnvironment.INITIAL_CHECKPOINT]]212 def test_restore_initial_checkpoint_should_erase_changes(self):213 shell_env = SE.ShellEnvironment()214 # Check to make sure the change doesn't exist.215 test_path_change = '/SE/TEST/PATH/1'216 self.assertNotIn(test_path_change, shell_env.active_path, "starting condition should not have the test change")217 # Make the change and verify.218 shell_env.append_path(test_path_change)219 self.assertIn(test_path_change, shell_env.active_path)220 # Add a shell_var while we're at it.221 self.assertEqual(shell_env.get_shell_var('i_should_not_exist'), None)222 shell_env.set_shell_var('i_should_not_exist', 'a_value')223 self.assertEqual(shell_env.get_shell_var('i_should_not_exist'), 'a_value')224 # Restore initial checkpoint and verify change is gone.225 shell_env.restore_initial_checkpoint()226 self.assertNotIn(test_path_change, shell_env.active_path, "restoring checkpoint should remove test change")227 self.assertEqual(shell_env.get_shell_var('i_should_not_exist'), None)228 def test_checkpoint_indices_should_be_unique(self):229 shell_env = SE.ShellEnvironment()230 shell_env.append_path('/SE/TEST/PATH/1')231 chkpt1 = shell_env.checkpoint()232 shell_env.append_path('/SE/TEST/PATH/2')233 chkpt2 = shell_env.checkpoint()234 self.assertNotEqual(chkpt1, SE.ShellEnvironment.INITIAL_CHECKPOINT)235 self.assertNotEqual(chkpt2, SE.ShellEnvironment.INITIAL_CHECKPOINT)236 self.assertNotEqual(chkpt1, chkpt2)237 def test_restore_new_checkpoint_should_contain_new_changes(self):238 shell_env = SE.ShellEnvironment()239 # Check to make sure the change doesn't exist.240 test_path_change = '/SE/TEST/PATH/3'241 self.assertNotIn(test_path_change, shell_env.active_path, "starting condition should not have the test change")242 # Make the change and checkpoint.243 shell_env.append_path(test_path_change)244 self.assertIn(test_path_change, shell_env.active_path)245 chkpt1 = shell_env.checkpoint()246 # Restore initial checkpoint and verify change is gone.247 shell_env.restore_initial_checkpoint()248 self.assertNotIn(test_path_change, shell_env.active_path,249 "restoring initial checkpoint should remove test change")250 # Restore new checkpoint and verify change is back.251 shell_env.restore_checkpoint(chkpt1)252 self.assertIn(test_path_change, shell_env.active_path, "restoring new checkpoint should restore test change")253 def test_checkpointed_objects_should_behave_correctly(self):254 shell_env = SE.ShellEnvironment()255 # This test is to make sure that pass-by-reference elements don't persist unexpectedly.256 test_var1_name = 'SE_TEST_VAR_3'257 test_var1_data = 'MyData1'258 test_var1_data2 = 'RevisedData1'259 test_var1_data3 = 'MoreRevisedData1'260 test_var2_name = 'SE_TEST_VAR_4'261 test_var2_data = 'MyData2'262 # Set the first data and make a checkpoint.263 shell_env.set_build_var(test_var1_name, test_var1_data)264 chkpt1 = shell_env.checkpoint()265 # Update previous value and set second data. Then checkpoint.266 shell_env.set_build_var(test_var1_name, test_var1_data2)267 shell_env.set_build_var(test_var2_name, test_var2_data)268 chkpt2 = shell_env.checkpoint()269 # Restore the first checkpoint and verify values.270 shell_env.restore_checkpoint(chkpt1)271 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data)272 self.assertIs(shell_env.get_build_var(test_var2_name), None)273 # Make a change to be tested later.274 shell_env.set_build_var(test_var1_name, test_var1_data3)275 # Restore the second checkpoint and verify values.276 shell_env.restore_checkpoint(chkpt2)277 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data2)278 self.assertEqual(shell_env.get_build_var(test_var2_name), test_var2_data)279 # Restore the first checkpoint again and make sure orignal value still stands.280 shell_env.restore_checkpoint(chkpt1)281 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data)282class TestShellEnvironmenSpecialBuildVars(unittest.TestCase):283 def setUp(self):284 # Grab the singleton and restore the initial checkpoint.285 shell_env = SE.ShellEnvironment()286 shell_env.restore_initial_checkpoint()287 # For testing, purge all checkpoints each time.288 shell_env.checkpoints = [shell_env.checkpoints[SE.ShellEnvironment.INITIAL_CHECKPOINT]]289 def test_get_build_vars_should_update_vars(self):290 shell_env = SE.ShellEnvironment()291 build_vars = SE.GetBuildVars()292 test_var_name = 'SE_TEST_VAR_4'293 test_var_data = 'NewData1'294 build_vars.SetValue(test_var_name, test_var_data, 'random set')295 self.assertEqual(shell_env.get_build_var(test_var_name), test_var_data)296 def test_special_build_vars_should_default_non_overrideable(self):297 shell_env = SE.ShellEnvironment()298 build_vars = SE.GetBuildVars()299 test_var_name = 'SE_TEST_VAR_4'300 test_var_data = 'NewData1'301 test_var_data2 = 'NewerData1'302 build_vars.SetValue(test_var_name, test_var_data, 'random set')303 build_vars.SetValue(test_var_name, test_var_data2, 'another random set')304 self.assertEqual(shell_env.get_build_var(test_var_name), test_var_data)305 def test_special_build_vars_should_always_update_current(self):306 shell_env = SE.ShellEnvironment()307 build_vars = SE.GetBuildVars()308 test_var1_name = 'SE_TEST_VAR_update_current1'309 test_var1_data = 'NewData1'310 test_var1_data2 = 'NewerData1'311 test_var2_name = 'SE_TEST_VAR_update_current2'312 test_var2_data = 'NewData2'313 # Make a change and checkpoint.314 build_vars.SetValue(test_var1_name, test_var1_data, 'var1 set', overridable=True)315 shell_env.checkpoint()316 # Make a couple more changes.317 build_vars.SetValue(test_var1_name, test_var1_data2, 'var1 set', overridable=True)318 build_vars.SetValue(test_var2_name, test_var2_data, 'var2 set', overridable=True)319 # Make sure that the newer changes are valid.320 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data2)321 self.assertEqual(shell_env.get_build_var(test_var2_name), test_var2_data)322 def test_special_build_vars_should_be_checkpointable(self):323 shell_env = SE.ShellEnvironment()324 build_vars = SE.GetBuildVars()325 # This test is basically a rehash of the object checkpointing test,326 # but this time with the special vars.327 test_var1_name = 'SE_TEST_VAR_3'328 test_var1_data = 'MyData1'329 test_var1_data2 = 'RevisedData1'330 test_var1_data3 = 'MoreRevisedData1'331 test_var2_name = 'SE_TEST_VAR_4'332 test_var2_data = 'MyData2'333 # Set the first data and make a checkpoint.334 build_vars.SetValue(test_var1_name, test_var1_data, 'var1 set', overridable=True)335 chkpt1 = shell_env.checkpoint()336 # Update previous value and set second data. Then checkpoint.337 build_vars.SetValue(test_var1_name, test_var1_data2, 'var1 set', overridable=True)338 build_vars.SetValue(test_var2_name, test_var2_data, 'var2 set', overridable=True)339 chkpt2 = shell_env.checkpoint()340 # Restore the first checkpoint and verify values.341 shell_env.restore_checkpoint(chkpt1)342 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data)343 self.assertIs(shell_env.get_build_var(test_var2_name), None)344 # Make a change to be tested later.345 build_vars.SetValue(test_var1_name, test_var1_data3, 'var1 set', overridable=True)346 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data3,347 'even after restore, special build vars should always update current')348 # Restore the second checkpoint and verify values.349 shell_env.restore_checkpoint(chkpt2)350 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data2)351 self.assertEqual(shell_env.get_build_var(test_var2_name), test_var2_data)352 # Restore the first checkpoint again and make sure orignal value still stands.353 shell_env.restore_checkpoint(chkpt1)354 self.assertEqual(shell_env.get_build_var(test_var1_name), test_var1_data)355if __name__ == '__main__':...
pre-commit-check.py
Source:pre-commit-check.py
2import sys3import os4import os.path5from svn import repos, fs, delta, core6### DEAR USER: Please populate the test_props() and test_path_change()7### to do your bidding.8def test_props(props):9 """Validate the PROPS (a dictionary mapping property names to10 values) set on the transaction. Return 0 if all is well, non-zero11 otherwise."""12 ### Test the transaction (revision-to-be) properties. If there is13 ### bogosity, write to sys.stderr and return non-zero.14 return 015def test_path_change(path, change):16 """Validate the CHANGE made to PATH in the transaction. Return 017 if all is well, non-zero otherwise."""18 19 # The svn_node_kind of the path.20 item_kind = change.item_kind21 # Non-zero iff properties of this path were changed.22 prop_changes = change.prop_changes23 # Non-zero iff path is a file, and its text was changed.24 text_changed = change.text_changed25 # The location of the previous revision of this resource, if any.26 base_path = change.base_path27 base_rev = change.base_rev28 # Non-zero iff this change represents an addition (see29 # base_path/base_rev for whether it was an addition with history).30 added = change.added31 ### Test the path change as you see fit. If there is bogosity,32 ### write to sys.stderr and return non-zero.33 return 134def main(pool, repos_dir, txn):35 # Construct a ChangeCollector to fetch our changes.36 fs_ptr = repos.svn_repos_fs(repos.svn_repos_open(repos_dir, pool))37 root = fs.txn_root(fs.open_txn(fs_ptr, txn, pool), pool)38 cc = repos.ChangeCollector(fs_ptr, root, pool)39 # Call the transaction property validator. Might as well get the40 # cheap checks outta the way first.41 retval = test_props(cc.get_root_props())42 if retval:43 return retval44 # Generate the path-based changes list.45 e_ptr, e_baton = delta.make_editor(cc, pool)46 repos.svn_repos_replay(root, e_ptr, e_baton, pool)47 # Call the path change validator.48 changes = cc.get_changes()49 paths = changes.keys()50 paths.sort(lambda a, b: core.svn_path_compare_paths(a, b))51 for path in paths:52 change = changes[path]53 retval = test_path_change(path, change)54 if retval:55 return retval56 57 return 058def _usage_and_exit():59 sys.stderr.write("USAGE: %s REPOS-DIR TXN-NAME\n" % (sys.argv[0]))60 sys.exit(1)61if __name__ == '__main__':62 if len(sys.argv) < 3:63 _usage_and_exit()64 sys.exit(core.run_app(main, sys.argv[1], sys.argv[2]))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!