diff --git a/tests.py b/tests.py index 319e13cde23d794f5e422a60d1c1edbaca362960..e33e1a2daa9a82e737c2c85dfd92995dd3440800 100644 --- a/tests.py +++ b/tests.py @@ -63,6 +63,41 @@ def test_os_chdir(tmp_path): assert os.getcwd() == initial_cwd +def test_run_method(tmp_path): + """Run tests for run method.""" + + run = mod.run + + # test a command with options: + assert run(["date", "+%Y-%m-%d"]).returncode == 0 + assert run("date +%Y-%m-%d", shell=True).returncode == 0 + + # tests interacting with the file system + with mod.os_chdir(str(tmp_path)): + # ensure single string command works + assert run(("ls -l"), shell=True).returncode == 0 + # test spaces are handled correctly: + assert run(["touch", "file1", "file2"]).returncode == 0 + assert os.path.exists("file1") + assert os.path.exists("file2") + # test output is captured: + assert ( + b"Hello, world!\n" + in run(["echo", "Hello, world!"], capture_output=True).stdout + ) + + # check exceptions + with pytest.raises(FileNotFoundError): + run(["doesnotexistcommand"]) + + # check error code is checked + # 1. expect this to parse: return code is non-zero, but we don't check + run(["ls", "/doesnotexist"]), + # 2. expect this to fail: + with pytest.raises(subprocess.CalledProcessError): + run(["ls", "/doesnotexist"], check=True) + + def test_prepare_environment(tmp_path): """Simulate running preparation of environment.