From 078d944da60354c4d28f2ff4f68aa083167053f0 Mon Sep 17 00:00:00 2001 From: Fabrice Normandin Date: Mon, 28 Aug 2023 14:23:20 -0400 Subject: [PATCH] Add tests for some commands that can run in CI Signed-off-by: Fabrice Normandin --- tests/cli/test_commands.py | 57 +++++++++++++++++-- ..._check_command_output_mila_docs_conda_.txt | 1 + ...st_check_command_output_mila_intranet_.txt | 1 + ...heck_command_output_mila_intranet_idt_.txt | 1 + tests/cli/test_commands/test_help_mila__.txt | 19 ------- ...test_invalid_command_output_mila_code_.txt | 0 ...t_invalid_command_output_mila_forward_.txt | 0 ...alid_command_output_mila_search_conda_.txt | 0 ...est_invalid_command_output_mila_serve_.txt | 0 9 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 tests/cli/test_commands/test_check_command_output_mila_docs_conda_.txt create mode 100644 tests/cli/test_commands/test_check_command_output_mila_intranet_.txt create mode 100644 tests/cli/test_commands/test_check_command_output_mila_intranet_idt_.txt delete mode 100644 tests/cli/test_commands/test_help_mila__.txt create mode 100644 tests/cli/test_commands/test_invalid_command_output_mila_code_.txt create mode 100644 tests/cli/test_commands/test_invalid_command_output_mila_forward_.txt create mode 100644 tests/cli/test_commands/test_invalid_command_output_mila_search_conda_.txt create mode 100644 tests/cli/test_commands/test_invalid_command_output_mila_serve_.txt diff --git a/tests/cli/test_commands.py b/tests/cli/test_commands.py index e372ca33..b69a21ea 100644 --- a/tests/cli/test_commands.py +++ b/tests/cli/test_commands.py @@ -1,7 +1,8 @@ +import argparse import contextlib import io +import os import shlex -import sys import pytest from pytest_regressions.file_regression import FileRegressionFixture @@ -12,10 +13,7 @@ @pytest.mark.parametrize( "command", ["mila"] - + [ - f"mila {command}" - for command in ["", "docs", "intranet", "init", "forward", "code", "serve"] - ] + + [f"mila {command}" for command in ["docs", "intranet", "init", "forward", "code", "serve"]] + [ f"mila serve {serve_subcommand}" for serve_subcommand in ( @@ -34,7 +32,7 @@ def test_help( command: str, file_regression: FileRegressionFixture, monkeypatch: pytest.MonkeyPatch ): """Test that the --help text matches what's expected (and is stable over time).""" - monkeypatch.setattr(sys, "argv", shlex.split(command + " --help")) + monkeypatch.setattr("sys.argv", shlex.split(command + " --help")) buf = io.StringIO() with contextlib.suppress(SystemExit), contextlib.redirect_stdout(buf): with pytest.raises(SystemExit): @@ -42,3 +40,50 @@ def test_help( output: str = buf.getvalue() file_regression.check(output) + + +@pytest.mark.parametrize( + "command", + [ + "mila search conda", + "mila code", # Error: Missing the required PATH argument. + "mila serve", # Error: Missing the subcommand. + "mila forward", # Error: Missing the REMOTE argument. + ], +) +def test_invalid_command_output( + command: str, file_regression: FileRegressionFixture, monkeypatch: pytest.MonkeyPatch +): + """Test that we get a proper output when we use an invalid command (that exits immediately).""" + monkeypatch.setattr("sys.argv", shlex.split(command)) + buf = io.StringIO() + with contextlib.suppress(SystemExit), pytest.raises(SystemExit), contextlib.redirect_stdout( + buf + ): + main() + file_regression.check(buf.getvalue()) + + +def dont_run_on_github(*args): + return pytest.param( + *args, + marks=pytest.mark.skipif( + "GITHUB_ACTIONS" in os.environ, + reason="We don't run this test on GitHub Actions for security reasons.", + ), + ) + + +@pytest.mark.parametrize("command", ["mila docs conda", "mila intranet", "mila intranet idt"]) +def test_check_command_output( + command: str, file_regression: FileRegressionFixture, monkeypatch: pytest.MonkeyPatch +): + """Test that the --help text matches what's expected (and is stable over time).""" + + monkeypatch.setattr("webbrowser.open", lambda url: None) + monkeypatch.setattr("sys.argv", shlex.split(command)) + buf = io.StringIO() + with contextlib.redirect_stdout(buf): + main() + output: str = buf.getvalue() + file_regression.check(output) diff --git a/tests/cli/test_commands/test_check_command_output_mila_docs_conda_.txt b/tests/cli/test_commands/test_check_command_output_mila_docs_conda_.txt new file mode 100644 index 00000000..a64578b5 --- /dev/null +++ b/tests/cli/test_commands/test_check_command_output_mila_docs_conda_.txt @@ -0,0 +1 @@ +Opening the docs: https://docs.mila.quebec/search.html?q=conda diff --git a/tests/cli/test_commands/test_check_command_output_mila_intranet_.txt b/tests/cli/test_commands/test_check_command_output_mila_intranet_.txt new file mode 100644 index 00000000..970bde9a --- /dev/null +++ b/tests/cli/test_commands/test_check_command_output_mila_intranet_.txt @@ -0,0 +1 @@ +Opening the intranet: https://intranet.mila.quebec diff --git a/tests/cli/test_commands/test_check_command_output_mila_intranet_idt_.txt b/tests/cli/test_commands/test_check_command_output_mila_intranet_idt_.txt new file mode 100644 index 00000000..ef796201 --- /dev/null +++ b/tests/cli/test_commands/test_check_command_output_mila_intranet_idt_.txt @@ -0,0 +1 @@ +Opening the intranet: https://sites.google.com/search/mila.quebec/mila-intranet?query=idt&scope=site&showTabs=false diff --git a/tests/cli/test_commands/test_help_mila__.txt b/tests/cli/test_commands/test_help_mila__.txt deleted file mode 100644 index c7135f70..00000000 --- a/tests/cli/test_commands/test_help_mila__.txt +++ /dev/null @@ -1,19 +0,0 @@ -usage: mila [-h] [--version] {docs,intranet,init,forward,code,serve} ... - -Tools to connect to and interact with the Mila cluster. Cluster documentation: -https://docs.mila.quebec/ - -positional arguments: - {docs,intranet,init,forward,code,serve} - docs Open the Mila cluster documentation. - intranet Open the Mila intranet in a browser. - init Set up your configuration and credentials. - forward Forward a port on a compute node to your local - machine. - code Open a remote VSCode session on a compute node. - serve Start services on compute nodes and forward them to - your local machine. - -optional arguments: - -h, --help show this help message and exit - --version, -v Milatools version diff --git a/tests/cli/test_commands/test_invalid_command_output_mila_code_.txt b/tests/cli/test_commands/test_invalid_command_output_mila_code_.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/cli/test_commands/test_invalid_command_output_mila_forward_.txt b/tests/cli/test_commands/test_invalid_command_output_mila_forward_.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/cli/test_commands/test_invalid_command_output_mila_search_conda_.txt b/tests/cli/test_commands/test_invalid_command_output_mila_search_conda_.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/cli/test_commands/test_invalid_command_output_mila_serve_.txt b/tests/cli/test_commands/test_invalid_command_output_mila_serve_.txt new file mode 100644 index 00000000..e69de29b