def test_run_script(self): """Unittest to test the _run_script function.""" # Test case where the script should fail and exit with 2 with self.subTest(): with self.assertRaises(SystemExit) as cm_: shared.run_script("this will exit with 2") self.assertEqual(cm_.exception.code, 2) # Test case where the script should print "this works" to the console with self.subTest(): expected = 0 result = shared.run_script("echo this works")[0] self.assertEqual(result, expected)
def test_install(self): """Unittest to test the install function.""" # Test with undefined requirements directory with self.subTest(): with self.assertRaises(SystemExit) as cm_: requirements_dir = data.hashstring(str(random())) install(requirements_dir) self.assertEqual(cm_.exception.code, 3) # Test with default expected behaviour with self.subTest(): # At least one expected package expected_package = 'PattooShared' expected = True # Create temporary directory with tempfile.TemporaryDirectory() as temp_dir: result = install(ROOT_DIR, temp_dir) # Get raw packages in requirements format packages = shared.run_script('python3 -m pip freeze')[1] # Get packages with versions removed installed_packages = [package.decode().split('==')[ 0] for package in packages.split()] result = expected_package in installed_packages self.assertEqual(result, expected)