Skip to content

Commit

Permalink
test node directly
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Feb 1, 2023
1 parent 3c2ca11 commit 909dd0e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 72 deletions.
5 changes: 0 additions & 5 deletions testing/resources/node_hooks_repo/.pre-commit-hooks.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions testing/resources/node_hooks_repo/bin/main.js

This file was deleted.

5 changes: 0 additions & 5 deletions testing/resources/node_hooks_repo/package.json

This file was deleted.

This file was deleted.

4 changes: 0 additions & 4 deletions testing/resources/node_versioned_hooks_repo/bin/main.js

This file was deleted.

5 changes: 0 additions & 5 deletions testing/resources/node_versioned_hooks_repo/package.json

This file was deleted.

41 changes: 41 additions & 0 deletions tests/languages/node_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
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


Expand Down Expand Up @@ -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
44 changes: 0 additions & 44 deletions tests/repository_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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,
):
Expand Down

0 comments on commit 909dd0e

Please sign in to comment.