Пример #1
0
def test_file_source_directory(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.mkdir('directory')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
Пример #2
0
def test_file_source_and_path_not_writable(tmpdir, monkeypatch):
    dp = tmpdir.join('testing.txt').ensure()
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    builtins_open = open

    def open_(  # pylint: disable=unused-argument
            file,
            mode='r',
            buffering=-1,
            encoding=None,
            errors=None,
            newline=None,
            closefd=True,
            opener=None):
        if file == dp.strpath and mode == 'wb':
            raise PermissionError(13, 'Permission denied', file)
        else:
            return builtins_open(file, mode, buffering, encoding, errors,
                                 newline, closefd, opener)

    monkeypatch.setattr('builtins.open', open_)

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
Пример #3
0
def test_alias_not_writable(tmpdir):
    sp = tmpdir.join('source.txt').ensure()

    # TODO: Determine a suitable way to mock the appropriate PyObjC methods to do this better
    file = File(path='/alias', source=sp.strpath, state='alias')
    with pytest.raises(ActionError):
        file.process()
Пример #4
0
def test_file_empty_exists(tmpdir):
    p = tmpdir.join('testing.txt')
    p.write('Hello there')

    file = File(path=p.strpath, state='file')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
Пример #5
0
def test_absent_exists(tmpdir):
    p = tmpdir.join('testing.txt').ensure()

    file = File(path=p.strpath, state='absent')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert not p.exists()
Пример #6
0
def test_directory_inexistent(tmpdir):
    p = tmpdir.join('directory')

    file = File(path=p.strpath, state='directory')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.isdir()
Пример #7
0
def test_file_empty_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, state='file')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.read() == ''
Пример #8
0
def test_symlink_exists_same(tmpdir):
    p = tmpdir.join('testing.txt')
    p.mksymlinkto('source.txt')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
Пример #9
0
def test_symlink_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': p.strpath})
    assert p.islink()
    assert p.readlink() == 'source.txt'
Пример #10
0
def test_symlink_destination_path_is_directory(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, source='source.txt', state='symlink')
    assert file.process() == ActionResponse(
        changed=True, data={'path': p.join('source.txt').strpath})
    assert p.join('source.txt').islink()
    assert p.join('source.txt').readlink() == 'source.txt'
Пример #11
0
def test_file_empty_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('testing.txt')

    monkeypatch.setattr('builtins.open',
                        build_open_with_permission_error(p.strpath))

    file = File(path=p.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
Пример #12
0
def test_file_source_exists_same(tmpdir):
    dp = tmpdir.join('testing.txt')
    dp.write('Hello there')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': dp.strpath})
Пример #13
0
def test_file_source_inexistent(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    assert dp.read() == 'Hello there'
Пример #14
0
def test_file_source_destination_path_is_directory(tmpdir):
    dp = tmpdir.mkdir('directory')
    sp = tmpdir.join('source.txt')
    sp.write('Hello there')

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    assert file.process() == ActionResponse(
        changed=True, data={'path': dp.join('source.txt').strpath})
    assert dp.join('source.txt').read() == 'Hello there'
Пример #15
0
def test_file_source_and_path_not_readable(tmpdir, monkeypatch):
    dp = tmpdir.join('testing.txt').ensure()
    sp = tmpdir.join('source.txt').ensure()

    monkeypatch.setattr('builtins.open',
                        build_open_with_permission_error(dp.strpath))

    file = File(path=dp.strpath, source=sp.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
Пример #16
0
def test_alias_exists_different(tmpdir):
    dp = tmpdir.join('test.alias')
    shutil.copy(os.path.join(FIXTURE_PATH, 'file', 'test.alias'), dp.strpath)
    sp = tmpdir.join('source.txt').ensure()

    file = File(path=dp.strpath, source=sp.strpath, state='alias')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    alias_data = dp.read_binary()
    assert alias_data.startswith(b'book')
    assert b'source.txt' in alias_data
Пример #17
0
def test_alias_path_inexistent(tmpdir):
    dp = tmpdir.join('testing.txt')
    sp = tmpdir.join('source.txt').ensure()

    file = File(path=dp.strpath, source=sp.strpath, state='alias')
    assert file.process() == ActionResponse(changed=True,
                                            data={'path': dp.strpath})
    assert dp.isfile()
    alias_data = dp.read_binary()
    assert alias_data.startswith(b'book')
    assert b'source.txt' in alias_data
Пример #18
0
def test_directory_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('directory')

    os_mkdir = os.mkdir

    def mkdir(path, *args, **kwargs):
        if path == p.strpath:
            raise PermissionError(13, 'Permission denied', file)
        else:
            return os_mkdir(path, *args, **kwargs)

    monkeypatch.setattr('os.mkdir', mkdir)

    file = File(path=p.strpath, state='directory')
    with pytest.raises(ActionError):
        file.process()
Пример #19
0
def test_symlink_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('testing.txt')

    os_symlink = os.symlink

    def symlink(src, dst, *args, **kwargs):
        if dst == p.strpath:
            raise PermissionError(13, 'Permission denied', file)
        else:
            return os_symlink(src, dst, *args, **kwargs)

    monkeypatch.setattr('os.symlink', symlink)

    file = File(path=p.strpath, source='source.txt', state='symlink')
    with pytest.raises(ActionError):
        file.process()
Пример #20
0
def test_alias_exists_same(tmpdir):
    p = tmpdir.join('test.alias')
    shutil.copy(os.path.join(FIXTURE_PATH, 'file', 'test.alias'), p.strpath)

    try:
        # Create a file at the source path as it must exist for the alias to work
        with open('/private/var/tmp/test.txt', 'w'):
            pass

        file = File(path=p.strpath,
                    source='/private/var/tmp/test.txt',
                    state='alias')
        assert file.process() == ActionResponse(changed=False,
                                                data={'path': p.strpath})
    finally:
        os.remove('/private/var/tmp/test.txt')
Пример #21
0
def test_file_empty_directory(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, state='file')
    with pytest.raises(ActionError):
        file.process()
Пример #22
0
def test_directory_exists(tmpdir):
    p = tmpdir.mkdir('directory')

    file = File(path=p.strpath, state='directory')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
Пример #23
0
def test_argument_state_symlink_after_init_invalid():
    file = File(path='boo.txt')
    with pytest.raises(ValueError):
        file.state = 'symlink'
Пример #24
0
def test_argument_state_directory_after_init_invalid():
    file = File(path='boo.txt', source='source')
    with pytest.raises(ValueError):
        file.state = 'directory'
Пример #25
0
def test_argument_state_absent_after_init_invalid():
    file = File(path='boo.txt', source='source.txt')
    with pytest.raises(ValueError):
        file.state = 'absent'
Пример #26
0
def test_argument_source_directory_symlink_combination_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', state='symlink')
Пример #27
0
def test_absent_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, state='absent')
    assert file.process() == ActionResponse(changed=False,
                                            data={'path': p.strpath})
Пример #28
0
def test_argument_source_absent_state_combination_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', source='source.txt', state='absent')
Пример #29
0
def test_alias_source_inexistent(tmpdir):
    p = tmpdir.join('testing.txt')

    file = File(path=p.strpath, source='source.txt', state='alias')
    with pytest.raises(ActionError):
        file.process()
Пример #30
0
def test_argument_state_invalid():
    with pytest.raises(ValueError):
        File(path='boo.txt', state='hmmm')