File size: 1,773 Bytes
05c9ac2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from unittest import mock
import pytest
from mlagents_envs.env_utils import validate_environment_path, launch_executable
from mlagents_envs.exception import UnityEnvironmentException
from mlagents_envs.logging_util import (
    set_log_level,
    get_logger,
    INFO,
    ERROR,
    FATAL,
    CRITICAL,
    DEBUG,
)


def mock_glob_method(path):
    """
    Given a path input, returns a list of candidates
    """
    if ".x86" in path:
        return ["linux"]
    if ".app" in path:
        return ["darwin"]
    if ".exe" in path:
        return ["win32"]
    if "*" in path:
        return "Any"
    return []


@mock.patch("sys.platform")
@mock.patch("glob.glob")
def test_validate_path_empty(glob_mock, platform_mock):
    glob_mock.return_value = None
    path = validate_environment_path(" ")
    assert path is None


@mock.patch("mlagents_envs.env_utils.get_platform")
@mock.patch("glob.glob")
def test_validate_path(glob_mock, platform_mock):
    glob_mock.side_effect = mock_glob_method
    for platform in ["linux", "darwin", "win32"]:
        platform_mock.return_value = platform
        path = validate_environment_path(" ")
        assert path == platform


@mock.patch("glob.glob")
@mock.patch("subprocess.Popen")
def test_launch_executable(mock_popen, glob_mock):
    with pytest.raises(UnityEnvironmentException):
        launch_executable(" ", [])
    glob_mock.return_value = ["FakeLaunchPath"]
    launch_executable(" ", [])
    mock_popen.side_effect = PermissionError("Fake permission error")
    with pytest.raises(UnityEnvironmentException):
        launch_executable(" ", [])


def test_set_logging_level():
    for level in [INFO, ERROR, FATAL, CRITICAL, DEBUG]:
        set_log_level(level)
        assert get_logger("test").level == level