Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

- name: Run flake8
run: |
flake8 --extend-ignore=E127 nodeenv.py tests
flake8 --extend-ignore=E127 nodeenv.py tests utils

activate-shells:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Nodeenv changelog

Version [unreleased]
--------------------
- Add an optional Windows utility, ``utils/integrate_nodeenv.py``, to move
an existing standalone Node environment into a Python venv without
overwriting destination files or downloading Node again.
- `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
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Any of `py310` to `py314` works, depending on the Python versions you have.

```sh
coverage run -p -m pytest -m 'not integration' tests
flake8 --extend-ignore=E127 nodeenv.py tests
flake8 --extend-ignore=E127 nodeenv.py tests utils
```

CI runs them on Python 3.10 to 3.14 on Ubuntu, macOS and Windows.
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ and add a node virtual environment to this existing new_venv::

$ nodeenv -p

To move an already populated standalone Windows Node environment into a
Python venv without downloading Node again, see the optional
`migration utility <https://github.com/ekalinin/nodeenv/blob/master/utils/README.md>`_
in a source checkout.

If you need to set the path to make used to build node::

$ nodeenv -m /usr/local/bin/gmake ENV
Expand Down
13 changes: 10 additions & 3 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,11 @@ def writefile(dest, content, overwrite=True, append=False):
else:
with open(dest, 'rb') as f:
c = f.read()
if content in c:
existing = c
if append:
content = content.replace(b'\r\n', b'\n')
existing = c.replace(b'\r\n', b'\n')
if content in existing:
logger.debug(' * Content %s already in place', dest)
return

Expand All @@ -752,13 +756,16 @@ def writefile(dest, content, overwrite=True, append=False):
# Editing the script voids that signature either way
# https://github.com/ekalinin/nodeenv/issues/243
head, signature, rest = c.partition(PS1_SIGNATURE)
first_line = c.partition(b'\n')[0]
newline = b'\r\n' if first_line.endswith(b'\r') else b'\n'
content = content.replace(b'\n', newline)
# and the appended part starts on a line of its own: a
# "deactivate.bat" ending with `:END` and no newline would
# swallow the first appended line into the label
if head and not head.endswith(b'\n'):
head += b'\n'
head += newline
if signature and not content.endswith(b'\n'):
content += b'\n'
content += newline
with open(dest, 'wb') as f:
f.write(head + content + signature + rest)
return
Expand Down
48 changes: 26 additions & 22 deletions tests/nodeenv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3499,38 +3499,42 @@ def test_with_certifi_is_configurable(self):
class TestWritefile:
"""Tests for writefile()"""

def test_append_starts_on_its_own_line(self, tmpdir):
"""A file not ending with a newline must not swallow the first
appended line: `:END` plus `@echo off` is a label, not a command
@pytest.mark.parametrize('newline', [b'\n', b'\r\n'])
def test_append_starts_on_its_own_line(self, tmpdir, newline):
"""A file not ending in a newline must not swallow the appended line.
"""
dest = tmpdir.join('deactivate.bat')
dest.write('@echo off\n:END')
original = b'@echo off\n:END'.replace(b'\n', newline)
content = b'@echo off\nset NODE_VIRTUAL_ENV=\n'
expected = original + newline + content.replace(b'\n', newline)
dest.write_binary(original)

nodeenv.writefile(str(dest), '@echo off\nset NODE_VIRTUAL_ENV=\n',
append=True)
nodeenv.writefile(str(dest), content, append=True)

assert dest.read() == (
'@echo off\n:END\n@echo off\nset NODE_VIRTUAL_ENV=\n')
assert dest.read_binary() == expected
mtime = os.stat(str(dest)).st_mtime_ns
nodeenv.writefile(str(dest), content, append=True)
assert dest.read_binary() == expected
assert os.stat(str(dest)).st_mtime_ns == mtime

def test_append_goes_before_a_powershell_signature(self, tmpdir):
@pytest.mark.parametrize('newline', [b'\n', b'\r\n'])
def test_append_goes_before_a_powershell_signature(self, tmpdir, newline):
"""
PowerShell refuses to parse code that follows the signature
block, and the Activate.ps1 python ships on Windows is signed
"""
dest = tmpdir.join('Activate.ps1')
dest.write('$env:VIRTUAL_ENV = "C:\\ws"\n'
'# SIG # Begin signature block\n'
'# MIIF...\n'
'# SIG # End signature block\n')

nodeenv.writefile(
str(dest), '$env:NODE_VIRTUAL_ENV = "C:\\ws"\n', append=True)

assert dest.read() == ('$env:VIRTUAL_ENV = "C:\\ws"\n'
'$env:NODE_VIRTUAL_ENV = "C:\\ws"\n'
'# SIG # Begin signature block\n'
'# MIIF...\n'
'# SIG # End signature block\n')
head = b'$env:VIRTUAL_ENV = "C:\\ws"\n'
content = b'$env:NODE_VIRTUAL_ENV = "C:\\ws"'
signature = (b'# SIG # Begin signature block\n'
b'# MIIF...\n'
b'# SIG # End signature block\n')
dest.write_binary((head + signature).replace(b'\n', newline))

nodeenv.writefile(str(dest), content, append=True)

expected = (head + content + b'\n' + signature).replace(b'\n', newline)
assert dest.read_binary() == expected


def _build_node_from_src(tmpdir, node_version, python2_path):
Expand Down
Loading