Repository: AgnostiqHQ/covalent (language: python)

Issue:
CLI unit tests
@wjcunningham7 commented on [Wed Jan 05 2022](https://github.com/AgnostiqHQ/covalent-staging/issues/259)

Add automated tests which cover the CLI tool




Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5af926475..e6f5d630c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,11 +5,29 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
 and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
+## [0.22.15] - 2022-02-01
+
+### Fixed
+
+- Covalent CLI command `covalent purge` will now stop the servers before deleting all the pid files.
+
+### Added
+
+- Test for `purge` method in covalent_dispatcher/_cli/service.py.
+
+### Removed
+
+- Unused `covalent_dispatcher` import from covalent_dispatcher/_cli/service.py.
+
+### Changed
+
+- Moved `_config_manager` import from within the `purge` method to the covalent_dispatcher/_cli/service.py for the purpose of mocking in tests.
+
 ## [0.22.14] - 2022-02-01
 
 ### Added
 
-- Type hint to `_server_dispatch` method in `covalent/_workflow/lattice.py`
+- Type hint to `_server_dispatch` method in `covalent/_workflow/lattice.py`.
 
 ## [0.22.13] - 2022-01-26
 
diff --git a/VERSION b/VERSION
index 1602c3624..7203c61b8 100644
--- a/VERSION
+++ b/VERSION
@@ -1,1 +1,1 @@
-0.22.14
+0.22.15
diff --git a/covalent_dispatcher/_cli/service.py b/covalent_dispatcher/_cli/service.py
index f76d71c07..ef3788d5a 100644
--- a/covalent_dispatcher/_cli/service.py
+++ b/covalent_dispatcher/_cli/service.py
@@ -30,7 +30,7 @@
 import click
 import psutil
 
-import covalent_dispatcher
+from covalent._shared_files.config import _config_manager as cm
 from covalent._shared_files.config import get_config, set_config
 
 DISPATCHER_PIDFILE = get_config("dispatcher.cache_dir") + "/dispatcher.pid"
@@ -381,16 +381,18 @@ def status() -> None:
 @click.command()
 def purge() -> None:
     """
-    Delete the cache and config settings.
+    Shutdown servers and delete the cache and config settings.
     """
 
+    # Shutdown UI and dispatcher server.
+    _graceful_shutdown("dispatcher", DISPATCHER_PIDFILE)
+    _graceful_shutdown("UI", UI_PIDFILE)
+
     shutil.rmtree(get_config("sdk.log_dir"), ignore_errors=True)
     shutil.rmtree(get_config("dispatcher.cache_dir"), ignore_errors=True)
     shutil.rmtree(get_config("dispatcher.log_dir"), ignore_errors=True)
     shutil.rmtree(get_config("user_interface.log_dir"), ignore_errors=True)
 
-    from covalent._shared_files.config import _config_manager as cm
-
     cm.purge_config()
 
     click.echo("Covalent server files have been purged.")


Test patch:
diff --git a/tests/covalent_dispatcher_tests/_cli/__init__.py b/tests/covalent_dispatcher_tests/_cli/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/tests/covalent_dispatcher_tests/_cli/service_test.py b/tests/covalent_dispatcher_tests/_cli/service_test.py
new file mode 100644
index 000000000..4a4c520f8
--- /dev/null
+++ b/tests/covalent_dispatcher_tests/_cli/service_test.py
@@ -0,0 +1,51 @@
+# Copyright 2021 Agnostiq Inc.
+#
+# This file is part of Covalent.
+#
+# Licensed under the GNU Affero General Public License 3.0 (the "License").
+# A copy of the License may be obtained with this software package or at
+#
+#      https://www.gnu.org/licenses/agpl-3.0.en.html
+#
+# Use of this file is prohibited except in compliance with the License. Any
+# modifications or derivative works of this file must retain this copyright
+# notice, and modified files must contain a notice indicating that they have
+# been altered from the originals.
+#
+# Covalent is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the License for more details.
+#
+# Relief from the License may be granted by purchasing a commercial license.
+
+"""Tests for Covalent command line interface (CLI) Tool."""
+
+import mock
+from click.testing import CliRunner
+
+from covalent_dispatcher._cli.service import purge
+
+
+def test_purge(mocker):
+    """Test the 'covalent purge' CLI command."""
+
+    from covalent_dispatcher._cli.service import DISPATCHER_PIDFILE, UI_PIDFILE, get_config
+
+    runner = CliRunner()
+    graceful_shutdown_mock = mocker.patch("covalent_dispatcher._cli.service._graceful_shutdown")
+    shutil_rmtree_mock = mocker.patch("covalent_dispatcher._cli.service.shutil.rmtree")
+    purge_config_mock = mocker.patch("covalent_dispatcher._cli.service.cm.purge_config")
+    result = runner.invoke(purge)
+    graceful_shutdown_mock.assert_has_calls(
+        [mock.call("dispatcher", DISPATCHER_PIDFILE), mock.call("UI", UI_PIDFILE)]
+    )
+    shutil_rmtree_mock.assert_has_calls(
+        [
+            mock.call(get_config("sdk.log_dir"), ignore_errors=True),
+            mock.call(get_config("dispatcher.cache_dir"), ignore_errors=True),
+            mock.call(get_config("dispatcher.log_dir"), ignore_errors=True),
+            mock.call(get_config("user_interface.log_dir"), ignore_errors=True),
+        ]
+    )
+    purge_config_mock.assert_called_once()
+    assert result.output == "Covalent server files have been purged.\n"