Skip to content

Commit

Permalink
Add tests for some commands that can run in CI
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrice Normandin <normandf@mila.quebec>
  • Loading branch information
lebrice committed Aug 28, 2023
1 parent d9ef604 commit 078d944
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 25 deletions.
57 changes: 51 additions & 6 deletions tests/cli/test_commands.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 (
Expand All @@ -34,11 +32,58 @@ 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):
main()

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)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Opening the docs: https://docs.mila.quebec/search.html?q=conda
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Opening the intranet: https://intranet.mila.quebec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Opening the intranet: https://sites.google.com/search/mila.quebec/mila-intranet?query=idt&scope=site&showTabs=false
19 changes: 0 additions & 19 deletions tests/cli/test_commands/test_help_mila__.txt

This file was deleted.

Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 078d944

Please sign in to comment.