aboutsummaryrefslogtreecommitdiff
path: root/tests/test_sandbox.py
blob: bb2ea980e6928629100c5ca0aa9ea39ffb355798 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    test_sandbox.py                                    :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: cacharle <me@cacharle.xyz>                 +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2021/03/03 08:09:00 by cacharle          #+#    #+#              #
#    Updated: 2021/03/06 09:58:17 by cacharle         ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import pytest
import shutil
from pathlib import Path

from minishell_test import sandbox
from minishell_test.config import Config

Config.init([])


@pytest.fixture
def sandbox_dirs():
    return [Path(s) for s in [
        "foo",
        "asdfasdfas;dlkfjas;dkfjas;lkdfj",
        "z/y",
        "a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z",
    ]]


def test_create(tmpdir, monkeypatch, sandbox_dirs):
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        assert not sandbox_dir.exists()
        sandbox.create()
        assert sandbox_dir.exists()


def test_remove(tmpdir, monkeypatch, sandbox_dirs):
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        sandbox.create()
        assert sandbox_dir.exists()
        sandbox.remove()
        assert not sandbox_dir.exists()
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        sandbox.create()
        assert sandbox_dir.exists()
        sandbox_dir.chmod(000)
        sandbox.remove()
        assert not sandbox_dir.exists()
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        sandbox.create()
        assert sandbox_dir.exists()
        shutil.rmtree(sandbox_dir)
        sandbox.remove()
        assert not sandbox_dir.exists()


def test_context(tmpdir, monkeypatch, sandbox_dirs):
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        assert not sandbox_dir.exists()
        with sandbox.context():
            assert sandbox_dir.exists()
        assert not sandbox_dir.exists()
    for sandbox_dir in sandbox_dirs:
        sandbox_dir = Path(tmpdir / sandbox_dir)
        monkeypatch.setattr(Config, 'sandbox_dir', sandbox_dir)
        with pytest.raises(RuntimeError):
            assert not sandbox_dir.exists()
            with sandbox.context():
                assert sandbox_dir.exists()
                raise RuntimeError
            assert not sandbox_dir.exists()