Beispiel #1
0
def test_file_action_set_file_attributes_change_mode_same(tmpdir):
    p = tmpdir.join('test.txt').ensure()
    p.chmod(0o644)

    file_action = FileAction(mode='0644')
    assert file_action.set_file_attributes(p.strpath) is False
    assert oct(p.stat().mode)[-4:] == '0644'
Beispiel #2
0
def test_file_action_remove_symlink(tmpdir):
    p = tmpdir.join('testing.txt')
    p.mksymlinkto('something.txt')

    file_action = FileAction()
    assert file_action.remove(p.strpath) is True
    assert not p.exists()
Beispiel #3
0
def test_file_action_set_file_attributes_change_flags_same(tmpdir):
    p = tmpdir.join('test.txt').ensure()
    os.chflags(p.strpath, 0b1000000000000000)

    file_action = FileAction(flags=['hidden'])
    assert file_action.set_file_attributes(p.strpath) is False
    assert p.stat().flags == 0b1000000000000000
Beispiel #4
0
def test_file_action_remove_directory(tmpdir):
    p = tmpdir.mkdir('test')
    p.join('file1.txt').ensure()
    p.join('file2.txt').ensure()

    file_action = FileAction()
    assert file_action.remove(p.strpath) is True
    assert not p.exists()
Beispiel #5
0
def test_file_action_remove_directory_not_writable(tmpdir, monkeypatch):
    p = tmpdir.mkdir('test')

    def rmtree(path):  # pylint: disable=unused-argument
        raise PermissionError(13, 'Permission denied', p.strpath)

    monkeypatch.setattr('shutil.rmtree', rmtree)

    file_action = FileAction()
    with pytest.raises(ActionError):
        file_action.remove(p.strpath)
Beispiel #6
0
def test_file_action_remove_file_not_writable(tmpdir, monkeypatch):
    p = tmpdir.join('test').ensure()

    def remove(path):  # pylint: disable=unused-argument
        raise PermissionError(13, 'Permission denied', p.strpath)

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

    file_action = FileAction()
    with pytest.raises(ActionError):
        file_action.remove(p.strpath)
Beispiel #7
0
def test_file_action_set_file_attributes_change_flags_not_writable(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt').ensure()

    def chflags(path, flags, follow_symlinks=True):  # pylint: disable=unused-argument
        raise PermissionError(1, 'Operation not permitted', p.strpath)

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

    file_action = FileAction(flags=['hidden'])
    with pytest.raises(ActionError):
        assert file_action.set_file_attributes(p.strpath)
Beispiel #8
0
def test_file_action_set_file_attributes_change_mode_not_writable(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt').ensure()
    p.chmod(0o600)

    def chmod(path, mode, follow_symlinks=True):  # pylint: disable=unused-argument
        raise PermissionError(1, 'Operation not permitted', p.strpath)

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

    file_action = FileAction(mode='0644')
    with pytest.raises(ActionError):
        assert file_action.set_file_attributes(p.strpath)
Beispiel #9
0
def test_file_action_set_file_attributes_change_group_same(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt')

    monkeypatch.setattr('grp.getgrnam', helpers.getgrnam)

    def stat(path, follow_symlinks=True):  # pylint: disable=unused-argument
        if path == p.strpath:
            return os.stat_result((33188, 3955467, 16777220, 1, 501, 20, 1308,
                                   1533358970, 1532951575, 1532951575))
        else:
            raise FileNotFoundError(
                f"[Errno 2] No such file or directory: '{path}'")

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

    file_action = FileAction(group='staff')
    assert file_action.set_file_attributes(p.strpath) is False
Beispiel #10
0
def test_file_action_set_file_attributes_change_owner_inexistent(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt')

    monkeypatch.setattr('pwd.getpwnam', helpers.getpwnam)

    def stat(path, follow_symlinks=True):  # pylint: disable=unused-argument
        if path == p.strpath:
            return os.stat_result((33188, 3955467, 16777220, 1, 501, 20, 1308,
                                   1533358970, 1532951575, 1532951575))
        else:
            raise FileNotFoundError(
                f"[Errno 2] No such file or directory: '{path}'")

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

    file_action = FileAction(owner='hmmm')
    with pytest.raises(ActionError):
        file_action.set_file_attributes(p.strpath)
Beispiel #11
0
def test_file_action_set_file_attributes_change_owner_different(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt')

    monkeypatch.setattr('pwd.getpwnam', helpers.getpwnam)

    def stat(path, follow_symlinks=True):  # pylint: disable=unused-argument
        if path == p.strpath:
            return os.stat_result((33188, 3955467, 16777220, 1, 502, 20, 1308,
                                   1533358970, 1532951575, 1532951575))
        else:
            raise FileNotFoundError(
                f"[Errno 2] No such file or directory: '{path}'")

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

    with mock.patch('os.chown') as chown_mock:
        file_action = FileAction(owner='fots')
        assert file_action.set_file_attributes(p.strpath) is True
        assert chown_mock.called
        assert chown_mock.call_args == mock.call(p.strpath, 501, -1)
Beispiel #12
0
def test_file_action_set_file_attributes_change_group_not_writable(
        tmpdir, monkeypatch):
    p = tmpdir.join('test.txt')

    monkeypatch.setattr('grp.getgrnam', helpers.getgrnam)

    def stat(path, follow_symlinks=True):  # pylint: disable=unused-argument
        if path == p.strpath:
            return os.stat_result((33188, 3955467, 16777220, 1, 501, 21, 1308,
                                   1533358970, 1532951575, 1532951575))
        else:
            raise FileNotFoundError(
                f"[Errno 2] No such file or directory: '{path}'")

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

    def chown(path, uid, gid, follow_symlinks=True):  # pylint: disable=unused-argument
        raise PermissionError(1, 'Operation not permitted', p.strpath)

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

    file_action = FileAction(group='staff')
    with pytest.raises(ActionError):
        assert file_action.set_file_attributes(p.strpath)
Beispiel #13
0
def test_file_action_set_file_attributes_change_flags_different(tmpdir):
    p = tmpdir.join('test.txt').ensure()

    file_action = FileAction(flags=['hidden'])
    assert file_action.set_file_attributes(p.strpath) is True
    assert p.stat().flags == 0b1000000000000000
Beispiel #14
0
def test_file_action_remove_file(tmpdir):
    p = tmpdir.join('test').ensure()

    file_action = FileAction()
    assert file_action.remove(p.strpath) is True
    assert not p.exists()
Beispiel #15
0
def test_file_action_remove_strange_file():
    file_action = FileAction()
    assert file_action.remove('/dev/null') is False
Beispiel #16
0
def test_file_action_remove_inexistent(tmpdir):
    p = tmpdir.join('test')

    file_action = FileAction()
    assert file_action.remove(p.strpath) is False
Beispiel #17
0
def test_file_action_set_file_attributes_change_flags_invalid(tmpdir):
    p = tmpdir.join('test.txt').ensure()

    file_action = FileAction(flags=['hmmm'])
    with pytest.raises(ActionError):
        file_action.set_file_attributes(p.strpath)
Beispiel #18
0
def test_file_action_set_file_attributes_no_changes(tmpdir):
    p = tmpdir.join('test.txt').ensure()

    file_action = FileAction()
    assert file_action.set_file_attributes(p.strpath) is False
Beispiel #19
0
def test_file_action_set_file_attributes_inexistent(tmpdir):
    p = tmpdir.join('test.txt')

    file_action = FileAction(mode='0644')
    with pytest.raises(ActionError):
        file_action.set_file_attributes(p.strpath)