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 dvnv.dvnv import main, parse_args |
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", ["dvnv", "--scripts_dir", tmpdir, "python", "dvnv"] |
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_dir = tmp_path / "scripts" |
24
|
|
|
with patch.object( |
25
|
|
|
sys, |
26
|
|
|
"argv", |
27
|
|
|
[ |
28
|
|
|
"dvnv", |
29
|
|
|
None, |
30
|
|
|
None, |
31
|
|
|
"--install_scripts", |
32
|
|
|
"--scripts_dir", |
33
|
|
|
str(scripts_dir), |
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
|
|
|
["dvnv", None, None, "--scripts_dir", 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
|
|
|
["dvnv", None, None, "--scripts_dir", 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_run_scripts_error(tmp_path): |
70
|
|
|
with patch.object( |
71
|
|
|
sys, |
72
|
|
|
"argv", |
73
|
|
|
[ |
74
|
|
|
"dvnv", |
75
|
|
|
"python", |
76
|
|
|
"exit-error", |
77
|
|
|
"--scripts_dir", |
78
|
|
|
str(tmp_path), |
79
|
|
|
], |
80
|
|
|
): |
81
|
|
|
for lang in ("all", "python"): |
82
|
|
|
(tmp_path / lang).mkdir() |
83
|
|
|
err_script = tmp_path / "python" / "err.sh" |
84
|
|
|
with err_script.open("w+") as file: |
85
|
|
|
file.write("#!/bin/sh\nexit 1\n") |
86
|
|
|
file.seek(0) |
87
|
|
|
err_script.chmod(0o777) |
88
|
|
|
with pytest.raises(SystemExit): |
89
|
|
|
main(parse_args()) |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def test_permission_error(tmp_path): |
93
|
|
|
scripts_dir = tmp_path / "scripts" |
94
|
|
|
scripts_dir.mkdir(0o000) |
95
|
|
|
with patch.object( |
96
|
|
|
sys, |
97
|
|
|
"argv", |
98
|
|
|
["dvnv", "python", "test-proj", "--scripts_dir", str(scripts_dir)], |
99
|
|
|
): |
100
|
|
|
with pytest.raises(PermissionError): |
101
|
|
|
main(parse_args()) |
102
|
|
|
# add all permissions back so the directory can be cleaned up |
103
|
|
|
scripts_dir.chmod(0o777) |
104
|
|
|
|