forked from pre-commit/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_test.py
More file actions
80 lines (56 loc) · 2.22 KB
/
node_test.py
File metadata and controls
80 lines (56 loc) · 2.22 KB
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
import os
import shutil
import sys
from unittest import mock
import pytest
import pre_commit.constants as C
from pre_commit import envcontext
from pre_commit import parse_shebang
from pre_commit.languages import node
from pre_commit.prefix import Prefix
from testing.util import xfailif_windows
ACTUAL_GET_DEFAULT_VERSION = node.get_default_version.__wrapped__
@pytest.fixture
def is_linux():
with mock.patch.object(sys, 'platform', 'linux'):
yield
@pytest.fixture
def is_win32():
with mock.patch.object(sys, 'platform', 'win32'):
yield
@pytest.fixture
def find_exe_mck():
with mock.patch.object(parse_shebang, 'find_executable') as mck:
yield mck
@pytest.mark.usefixtures('is_linux')
def test_sets_system_when_node_and_npm_are_available(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
assert ACTUAL_GET_DEFAULT_VERSION() == 'system'
@pytest.mark.usefixtures('is_linux')
def test_uses_default_when_node_and_npm_are_not_available(find_exe_mck):
find_exe_mck.return_value = None
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
@pytest.mark.usefixtures('is_win32')
def test_sets_default_on_windows(find_exe_mck):
find_exe_mck.return_value = '/path/to/exe'
assert ACTUAL_GET_DEFAULT_VERSION() == C.DEFAULT
@xfailif_windows # pragma: win32 no cover
def test_healthy_system_node(tmpdir):
tmpdir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
prefix = Prefix(str(tmpdir))
node.install_environment(prefix, 'system', ())
assert node.healthy(prefix, 'system')
@xfailif_windows # pragma: win32 no cover
def test_unhealthy_if_system_node_goes_missing(tmpdir):
bin_dir = tmpdir.join('bin').ensure_dir()
node_bin = bin_dir.join('node')
node_bin.mksymlinkto(shutil.which('node'))
prefix_dir = tmpdir.join('prefix').ensure_dir()
prefix_dir.join('package.json').write('{"name": "t", "version": "1.0.0"}')
path = ('PATH', (str(bin_dir), os.pathsep, envcontext.Var('PATH')))
with envcontext.envcontext((path,)):
prefix = Prefix(str(prefix_dir))
node.install_environment(prefix, 'system', ())
assert node.healthy(prefix, 'system')
node_bin.remove()
assert not node.healthy(prefix, 'system')