@@ -423,11 +423,11 @@ def test_isolate_npm_shim_content(tmpdir):
423423@pytest .fixture
424424def fake_win ():
425425 """
426- Pretend the host is Windows. install_activate() links nodejs.exe with
427- mklink there, which exists on Windows only, so callit() is stubbed too .
426+ Pretend the host is Windows. The nodejs.exe link is stubbed out, so
427+ Scripts/ holds only the generated scripts .
428428 """
429429 with mock .patch .object (nodeenv , 'is_WIN' , True ):
430- with mock .patch .object (nodeenv , 'callit ' ):
430+ with mock .patch .object (os , 'symlink ' ):
431431 yield
432432
433433
@@ -581,3 +581,58 @@ def test_isolate_npm_node_system_shim_exports(tmpdir):
581581 env_dir = str (tmpdir )
582582 assert out .decode ('utf-8' ).strip () == '|' .join ((
583583 env_dir + '/.npm' , env_dir + '/.npmrc' , env_dir + '/.npm-init.js' ))
584+
585+
586+ # nodejs.exe is linked with os.symlink/os.link, not with `mklink` through
587+ # cmd, which needed quoting and elevation
588+ # https://github.com/ekalinin/nodeenv/issues/303
589+ #
590+ # is_WIN is faked, so these run on every platform.
591+
592+
593+ def _install_win_node (tmpdir ):
594+ bin_dir = tmpdir .join ('Scripts' )
595+ bin_dir .mkdir ()
596+ bin_dir .join ('node.exe' ).write ('node' )
597+
598+ with mock .patch .object (nodeenv , 'is_WIN' , True ), \
599+ mock .patch .object (sys , 'argv' , ['nodeenv' , str (tmpdir )]):
600+ opts = nodeenv .parse_args ()
601+ nodeenv .install_activate (str (tmpdir ), opts )
602+ return bin_dir
603+
604+
605+ @pytest .mark .skipif (nodeenv .is_WIN , reason = 'symlinks need privileges on win32' )
606+ def test_win_nodejs_symlink_created (tmpdir ):
607+ # forward slashes and a space broke the unquoted mklink command line
608+ env_dir = tmpdir .join ('with space' )
609+ env_dir .mkdir ()
610+
611+ bin_dir = _install_win_node (env_dir )
612+
613+ nodejs_file = bin_dir .join ('nodejs.exe' )
614+ assert os .path .islink (str (nodejs_file ))
615+ assert os .readlink (str (nodejs_file )) == 'node.exe'
616+
617+
618+ def test_win_nodejs_hardlink_when_symlink_fails (tmpdir ):
619+ # no elevation and no Developer Mode: a hard link needs neither
620+ with mock .patch .object (os , 'symlink' , side_effect = OSError ):
621+ bin_dir = _install_win_node (tmpdir )
622+
623+ nodejs_file = bin_dir .join ('nodejs.exe' )
624+ assert not os .path .islink (str (nodejs_file ))
625+ assert os .path .samefile (str (nodejs_file ), str (bin_dir .join ('node.exe' )))
626+
627+
628+ def test_win_nodejs_link_failure_is_a_warning (tmpdir ):
629+ with mock .patch .object (os , 'symlink' , side_effect = OSError ), \
630+ mock .patch .object (os , 'link' , side_effect = OSError ), \
631+ mock .patch .object (nodeenv .logger , 'warning' ) as m_warning , \
632+ mock .patch .object (nodeenv .logger , 'error' ) as m_error :
633+ bin_dir = _install_win_node (tmpdir )
634+
635+ assert not bin_dir .join ('nodejs.exe' ).check ()
636+ m_error .assert_not_called ()
637+ m_warning .assert_called_once ()
638+ assert 'nodejs.exe' in m_warning .call_args [0 ][0 ]
0 commit comments