def test_nonexistent(): """Running `which [-as] <program>` where <program> does not exist should exit with a nonzero status.""" assert len(check_fail(["which", "this-program-does-not-exist"]).stdout) == 0 assert len( check_fail(["which", "-a", "this-program-does-not-exist"]).stdout) == 0 assert len( check_fail(["which", "-s", "this-program-does-not-exist"]).stdout) == 0 assert len( check_fail(["which", "-as", "this-program-does-not-exist"]).stdout) == 0 assert len( check_fail(["which", "-sa", "this-program-does-not-exist"]).stdout) == 0
def test_main(): """Test that uname returns the correct values, and errors when appropriate.""" uname = os.uname() uname_smr = f"{uname.sysname} {uname.release} {uname.machine}\n" uname_a = f"{uname.sysname} {uname.nodename} {uname.release} {uname.version} {uname.machine}\n" assert check(["uname"]).stdout == uname.sysname + "\n" assert check(["uname", "-m"]).stdout == uname.machine + "\n" assert check(["uname", "-n"]).stdout == uname.nodename + "\n" assert check(["uname", "-r"]).stdout == uname.release + "\n" assert check(["uname", "-s"]).stdout == uname.sysname + "\n" assert check(["uname", "-v"]).stdout == uname.version + "\n" assert check(["uname", "-smr"]).stdout == uname_smr assert check(["uname", "-mnrsv"]).stdout == uname_a assert check(["uname", "-a"]).stdout == uname_a assert check_fail(["uname", "-x"]).stderr.startswith("uname: Invalid") assert check_fail(["uname", "x"]).stderr.startswith("uname: Invalid")
def test_nonexistent_paths(tmpdir_factory): """Passing nonexistent paths reports an error.""" data = Path(tmpdir_factory.mktemp("data")) dir1 = data / "dir1" dir2 = data / "dir2" assert not dir1.exists() assert not dir2.exists() with chdir(data): ret = check_fail(["rmdir", str(dir1)]) assert ret.stdout == "" assert ret.stderr != "" with chdir(data): ret = check_fail(["rmdir", str(dir1), str(dir2)]) assert ret.stdout == "" assert ret.stderr != ""
def test_main(): """Running `date` should print the current date, using the appropriate format string. TODO: The same tests, but UTC (with -u).""" # Default format string. assert check(["date"]).stdout == time.strftime("%a %b %e %H:%M:%S %Z %Y\n") assert len(check(["date"]).stderr) == 0 # Format string for ISO 8601. assert check(["date", "+%FT%TZ"]).stdout == time.strftime("%FT%TZ\n") assert len(check(["date", "+%FT%TZ"]).stderr) == 0 # Example without a leading + sign. assert len(check_fail(["date", "%H:%M:%S"]).stdout) == 0 assert "Invalid date string" in check_fail(["date", "%H:%M:%S"]).stderr
def test_no_flags(tree): """Test without -f, -i, or -r/-R.""" data, a, b = tree assert f"cannot remove '{data}': " in check_fail(["rm", str(data)]).stderr assert data.exists() assert a.exists() assert b.exists() ret = check(["rm", str(a)]) assert len(ret.stdout) == 0 assert len(ret.stderr) == 0
def test_nonexistent_filepath(tmpdir_factory): """Passing a nonexistent filepath reports an error.""" data = Path(tmpdir_factory.mktemp("data")) path = data / "file1.txt" assert not path.exists() ret = check_fail(["unlink", str(path)]) assert ret.stdout == "" assert len(ret.stderr) > 0 assert not path.exists()
def test_file1_and_file2_do_not_exist(tmpdir_factory): """If the source filepath and destination filepath don't exist, raises an error.""" data = Path(tmpdir_factory.mktemp("data")) file1 = data / "file1.txt" file2 = data / "file2.txt" assert not file1.exists() assert not file2.exists() ret = check_fail(["link", str(file1), str(file2)]) assert ret.stdout == "" assert len(ret.stderr) > 0 assert not file1.exists() assert not file2.exists()
def test_file1_does_not_exist(tmpdir_factory): """If the source filepath does not exist, raises an error.""" data = Path(tmpdir_factory.mktemp("data")) file1 = data / "file1.txt" file2 = data / "file2.txt" file2_text = "this should not be overwritten" file2.write_text(file2_text) assert not file1.exists() assert file2.read_text() == file2_text ret = check_fail(["link", str(file1), str(file2)]) assert ret.stdout == "" assert len(ret.stderr) > 0 assert not file1.exists() assert file2.read_text() == file2_text
def test_file2_exists(tmpdir_factory): """If the destination filepath exists, raises an error.""" data = Path(tmpdir_factory.mktemp("data")) file1 = data / "file1.txt" file2 = data / "file2.txt" file1_text = "hello, world!" file1.write_text(file1_text) file2_text = "this should not be overwritten" file2.write_text(file2_text) assert file1.read_text() == file1_text assert file2.read_text() == file2_text ret = check_fail(["link", str(file1), str(file2)]) assert ret.stdout == "" assert len(ret.stderr) > 0 assert file1.read_text() == file1_text assert file2.read_text() == file2_text
def test_extra_args(): """Passing more than one directory should raise an error.""" assert check_fail(["mkdir", "a", "b"]).stderr.startswith("mkdir:") assert check_fail(["mkdir", "-p", "a", "b"]).stderr.startswith("mkdir:") assert check_fail(["mkdir", "-m", "777", "a", "b"]).stderr.startswith("mkdir:")
def test_missing_args(): """No args => error of the form `mkdir: ...`""" assert check_fail(["mkdir"]).stderr.startswith("mkdir:")
def test_extra_args(): """Nothing to test: `date` accepts any number of arguments.""" assert len(check_fail(["date", "a", "b"]).stderr) > 0