tests.run_scripts_test   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 31
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A TestRunScripts.test_success() 0 11 2
A TestRunScripts.test_non_executable() 0 10 2
A TestRunScripts.test_failure() 0 9 2
1
# -*- coding: utf-8 -*-
2
from dvnv.dvnv import run_scripts
3
4
SHEBANG = "#!/usr/bin/env bash"
5
6
7
class TestRunScripts:
8
    """Tests for run_scripts"""
9
10
    def test_non_executable(self, capsys, tmp_path):
11
        """Assert that a non-executable file will be logged to stderr"""
12
        script_dir = tmp_path / "scripts"
13
        script_dir.mkdir(parents=True)
14
        nonexecutable = script_dir / "nonexecutable"
15
        with nonexecutable.open("w") as file:
16
            file.write(f"{SHEBANG}\necho 'hello'")
17
        assert run_scripts(script_dir, "python", "test") is True
18
        captured = capsys.readouterr()
19
        assert captured.err == "[WARN] 'nonexecutable' is not executable! Skipping.\n"
20
21
    def test_failure(self, tmp_path):
22
        """Test case where a command in a script is not found"""
23
        script_dir = tmp_path / "scripts"
24
        script_dir.mkdir(parents=True)
25
        invalid_command = script_dir / "invalid_command"
26
        with invalid_command.open("w") as file:
27
            file.write(f"{SHEBANG}\nfake_command")
28
        invalid_command.chmod(0o111)
29
        assert run_scripts(script_dir, "python", "test") is False
30
31
    def test_success(self, capsys, tmp_path):
32
        """Test case for success"""
33
        script_dir = tmp_path / "scripts"
34
        script_dir.mkdir(parents=True)
35
        valid_command = script_dir / "hello"
36
        with valid_command.open("w") as file:
37
            file.write(f"{SHEBANG}\necho 'hello!'")
38
        valid_command.chmod(0o777)
39
        assert run_scripts(script_dir, "python", "test") is True
40
        captured = capsys.readouterr()
41
        assert captured.out == "Running 'hello'...\n"
42