From 7246b42101bc1cde9cb897b4d16032ab879ebaa1 Mon Sep 17 00:00:00 2001 From: Eugene Kalinin Date: Sun, 1 Nov 2026 23:07:34 +0300 Subject: [PATCH] feat(nodeenv): read DEST_DIR from env_dir in the config env_dir in the [nodeenv] section of the config file sets DEST_DIR for runs that pass neither DEST_DIR nor -p. DEST_DIR on the command line and -p take precedence. The value is used as is, a relative path is taken from the current directory. Fixes #377 --- CHANGES | 4 +++ README.rst | 2 ++ nodeenv.py | 9 +++--- tests/nodeenv_test.py | 69 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 1f2ec3b..d23f270 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,10 @@ Nodeenv changelog Version [unreleased] -------------------- +- `env_dir` in the `[nodeenv]` section of the config file sets DEST_DIR for + runs that pass neither DEST_DIR nor `-p`. DEST_DIR on the command line and + `-p` take precedence over it + `#377 `_ - The package metadata moved from `setup.py` to `pyproject.toml`, and `setup.py` is gone: build nodeenv with a PEP 517 frontend such as `pip` or `python -m build`. Building needs setuptools 77 or later. The license diff --git a/README.rst b/README.rst index 05e5cf1..a382564 100644 --- a/README.rst +++ b/README.rst @@ -529,6 +529,8 @@ Configuration ------------- You can use the INI-style file ``~/.nodeenvrc`` to set default values for many options, the keys in that file are the long command-line option names. +``env_dir`` sets ``DEST_DIR`` for runs that pass neither ``DEST_DIR`` nor ``-p``: +an absolute path, or a path relative to the current directory. These are the available options and their defaults:: diff --git a/nodeenv.py b/nodeenv.py index d04b1fa..3d7c540 100644 --- a/nodeenv.py +++ b/nodeenv.py @@ -88,6 +88,7 @@ class Config(object): isolate_npm = False clean_src = True parallel_extract = True + env_dir = None @classmethod def _load(cls, configfiles, verbose=False): @@ -659,8 +660,8 @@ def make_parser(): 'if certifi is installed. Ignored with --ignore_ssl_certs.') parser.add_argument( - metavar='DEST_DIR', dest='env_dir', nargs='?', - help='Destination directory') + metavar='DEST_DIR', dest='env_dir', nargs='?', default=Config.env_dir, + help='Destination directory, defaults to env_dir from the config file') return parser @@ -690,8 +691,8 @@ def parse_args(check=True): if not args.list: if not args.python_virtualenv and not args.env_dir: - parser.error('You must provide a DEST_DIR or ' - 'use current python virtualenv') + parser.error('You must provide a DEST_DIR, set env_dir in the ' + 'config file or use current python virtualenv') return args diff --git a/tests/nodeenv_test.py b/tests/nodeenv_test.py index 3501317..5181134 100644 --- a/tests/nodeenv_test.py +++ b/tests/nodeenv_test.py @@ -1658,6 +1658,75 @@ def test_parse_args_parallel_extract(): assert nodeenv.parse_args().parallel_extract is True +def test_env_dir_default(): + assert nodeenv.Config._default['env_dir'] is None + + +@pytest.mark.parametrize('value, expected', [ + ('/opt/node', '/opt/node'), + ('"/opt/my node"', '/opt/my node'), + ('~/node', '~/node'), + ('', ''), +]) +def test_env_dir_is_configurable(tmpdir, value, expected): + """DEST_DIR is read from env_dir in the config as is, see #377""" + rc = tmpdir.join('nodeenvrc') + rc.write('[nodeenv]\nenv_dir = %s\n' % value) + try: + nodeenv.Config._load([str(rc)]) + assert nodeenv.Config.env_dir == expected + finally: + nodeenv.Config.env_dir = None + + +def test_parse_args_env_dir(tmpdir): + venv = str(tmpdir.mkdir('venv')) + with mock.patch.object(nodeenv.Config, 'env_dir', '/opt/node'): + with mock.patch.object(sys, 'argv', ['nodeenv']): + assert nodeenv.parse_args().env_dir == '/opt/node' + # DEST_DIR and -p win over the config + with mock.patch.object(sys, 'argv', ['nodeenv', './my-env']): + assert nodeenv.parse_args().env_dir == './my-env' + with mock.patch.object(sys, 'argv', ['nodeenv', '-p', venv]): + assert nodeenv.get_env_dir(nodeenv.parse_args()) == venv + with mock.patch.object(sys, 'argv', ['nodeenv', '-p']), \ + mock.patch.dict(os.environ, {'VIRTUAL_ENV': venv}): + assert nodeenv.get_env_dir(nodeenv.parse_args()) == venv + # an empty value is the same as no env_dir + with mock.patch.object(nodeenv.Config, 'env_dir', ''): + with mock.patch.object(sys, 'argv', ['nodeenv']): + with pytest.raises(SystemExit): + nodeenv.parse_args() + + +def test_parse_args_without_dest_dir(capsys): + with mock.patch.object(sys, 'argv', ['nodeenv']): + with pytest.raises(SystemExit): + nodeenv.parse_args() + err = capsys.readouterr().err + assert 'You must provide a DEST_DIR' in err + assert 'env_dir' in err + + +@pytest.mark.usefixtures('mock_host_platform') +def test_main_env_dir_from_config(tmpdir): + env_dir = str(tmpdir.join('env')) + rc = tmpdir.join('nodeenvrc') + rc.write('[nodeenv]\nenv_dir = %s\n' % env_dir) + argv = ['nodeenv', '-C', str(rc), '--node', '22.11.0'] + try: + with mock.patch.object(sys, 'argv', argv), \ + mock.patch.object(nodeenv, 'create_environment') as create: + nodeenv.main() + assert create.call_args[0][0] == env_dir + with mock.patch.object(sys, 'argv', argv + ['--update']), \ + mock.patch.object(nodeenv, 'install_packages') as install: + nodeenv.main() + assert install.call_args[0][0] == env_dir + finally: + nodeenv.Config.env_dir = None + + def _run_create_environment(tmpdir, extra): """ Run create_environment() in tmpdir without installing anything