Beispiel #1
0
def test_shutil_move(src_path, dst_path, expected_exception):
    src_path = os.path.join(src_path, "unique-nonexistent-directory")
    os.mkdir(src_path)
    
    file_path = os.path.join(src_path, "unique-nonexistent-file.txt")
    with open(file_path, 'w') as f:
        f.write("test\n")

    shield.install_hooks()
    try:
        if expected_exception:
            with pytest.raises(expected_exception):
                shutil.move(src_path, dst_path)
        else:
            shutil.move(src_path, dst_path)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if expected_exception:
        shutil.rmtree(src_path)
    else:
        final_path = os.path.join(dst_path, "unique-nonexistent-directory")
        shutil.rmtree(final_path)
Beispiel #2
0
def test_builtin_file():
    shield.install_hooks()
    try:
        with pytest.raises(shield.common.ShieldError):
            file("something")
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()
Beispiel #3
0
def test_builtin_open_weird():
    shield.install_hooks()
    try:
        with pytest.raises(TypeError):
            open(1)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()
Beispiel #4
0
def test_disabled(disabled_call):
    shield.install_hooks()
    try:
        # With this try-except, other errors will be caught
        #   and hooks will still be uninstalled
        with pytest.raises(shield.common.ShieldError):
            disabled_call()
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()
Beispiel #5
0
def test_io_open_weird():
    shield.install_hooks()
    try:
        with pytest.raises(IOError):
            # Picked 64 because 1 (used to test __builtin__)
            #   actually works for io.open()
            #   because io.open() can take fds
            io.open(64)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()
Beispiel #6
0
def os_mkdir_fixture(request):
    path, mode, expected_exception = request.param

    shield.install_hooks()
    try:
        yield (path, mode, expected_exception)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if expected_exception is None:
        os.rmdir(path)
Beispiel #7
0
def os_rmdir_fixture(request):
    created_directory = False
    path, should_create, expected_exception = request.param

    if should_create:
        path = shield.common.make_unique(path)
        os.mkdir(path)
        created_directory = True

    shield.install_hooks()
    try:
        yield (path, expected_exception)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if created_directory and expected_exception is not None:
        os.rmdir(path)
Beispiel #8
0
def os_open_fixture(request):
    created_file = False
    path, should_create, flags, expected_exception = request.param

    if should_create:
        path = shield.common.make_unique(path)
        with open(path, 'w') as f:
            f.write(test_file_contents)
        created_file = True

    shield.install_hooks()
    try:
        yield (path, flags, expected_exception)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if created_file:
        os.remove(path)
Beispiel #9
0
def os_open_fixture(request):
    created_dir = False
    src, dst, should_create, expected_exception = request.param

    build_zip_file(src)

    if should_create:
        dst = shield.common.make_unique(dst)
        os.mkdir(dst)
        created_dir = True

    shield.install_hooks()
    try:
        yield (path, flags, expected_exception)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if created_dir:
        shutil.rmtree(dst)
Beispiel #10
0
def os_rename_fixture(request):
    created_file = False
    src, should_create, dst, expected_exception = request.param

    if should_create:
        src = shield.common.make_unique(src)
        with open(src, 'w') as f:
            f.write(test_file_contents)
        created_file = True

    shield.install_hooks()
    try:
        yield (src, dst, expected_exception)
    except Exception:
        traceback.print_exc()
    finally:
        shield.uninstall_hooks()

    if created_file:
        if expected_exception is None:
            os.remove(dst)
        else:
            os.remove(src)
Beispiel #11
0
def test_shutil_rmtree(base_path, expected_exception):
    shield.uninstall_hooks()
    
    path = os.path.join(base_path, "unique-nonexistent-directory")
    os.mkdir(path)
    
    file_path = os.path.join(path, "unique-nonexistent-file.txt")
    with open(file_path, 'w') as f:
        f.write("test\n")

    shield.install_hooks()

    if expected_exception:
        try:
            shutil.rmtree(path)
        except expected_exception:
            print "Correctly shielded!"
    else:
        shutil.rmtree(path)

    if expected_exception:
        shield.uninstall_hooks()
        shutil.rmtree(path)
        shield.install_hooks()