Example #1
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
Example #2
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'
Example #3
0
def test_file_action_set_file_attributes_change_group_inexistent(
        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='hmmm')
    with pytest.raises(ActionError):
        file_action.set_file_attributes(p.strpath)
Example #4
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)
Example #5
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)
Example #6
0
def test_file_action_set_file_attributes_change_owner_same(
        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='fots')
    assert file_action.set_file_attributes(p.strpath) is False
Example #7
0
def test_file_action_set_file_attributes_change_group_different(
        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)

    with mock.patch('os.chown') as chown_mock:
        file_action = FileAction(group='staff')
        assert file_action.set_file_attributes(p.strpath) is True
        assert chown_mock.called
        assert chown_mock.call_args == mock.call(p.strpath, -1, 20)
Example #8
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)
Example #9
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
Example #10
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
Example #11
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)
Example #12
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)