Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/ekalinin/nodeenv/issues/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
Expand Down
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::

Expand Down
9 changes: 5 additions & 4 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
69 changes: 69 additions & 0 deletions tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading