Skip to content

Commit d9ddf43

Browse files
committed
feat: add predeactivate hooks for Windows
- Implemented predeactivate hooks for Windows, creating both predeactivate.bat and predeactivate.ps1 files. - Updated set_predeactivate_hook function to handle both Windows and Unix systems. - Enhanced tests to verify the creation and content of predeactivate hooks based on the operating system.
1 parent d75aadb commit d9ddf43

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

nodeenv.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,14 @@ def install_activate(env_dir, args):
986986

987987

988988
def set_predeactivate_hook(env_dir):
989-
if not is_WIN:
989+
if is_WIN:
990+
# Windows: create predeactivate.bat for CMD and predeactivate.ps1 for PowerShell
991+
with open(join(env_dir, 'Scripts', 'predeactivate.bat'), 'a') as hook:
992+
hook.write(PREDEACTIVATE_BAT)
993+
with open(join(env_dir, 'Scripts', 'predeactivate.ps1'), 'a') as hook:
994+
hook.write(PREDEACTIVATE_PS1)
995+
else:
996+
# Unix: create predeactivate for bash/sh
990997
with open(join(env_dir, 'bin', 'predeactivate'), 'a') as hook:
991998
hook.write(PREDEACTIVATE_SH)
992999

@@ -1564,6 +1571,21 @@ def main():
15641571
if type -p deactivate_node > /dev/null; then deactivate_node;fi
15651572
"""
15661573

1574+
PREDEACTIVATE_BAT = """\
1575+
@echo off
1576+
REM Deactivate Node.js environment
1577+
if exist "%NODE_VIRTUAL_ENV%\\Scripts\\deactivate.bat" (
1578+
call "%NODE_VIRTUAL_ENV%\\Scripts\\deactivate.bat"
1579+
)
1580+
"""
1581+
1582+
PREDEACTIVATE_PS1 = """\
1583+
# Deactivate Node.js environment
1584+
if (Get-Command deactivate -ErrorAction SilentlyContinue) {
1585+
deactivate
1586+
}
1587+
"""
1588+
15671589
CYGWIN_NODE = """#!/bin/sh
15681590
15691591
if [ -r "$1" ]; then

tests/nodeenv_test.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,25 @@ def test_predeactivate_hook(tmpdir):
116116
# Throw error if environment directory has no bin path
117117
with pytest.raises((OSError, IOError)):
118118
nodeenv.set_predeactivate_hook(tmpdir.strpath)
119-
tmpdir.mkdir('bin')
120-
nodeenv.set_predeactivate_hook(tmpdir.strpath)
121-
p = tmpdir.join('bin').join('predeactivate')
122-
assert 'deactivate_node' in p.read()
119+
120+
if nodeenv.is_WIN:
121+
tmpdir.mkdir('Scripts')
122+
nodeenv.set_predeactivate_hook(tmpdir.strpath)
123+
# Check BAT file
124+
p_bat = tmpdir.join('Scripts').join('predeactivate.bat')
125+
assert p_bat.exists()
126+
content_bat = p_bat.read()
127+
assert 'deactivate.bat' in content_bat
128+
# Check PS1 file
129+
p_ps1 = tmpdir.join('Scripts').join('predeactivate.ps1')
130+
assert p_ps1.exists()
131+
assert 'deactivate' in p_ps1.read()
132+
else:
133+
tmpdir.mkdir('bin')
134+
nodeenv.set_predeactivate_hook(tmpdir.strpath)
135+
p = tmpdir.join('bin').join('predeactivate')
136+
assert 'deactivate_node' in p.read()
137+
123138

124139

125140
def test_mirror_option():

0 commit comments

Comments
 (0)