Pārlūkot izejas kodu

fix: reset argparse at each entrypoint test

trgiangdo 2 gadi atpakaļ
vecāks
revīzija
de2fbb42a9
2 mainītis faili ar 34 papildinājumiem un 10 dzēšanām
  1. 5 4
      src/taipy/_entrypoint.py
  2. 29 6
      tests/cli/test_cli.py

+ 5 - 4
src/taipy/_entrypoint.py

@@ -25,15 +25,16 @@ def _entrypoint():
     sys.path.append(os.path.normpath(os.getcwd()))
 
     _CLI._parser.add_argument("-v", "--version", action="store_true", help="Print the current Taipy version and exit.")
-    args = _CLI._parse()
-    if args.version:
-        print(f"Taipy {_get_version()}")
-        sys.exit(0)
 
     _VersionCLI.create_parser()
     _ScaffoldCLI.create_parser()
     _HelpCLI.create_parser()
 
+    args = _CLI._parse()
+    if args.version:
+        print(f"Taipy {_get_version()}")
+        sys.exit(0)
+
     _HelpCLI.parse_arguments()
     _VersionCLI.parse_arguments()
     _ScaffoldCLI.parse_arguments()

+ 29 - 6
tests/cli/test_cli.py

@@ -9,13 +9,14 @@
 # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 # specific language governing permissions and limitations under the License.
 
+import argparse
 import re
-from argparse import Namespace
 from unittest.mock import patch
 
 import pytest
 
 from src.taipy._entrypoint import _entrypoint
+from taipy._cli._base_cli import _CLI
 
 
 def preprocess_stdout(stdout):
@@ -23,6 +24,30 @@ def preprocess_stdout(stdout):
     return re.sub(" +", " ", stdout)
 
 
+def remove_subparser(name: str):
+    """Remove a subparser from the _CLI class."""
+    _CLI._sub_taipyparsers.pop(name, None)
+
+    if _CLI._subparser_action:
+        _CLI._subparser_action._name_parser_map.pop(name, None)
+
+        for action in _CLI._subparser_action._choices_actions:
+            if action.dest == name:
+                _CLI._subparser_action._choices_actions.remove(action)
+
+
+@pytest.fixture(autouse=True, scope="function")
+def clean_argparser():
+    _CLI._parser = argparse.ArgumentParser(conflict_handler="resolve")
+    _CLI._subparser_action = None
+    _CLI._arg_groups = {}
+    subcommands = list(_CLI._sub_taipyparsers.keys())
+    for subcommand in subcommands:
+        remove_subparser(subcommand)
+
+    yield
+
+
 def test_taipy_help(capsys):
     expected_help = """{manage-versions,create,help} ...
 
@@ -52,8 +77,7 @@ positional arguments:
 
 
 def test_help_non_existed_command(caplog):
-    # with patch("sys.argv", ["prog", "help", "non_existed_command"]):
-    with patch("taipy._cli._base_cli._CLI._parse", return_value=Namespace(which="help", command="non_existed_command")):
+    with patch("sys.argv", ["prog", "help", "non_existed_command"]):
         with pytest.raises(SystemExit):
             _entrypoint()
         assert "non_existed_command is not a valid command." in caplog.text
@@ -62,14 +86,13 @@ def test_help_non_existed_command(caplog):
 def test_taipy_create_help(capsys):
     expected_help = "create [-h] [--template"
 
-    with patch("sys.argv", ["prog", "create", "--help"]):
+    with patch("sys.argv", ["prog", "help", "create"]):
         with pytest.raises(SystemExit):
             _entrypoint()
         out, _ = capsys.readouterr()
         assert preprocess_stdout(expected_help) in preprocess_stdout(out)
 
-    # with patch("sys.argv", ["prog", "help", "create"]):
-    with patch("taipy._cli._base_cli._CLI._parse", return_value=Namespace(which="help", command="create")):
+    with patch("sys.argv", ["prog", "create", "--help"]):
         with pytest.raises(SystemExit):
             _entrypoint()
         out, _ = capsys.readouterr()