test node directly

This commit is contained in:
Anthony Sottile
2023-01-31 19:37:37 -05:00
parent 3c2ca11332
commit 909dd0e8a1
8 changed files with 41 additions and 72 deletions

View File

@@ -1,5 +0,0 @@
- id: foo
name: Foo
entry: foo
language: node
files: \.js$

View File

@@ -1,3 +0,0 @@
#!/usr/bin/env node
console.log('Hello World');

View File

@@ -1,5 +0,0 @@
{
"name": "foo",
"version": "0.0.1",
"bin": {"foo": "./bin/main.js"}
}

View File

@@ -1,6 +0,0 @@
- id: versioned-node-hook
name: Versioned node hook
entry: versioned-node-hook
language: node
language_version: 9.3.0
files: \.js$

View File

@@ -1,4 +0,0 @@
#!/usr/bin/env node
console.log(process.version);
console.log('Hello World');

View File

@@ -1,5 +0,0 @@
{
"name": "versioned-node-hook",
"version": "0.0.1",
"bin": {"versioned-node-hook": "./bin/main.js"}
}

View File

@@ -13,7 +13,9 @@ from pre_commit import envcontext
from pre_commit import parse_shebang
from pre_commit.languages import node
from pre_commit.prefix import Prefix
from pre_commit.store import _make_local_repo
from pre_commit.util import cmd_output
from testing.language_helpers import run_language
from testing.util import xfailif_windows
@@ -109,3 +111,42 @@ def test_installs_without_links_outside_env(tmpdir):
with node.in_env(prefix, 'system'):
assert cmd_output('foo')[1] == 'success!\n'
def _make_hello_world(tmp_path):
package_json = '''\
{"name": "t", "version": "0.0.1", "bin": {"node-hello": "./bin/main.js"}}
'''
tmp_path.joinpath('package.json').write_text(package_json)
bin_dir = tmp_path.joinpath('bin')
bin_dir.mkdir()
bin_dir.joinpath('main.js').write_text(
'#!/usr/bin/env node\n'
'console.log("Hello World");\n',
)
def test_node_hook_system(tmp_path):
_make_hello_world(tmp_path)
ret = run_language(tmp_path, node, 'node-hello')
assert ret == (0, b'Hello World\n')
def test_node_with_user_config_set(tmp_path):
cfg = tmp_path.joinpath('cfg')
cfg.write_text('cache=/dne\n')
with envcontext.envcontext((('NPM_CONFIG_USERCONFIG', str(cfg)),)):
test_node_hook_system(tmp_path)
@pytest.mark.parametrize('version', (C.DEFAULT, '18.13.0'))
def test_node_hook_versions(tmp_path, version):
_make_hello_world(tmp_path)
ret = run_language(tmp_path, node, 'node-hello', version=version)
assert ret == (0, b'Hello World\n')
def test_node_additional_deps(tmp_path):
_make_local_repo(str(tmp_path))
ret, out = run_language(tmp_path, node, 'npm ls -g', deps=('lodash',))
assert b' lodash@' in out

View File

@@ -17,7 +17,6 @@ from pre_commit.envcontext import envcontext
from pre_commit.hook import Hook
from pre_commit.languages import golang
from pre_commit.languages import helpers
from pre_commit.languages import node
from pre_commit.languages import python
from pre_commit.languages.all import languages
from pre_commit.prefix import Prefix
@@ -193,38 +192,6 @@ def test_run_a_docker_image_hook(tempdir_factory, store, hook_id):
)
def test_run_a_node_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'node_hooks_repo',
'foo', [os.devnull], b'Hello World\n',
)
def test_run_a_node_hook_default_version(tempdir_factory, store):
# make sure that this continues to work for platforms where node is not
# installed at the system
with mock.patch.object(
node,
'get_default_version',
return_value=C.DEFAULT,
):
test_run_a_node_hook(tempdir_factory, store)
def test_run_versioned_node_hook(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'node_versioned_hooks_repo',
'versioned-node-hook', [os.devnull], b'v9.3.0\nHello World\n',
)
def test_node_hook_with_npm_userconfig_set(tempdir_factory, store, tmpdir):
cfg = tmpdir.join('cfg')
cfg.write('cache=/dne\n')
with mock.patch.dict(os.environ, NPM_CONFIG_USERCONFIG=str(cfg)):
test_run_a_node_hook(tempdir_factory, store)
def test_system_hook_with_spaces(tempdir_factory, store):
_test_hook_repo(
tempdir_factory, store, 'system_hook_with_spaces_repo',
@@ -482,17 +449,6 @@ def test_repository_state_compatibility(tempdir_factory, store, v):
assert _hook_installed(hook) is True
def test_additional_node_dependencies_installed(tempdir_factory, store):
path = make_repo(tempdir_factory, 'node_hooks_repo')
config = make_config_from_repo(path)
# Careful to choose a small package that's not depped by npm
config['hooks'][0]['additional_dependencies'] = ['lodash']
hook = _get_hook(config, store, 'foo')
with node.in_env(hook.prefix, hook.language_version):
output = cmd_output('npm', 'ls', '-g')[1]
assert 'lodash' in output
def test_additional_golang_dependencies_installed(
tempdir_factory, store,
):