Passed
Push — main ( 2d6d90...79017f )
by J
01:57
created

tests.main_test   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 59
dl 0
loc 81
rs 10
c 0
b 0
f 0

5 Functions

Rating   Name   Duplication   Size   Complexity  
A test_empty_dir() 0 9 4
A test_install_no_run() 0 16 3
A test_list_langs_populated() 0 12 4
A test_permission_error() 0 12 3
A test_list_langs_empty_dir() 0 12 3
1
# -*- coding: utf-8 -*-
2
import sys
3
from tempfile import TemporaryDirectory
4
from unittest.mock import patch
5
6
import pytest
7
8
from devenv.devenv import main, parse_args
0 ignored issues
show
Bug introduced by
The name devenv does not seem to exist in module devenv.
Loading history...
introduced by
Cannot import 'devenv.devenv' due to syntax error 'invalid syntax (<unknown>, line 154)'
Loading history...
9
10
11
def test_empty_dir():
12
    # usually we'd use pytest's tmp_path, but it wasn't cleaning up in this instance
13
    with TemporaryDirectory() as tmpdir:
14
        with patch.object(
15
            sys, "argv", ["devenv", "--scripts_path", tmpdir, "python", "devenv"]
16
        ):
17
            args = parse_args()
18
            with pytest.raises(SystemError):
19
                main(args)
20
21
22
def test_install_no_run(tmp_path):
23
    scripts_path = tmp_path / "scripts"
24
    with patch.object(
25
        sys,
26
        "argv",
27
        [
28
            "devenv",
29
            None,
30
            None,
31
            "--install_scripts",
32
            "--scripts_path",
33
            str(scripts_path),
34
        ],
35
    ):
36
        with pytest.raises(SystemExit):
37
            main(parse_args())
38
39
40
def test_list_langs_empty_dir(capsys, tmp_path):
41
    with patch.object(
42
        sys,
43
        "argv",
44
        ["devenv", None, None, "--scripts_path", str(tmp_path), "--list_langs"],
45
    ):
46
        with pytest.raises(SystemExit):
47
            main(parse_args())
48
            captured = capsys.readouterr()
49
            assert (
50
                captured.stderr
51
                == f"Return with `--install_scripts` to populate {tmp_path}\n"
52
            )
53
54
55
def test_list_langs_populated(capsys, tmp_path):
56
    with patch.object(
57
        sys,
58
        "argv",
59
        ["devenv", None, None, "--scripts_path", str(tmp_path), "--list_langs"],
60
    ):
61
        with pytest.raises(SystemExit):
62
            for lang in ("all", "python", "vim"):
63
                (tmp_path / lang).mkdir()
64
            main(parse_args())
65
            captured = capsys.readouterr()
66
            assert captured.stdout == "Available languages are:  all python vim\n"
67
68
69
def test_permission_error(tmp_path):
70
    scripts_path = tmp_path / "scripts"
71
    scripts_path.mkdir(0o000)
72
    with patch.object(
73
        sys,
74
        "argv",
75
        ["devenv", "python", "test-proj", "--scripts_path", str(scripts_path)],
76
    ):
77
        with pytest.raises(PermissionError):
78
            main(parse_args())
79
    # add all permissions back so the directory can be cleaned up
80
    scripts_path.chmod(0o777)
81