Skip to content

Commit

Permalink
Fix the issue appeared to incorrect return exit codes for this python…
Browse files Browse the repository at this point in the history
… package
  • Loading branch information
vladpunko committed Nov 21, 2021
1 parent bc15004 commit 1f67d38
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ workdir="$(mktemp -d)"
git clone --depth=1 --branch=master -- https://github.com/vladpunko/notebook-environments.git "${workdir}" > /dev/null 2>&1 || {
echo 'An error occurred while downloading the source code to the current machine.' >&2
# Stop this program runtime and return the exit status code.
exit 70
exit 1
}

# Step -- 2.
Expand Down
42 changes: 21 additions & 21 deletions notebook_environments.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
"show_kernels",
)

__version__ = "0.8.6"
__version__ = "0.8.7"


def _in_virtual_environment():
Expand Down Expand Up @@ -143,7 +143,7 @@ def _get_data_path(*subdirs):
except KeyError:
_logger.error("This user's operating system isn't supported now.")
# Stop this program runtime and return the exit status code.
sys.exit(1)
sys.exit(errno.EPERM)


def _get_kernel_name():
Expand All @@ -152,7 +152,7 @@ def _get_kernel_name():
if not KERNEL_NAME_PATTERN.match(name):
_logger.error("It's impossible to create a new kernel name with invalid characters.")
# Stop this program runtime and return the exit status code.
sys.exit(78)
sys.exit(errno.EPERM)

return name

Expand Down Expand Up @@ -194,10 +194,10 @@ def _write_kernel_specification(path):
with io.open(os.path.join(path, "kernel.json"), mode="wt", encoding="utf-8") as stream_out:
# Create a new kernel specification on the current machine.
stream_out.write(_to_unicode(json.dumps(kernel_spec, ensure_ascii=False, indent=2)))
except (IOError, OSError):
except (IOError, OSError) as err:
_logger.error("It's impossible to create a new specification on the current machine.")
# Stop this program runtime and return the exit status code.
sys.exit(74)
sys.exit(getattr(err, "errno", errno.EIO))


def _provide_required_packages():
Expand All @@ -208,14 +208,14 @@ def _provide_required_packages():
stderr=devnull,
stdout=devnull,
)
except subprocess.CalledProcessError:
except subprocess.CalledProcessError as err:
_logger.error((
"It's impossible to install packages on the current machine.\n"
"You are to update setup tools and run the installation process another time.\n"
"python -m pip install --upgrade pip setuptools wheel"
))
# Stop this program runtime and return the exit status code.
sys.exit(70)
sys.exit(getattr(err, "errno", errno.EPERM))


def _write_python_logos(path):
Expand All @@ -229,10 +229,10 @@ def _write_python_logos(path):
with io.open(os.path.join(path, logo_name), mode="wb") as stream_out:
# Create a new python logo on the current machine.
stream_out.write(base64.b64decode(logo_image))
except (IOError, OSError):
except (IOError, OSError) as err:
_logger.error("It's impossible to create python logos on the current machine.")
# Stop this program runtime and return the exit status code.
sys.exit(74)
sys.exit(getattr(err, "errno", errno.EIO))


def _check_and_remove_broken_kernel(path):
Expand Down Expand Up @@ -260,7 +260,7 @@ def _create_dir(path):
else:
_logger.error("It's impossible to create a new directory on the current machine.")
# Stop this program runtime and return the exit status code.
sys.exit(71)
sys.exit(getattr(err, "errno", errno.EPERM))


def _remove_dir(path):
Expand All @@ -270,10 +270,10 @@ def _remove_dir(path):
else:
# Execute a function to remove the specified directory tree from the current machine.
shutil.rmtree(path)
except OSError:
except OSError as err:
_logger.error("It's impossible to remove a directory from the current machine.")
# Stop this program runtime and return the exit status code.
sys.exit(71)
sys.exit(getattr(err, "errno", errno.EPERM))


def _create_new_kernel(name):
Expand All @@ -289,7 +289,7 @@ def add_active_environment():
if not _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment isn't active.")
# Stop this program runtime and return the exit status code.
sys.exit(1)
sys.exit(errno.EPERM)

# Add an active python environment to the working notebook server on the current machine.
_create_new_kernel(_get_kernel_name())
Expand All @@ -299,7 +299,7 @@ def remove_active_environment():
if not _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment isn't active.")
# Stop this program runtime and return the exit status code.
sys.exit(1)
sys.exit(errno.EPERM)

# Find and remove an active python environment from the working notebook server.
for path in glob.glob(_get_data_path("kernels", _get_kernel_name())):
Expand All @@ -311,17 +311,17 @@ def purge_broken_kernels():
# Find and remove broken python kernels from the working notebook server.
for kernel_info in _list_kernels_in(_get_data_path("kernels")):
_check_and_remove_broken_kernel(kernel_info.path)
except OSError:
except OSError as err:
_logger.error("It's impossible to find and remove broken python kernels.")
# Stop this program runtime and return the exit status code.
sys.exit(71)
sys.exit(getattr(err, "errno", errno.EPERM))


def initialize_new_notebook_environment():
if _in_virtual_environment():
_logger.error("This action is blocked because a specific python environment is active.")
# Stop this program runtime and return the exit status code.
sys.exit(1)
sys.exit(errno.EPERM)

try:
from jupyter_core.paths import jupyter_path
Expand All @@ -343,10 +343,10 @@ def show_kernels():
for kernel_info in _list_kernels_in(_get_data_path("kernels")):
# Show information about the location of a kernel on the current machine.
print("kernel: {0} --> {1}".format(kernel_info.name, kernel_info.path))
except OSError:
except OSError as err:
_logger.error("It's impossible to show python kernels on the working notebook server.")
# Stop this program runtime and return the exit status code.
sys.exit(71)
sys.exit(getattr(err, "errno", errno.EPERM))


def main(): # pragma: no cover
Expand Down Expand Up @@ -403,10 +403,10 @@ def main(): # pragma: no cover
try:
# Execute a command on the current machine.
parser.parse_args().command()
except KeyboardInterrupt:
except KeyboardInterrupt as err:
_logger.error("Stop this program runtime on the current machine.")
# Stop this program runtime and return the exit status code.
sys.exit(130)
sys.exit(getattr(err, "errno", errno.EINTR))


if __name__ == "__main__":
Expand Down
42 changes: 21 additions & 21 deletions notebook_environments_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_get_data_path(self, logger_mock, expanduser_mock, platform_mock, sys_mo
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 1)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch.dict("notebook_environments.os.environ", {"VIRTUAL_ENV": "test"}, clear=True)
def test_get_kernel_name(self, sys_mock):
Expand Down Expand Up @@ -185,7 +185,7 @@ def test_get_kernel_name_error(self, logger_mock, basename_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 78)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch.dict("notebook_environments.os.environ", {}, clear=True)
def test_list_kernels_in(self, sys_mock):
Expand Down Expand Up @@ -222,7 +222,7 @@ def test_list_kernels_in_error(self, logger_mock, listdir_mock, sys_mock):
self.assertEqual(system_exit.exception.code, 0)

# Raise an operating system exception to test a function fault tolerance.
listdir_mock.side_effect = OSError("")
listdir_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(OSError):
# Execute a function from the python package under test to run this case.
Expand All @@ -241,7 +241,7 @@ def test_write_python_logos(self, logger_mock, sys_mock):

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError("")
open_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -253,7 +253,7 @@ def test_write_python_logos(self, logger_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 74)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments.subprocess.check_call")
@mock.patch("notebook_environments._logger")
Expand All @@ -271,7 +271,7 @@ def test_provide_required_packages(self, logger_mock, check_call_mock, sys_mock)
)

# Raise an installation exception of required packages for error testing.
check_call_mock.side_effect = subprocess.CalledProcessError(1, "", "")
check_call_mock.side_effect = subprocess.CalledProcessError(errno.EPERM, "", "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -285,7 +285,7 @@ def test_provide_required_packages(self, logger_mock, check_call_mock, sys_mock)
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 70)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments._logger")
@mock.patch.dict("notebook_environments.os.environ", {"VIRTUAL_ENV": "test"}, clear=True)
Expand Down Expand Up @@ -317,7 +317,7 @@ def test_write_kernel_specification(self, logger_mock, sys_mock):

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError("")
open_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -329,7 +329,7 @@ def test_write_kernel_specification(self, logger_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 74)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments.os.makedirs")
@mock.patch("notebook_environments._logger")
Expand All @@ -351,7 +351,7 @@ def test_create_dir(self, logger_mock, makedirs_mock, sys_mock):
notebook_environments._create_dir(self.data_path)

# Raise an operating system exception to test a function fault tolerance.
makedirs_mock.side_effect = OSError("")
makedirs_mock.side_effect = OSError(errno.EPERM, "")

# Try to call the function under test again for error testing.
notebook_environments._create_dir(self.data_path)
Expand All @@ -362,7 +362,7 @@ def test_create_dir(self, logger_mock, makedirs_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 71)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments.shutil.rmtree")
@mock.patch("notebook_environments._logger")
Expand All @@ -377,7 +377,7 @@ def test_remove_dir(self, logger_mock, rmtree_mock, sys_mock):
rmtree_mock.assert_called_with(self.data_path)

# Raise an operating system exception to test a function fault tolerance.
rmtree_mock.side_effect = OSError("")
rmtree_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -389,7 +389,7 @@ def test_remove_dir(self, logger_mock, rmtree_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 71)
self.assertEqual(system_exit.exception.code, errno.EPERM)

self.fs.create_symlink(self.link_path, self.data_path)

Expand Down Expand Up @@ -451,7 +451,7 @@ def test_add_not_active_environment(self, logger_mock, create_new_kernel_mock, s
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 1)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments._get_data_path")
@mock.patch.dict("notebook_environments.os.environ", {"VIRTUAL_ENV": "test"}, clear=True)
Expand Down Expand Up @@ -484,7 +484,7 @@ def test_remove_not_active_environment(self, logger_mock, remove_dir_mock, sys_m
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 1)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments._get_data_path")
@mock.patch.dict("notebook_environments.os.environ", {}, clear=True)
Expand All @@ -509,7 +509,7 @@ def test_remove_dead_kernels_error(self, logger_mock, list_kernels_in_mock, sys_
sys_mock.deactivate()

# Raise an operating system exception to test a function fault tolerance.
list_kernels_in_mock.side_effect = OSError("")
list_kernels_in_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -521,7 +521,7 @@ def test_remove_dead_kernels_error(self, logger_mock, list_kernels_in_mock, sys_
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 71)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments._remove_dir")
@mock.patch.dict("notebook_environments.os.environ", {}, clear=True)
Expand All @@ -530,7 +530,7 @@ def test_check_and_remove_broken_kernel(self, remove_dir_mock, sys_mock):

with mock.patch("notebook_environments.io.open", new=mock.mock_open()) as open_mock:
# Raise an operating system exception to test a function fault tolerance.
open_mock.side_effect = OSError("")
open_mock.side_effect = OSError(errno.EPERM, "")

# Execute a function from the python package under test to run this case.
notebook_environments._check_and_remove_broken_kernel(self.kernels_paths[0])
Expand Down Expand Up @@ -562,7 +562,7 @@ def test_show_kernels_error(self, logger_mock, list_kernels_in_mock, sys_mock):
sys_mock.deactivate()

# Raise an operating system exception to test a function fault tolerance.
list_kernels_in_mock.side_effect = OSError("")
list_kernels_in_mock.side_effect = OSError(errno.EPERM, "")

with self.assertRaises(SystemExit) as system_exit:
# Execute a function from the python package under test to run this case.
Expand All @@ -574,7 +574,7 @@ def test_show_kernels_error(self, logger_mock, list_kernels_in_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 71)
self.assertEqual(system_exit.exception.code, errno.EPERM)

@mock.patch("notebook_environments._create_new_kernel")
@mock.patch("notebook_environments._get_data_path")
Expand Down Expand Up @@ -610,7 +610,7 @@ def test_initialize_new_notebook_environment_error(self, logger_mock, sys_mock):
)

# Check the received exit status code from the function under test.
self.assertEqual(system_exit.exception.code, 1)
self.assertEqual(system_exit.exception.code, errno.EPERM)


if __name__ == "__main__":
Expand Down

0 comments on commit 1f67d38

Please sign in to comment.