66import warnings
77import textwrap
88
9+ from unittest import mock
10+
911from distutils .dist import Distribution , fix_help_options
1012from distutils .cmd import Command
1113
@@ -18,7 +20,7 @@ class test_dist(Command):
1820
1921 user_options = [
2022 ("sample-option=" , "S" , "help text" ),
21- ]
23+ ]
2224
2325 def initialize_options (self ):
2426 self .sample_option = None
@@ -77,6 +79,64 @@ def test_command_packages_cmdline(self):
7779 self .assertIsInstance (cmd , test_dist )
7880 self .assertEqual (cmd .sample_option , "sometext" )
7981
82+ def test_venv_install_options (self ):
83+ sys .argv .append ("install" )
84+ self .addCleanup (os .unlink , TESTFN )
85+
86+ fakepath = '/somedir'
87+
88+ with open (TESTFN , "w" ) as f :
89+ print (("[install]\n "
90+ "install-base = {0}\n "
91+ "install-platbase = {0}\n "
92+ "install-lib = {0}\n "
93+ "install-platlib = {0}\n "
94+ "install-purelib = {0}\n "
95+ "install-headers = {0}\n "
96+ "install-scripts = {0}\n "
97+ "install-data = {0}\n "
98+ "prefix = {0}\n "
99+ "exec-prefix = {0}\n "
100+ "home = {0}\n "
101+ "user = {0}\n "
102+ "root = {0}" ).format (fakepath ), file = f )
103+
104+ # Base case: Not in a Virtual Environment
105+ with mock .patch .multiple (sys , prefix = '/a' , base_prefix = '/a' ) as values :
106+ d = self .create_distribution ([TESTFN ])
107+
108+ option_tuple = (TESTFN , fakepath )
109+
110+ result_dict = {
111+ 'install_base' : option_tuple ,
112+ 'install_platbase' : option_tuple ,
113+ 'install_lib' : option_tuple ,
114+ 'install_platlib' : option_tuple ,
115+ 'install_purelib' : option_tuple ,
116+ 'install_headers' : option_tuple ,
117+ 'install_scripts' : option_tuple ,
118+ 'install_data' : option_tuple ,
119+ 'prefix' : option_tuple ,
120+ 'exec_prefix' : option_tuple ,
121+ 'home' : option_tuple ,
122+ 'user' : option_tuple ,
123+ 'root' : option_tuple ,
124+ }
125+
126+ self .assertEqual (
127+ sorted (d .command_options .get ('install' ).keys ()),
128+ sorted (result_dict .keys ()))
129+
130+ for (key , value ) in d .command_options .get ('install' ).items ():
131+ self .assertEqual (value , result_dict [key ])
132+
133+ # Test case: In a Virtual Environment
134+ with mock .patch .multiple (sys , prefix = '/a' , base_prefix = '/b' ) as values :
135+ d = self .create_distribution ([TESTFN ])
136+
137+ for key in result_dict .keys ():
138+ self .assertNotIn (key , d .command_options .get ('install' , {}))
139+
80140 def test_command_packages_configfile (self ):
81141 sys .argv .append ("build" )
82142 self .addCleanup (os .unlink , TESTFN )
@@ -304,7 +364,7 @@ def test_custom_pydistutils(self):
304364 os .environ ['HOME' ] = temp_dir
305365 files = dist .find_config_files ()
306366 self .assertIn (user_filename , files ,
307- '%r not found in %r' % (user_filename , files ))
367+ '%r not found in %r' % (user_filename , files ))
308368 finally :
309369 os .remove (user_filename )
310370
0 commit comments