def test_bad_riotfile_name(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile", "w") as f: f.write( """ from riot import Venv venv = Venv( venvs=[ Venv( pys=[3], name="success", command="echo hello", ), ], ) """ ) result = cli.invoke( riot.cli.main, ["-f", "riotfile", "list"], catch_exceptions=False ) assert result.exit_code == 1 assert ( result.stdout == "Failed to construct config file:\nInvalid file format for riotfile. Expected file with .py extension got 'riotfile'.\n" )
def test_types(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv venv = Venv( venvs=[ Venv( pys=[3], name="success", command="exit 0", ), Venv( pys=[3], name="success2", command="exit 0", ), ], ) """ ) result = cli.invoke( riot.cli.main, ["run", "-s", "success"], catch_exceptions=False ) assert result.exit_code == 0 assert re.search(r"✓ success: \[[0-9a-f]{7}\]", result.stdout) result = cli.invoke( riot.cli.main, ["run", "-s", "success2"], catch_exceptions=False ) assert result.exit_code == 0 assert re.search(r"✓ success2: \[[0-9a-f]{7}\]", result.stdout)
def test_pass_env_always(cli: click.testing.CliRunner, monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write(""" from riot import Venv venv = Venv( pkgs={ "pytest": [""], }, venvs=[ Venv( pys=[3], name="envtest", command="pytest", ), ], ) """) with open("test_success.py", "w") as f: f.write(""" import os def test_success(): assert os.environ["NO_PROXY"] == "baz" """) monkeypatch.setenv("NO_PROXY", "baz") result = cli.invoke(riot.cli.main, ["run", "-s", "envtest"], catch_exceptions=False) assert result.exit_code == 0 assert "✓ envtest" in result.stdout
def test_env(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write(""" from riot import Venv, latest venv = Venv( pkgs={ "pytest": latest, }, venvs=[ Venv( env={"foobar": "baz"}, pys=[3], name="envtest", command="pytest", ), ], ) """) with open("test_success.py", "w") as f: f.write(""" import os def test_success(): assert os.environ["foobar"] == "baz" """) result = cli.invoke(riot.cli.main, ["run", "-s", "envtest"], catch_exceptions=False) assert result.exit_code == 0 assert "✓ envtest" in result.stdout
def with_riotfile( cli: click.testing.CliRunner, riotfile: str, dst_filename: str = "riotfile.py" ) -> typing.Generator[None, None, None]: with cli.isolated_filesystem() as fs_dir: shutil.copy( os.path.join(DATA_DIR, riotfile), os.path.join(fs_dir, dst_filename) ) yield
def test_nested_venv(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv venv = Venv( pys=[3], pkgs={ "pytest": [""], }, venvs=[ Venv( name="success", command="pytest test_success.py", ), Venv( name="failure", command="pytest test_failure.py", ), ], ) """ ) with open("test_success.py", "w") as f: f.write( """ def test_success(): assert 1 == 1 """ ) with open("test_failure.py", "w") as f: f.write( """ def test_failure(): assert 1 == 0 """ ) result = cli.invoke( riot.cli.main, ["run", "-s", "success"], catch_exceptions=False ) assert result.exit_code == 0 assert re.search(r"✓ success: \[[0-9a-f]{7}\]", result.stdout) assert "1 passed with 0 warnings, 0 failed" in result.stdout result = cli.invoke( riot.cli.main, ["run", "-s", "failure"], catch_exceptions=False ) assert result.exit_code == 1 assert re.search(r"x failure: \[[0-9a-f]{7}\]", result.stdout) assert "0 passed with 0 warnings, 1 failed" in result.stdout
def test_riotfile_execute_error(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write(""" this is invalid syntax """) result = cli.invoke(riot.cli.main, ["list"], catch_exceptions=False) assert result.exit_code == 1 assert "Failed to parse" in result.stdout assert "SyntaxError: invalid syntax" in result.stdout
def test_nested_venv(cli: click.testing.CliRunner): with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv venv = Venv( pys=[3], pkgs={ "pytest": [""], }, venvs=[ Venv( name="success", command="pytest test_success.py", ), Venv( name="failure", command="pytest test_failure.py", ), ], ) """ ) with open("test_success.py", "w") as f: f.write( """ def test_success(): assert 1 == 1 """ ) with open("test_failure.py", "w") as f: f.write( """ def test_failure(): assert 1 == 0 """ ) result = cli.invoke(riot.cli.main, ["run", "-s", "success"]) assert result.exit_code == 0 assert result.stdout == "" result = cli.invoke(riot.cli.main, ["run", "-s", "failure"]) assert result.exit_code == 1 assert result.stdout == ""
def test_run_suites_cmdargs_not_set( cli: click.testing.CliRunner, name: str, cmdargs: str, cmdrun: str ): """Running command with optional infix cmdargs""" with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv venv = Venv( venvs=[ Venv( name="test_nocmdargs", command="echo no cmdargs", venvs=[ Venv( pys=[3.8], ), ], ), Venv( name="test_cmdargs", command="echo cmdargs={cmdargs}", venvs=[ Venv( pys=[3.8], ), ], ), ] ) """ ) with mock.patch("subprocess.run") as subprocess_run: subprocess_run.return_value.returncode = 0 args = ["run", name] if cmdargs: args += ["--cmdargs", cmdargs] result = cli.invoke(riot.cli.main, args, catch_exceptions=False) assert result.exit_code == 0 assert result.stdout == "" subprocess_run.assert_called() cmd = subprocess_run.call_args_list[-1].args[0] assert cmd.endswith(cmdrun)
def test_default_env(cli: click.testing.CliRunner) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv, latest venv = Venv( pkgs={ "pytest": latest, }, venvs=[ Venv( pys=[3], pkgs={"packaging": ">=21.3"}, name="envtest", command="pytest", ), ], ) """ ) with open("test_success.py", "w") as f: f.write( """ import os def test_success(): assert os.environ["RIOT"] == "1" assert os.environ["RIOT_PYTHON_HINT"] == "Interpreter(_hint='3')" assert os.environ["RIOT_PYTHON_VERSION"].startswith("3.") assert os.environ["RIOT_VENV_HASH"] == "f8691e0" assert os.environ["RIOT_VENV_IDENT"] == "packaging213" assert os.environ["RIOT_VENV_NAME"] == "envtest" assert os.environ["RIOT_VENV_PKGS"] == "'packaging>=21.3'" assert os.environ["RIOT_VENV_FULL_PKGS"] == "'pytest' 'packaging>=21.3'" """ ) result = cli.invoke( riot.cli.main, ["run", "-s", "envtest"], catch_exceptions=False ) assert result.exit_code == 0 assert "✓ envtest" in result.stdout
def without_riotfile( cli: click.testing.CliRunner, ) -> typing.Generator[None, None, None]: with cli.isolated_filesystem(): yield
def test_run_pass_env( cli: click.testing.CliRunner, monkeypatch: _pytest.monkeypatch.MonkeyPatch ) -> None: with cli.isolated_filesystem(): with open("riotfile.py", "w") as f: f.write( """ from riot import Venv venv = Venv( pys=[3], pkgs={ "pytest": [""], }, venvs=[ Venv( name="no_env", command="pytest test_no_env.py", ), Venv( name="env", command="pytest test_env.py", ), ], ) """ ) with open("test_no_env.py", "w") as f: f.write( """ import os def test_no_env(): assert os.environ.get("TEST_ENV_VAR") is None """ ) with open("test_env.py", "w") as f: f.write( """ import os def test_env(): assert os.environ.get("TEST_ENV_VAR") == "1" """ ) # set environment variables to check in test execution monkeypatch.setenv("TEST_ENV_VAR", "1") result = cli.invoke( riot.cli.main, ["run", "-s", "no_env"], catch_exceptions=False ) assert result.exit_code == 0 assert "1 passed with 0 warnings, 0 failed" in result.stdout result = cli.invoke(riot.cli.main, ["run", "-s", "env"], catch_exceptions=False) assert result.exit_code == 1 assert "0 passed with 0 warnings, 1 failed" in result.stdout result = cli.invoke( riot.cli.main, ["run", "-s", "--pass-env", "env"], catch_exceptions=False ) assert result.exit_code == 0 assert "1 passed with 0 warnings, 0 failed" in result.stdout