99
1010# blocktodo: make friendly error message if `nose` is missing
1111# blocktodo: make friendly error message if launched from wrong path
12+ # blocktodo: add help message
1213
1314import os .path
1415import sys
@@ -88,47 +89,92 @@ def configureWhere(self, where):
8889 )
8990
9091
92+ def prepare_zip_testing ():
93+ result = os .system (
94+ '"%s"' % \
95+ os .path .realpath (
96+ os .path .join (our_path , 'misc' , 'testing' , 'zip' , 'make_zip.py' )
97+ )
98+ )
99+
100+ if result != 0 :
101+ exit (result )
102+
103+ for package_name in package_names :
104+ assert not exists (package_name )
105+ assert package_name not in sys .modules
106+
107+ for i , package_name in enumerate (package_names ):
108+ zip_file = os .path .realpath (
109+ os .path .join (our_path , 'misc' , 'testing' , 'zip' , 'build' ,
110+ (str (i ) + '.zip' ))
111+ )
112+ assert zip_file not in sys .path
113+ sys .path .append (zip_file )
114+ package = __import__ (package_name )
115+ assert '.zip' in package .__file__
116+ print ('Imported all GarlicSim packages from zip files.' )
117+
118+
119+ def ensure_zip_testing_was_legit ():
120+ '''
121+ Ensure GarlicSim packages were indeed used from zip.
122+
123+ This is used only in `--from-zip` testing, to ensure that the GarlicSim
124+ packages weren't used from the source folders accidentally.
125+ '''
126+ print ('Confirming all GarlicSim packages were used from zip files... ' ,
127+ end = '' )
128+ for i , package_name in enumerate (package_names ):
129+ assert package_name in sys .modules
130+ package = sys .modules [package_name ]
131+ assert '.zip' in package .__file__
132+
133+ raw_module_names = \
134+ [module_name for module_name in sys .modules .keys () if
135+ module_name .split ('.' )[0 ] == package_name ]
136+
137+ # Filtering out module names that map to `None`, because of a bug,
138+ # probably in `zipimport`, which litters `sys.modules` with
139+ # non-sense modules:
140+
141+ module_names = [module_name for module_name in raw_module_names if
142+ sys .modules [module_name ] is not None ]
143+
144+ module_paths = [sys .modules [module_name ].__file__ for
145+ module_name in module_names ]
146+
147+ zip_file_name = str (i ) + '.zip'
148+ snippet_from_real_folder_path = \
149+ os .path .sep .join ((package_name , package_name ))
150+ for module_path in module_paths :
151+ assert zip_file_name in module_path
152+ assert snippet_from_real_folder_path not in module_path
153+ print ('Done.' )
154+
155+
156+ package_names = ['garlicsim' , 'garlicsim_lib' , 'garlicsim_wx' ]
157+
158+
91159if __name__ == '__main__' :
92160
93161 argv = sys .argv [:]
94162
95- package_names = ['garlicsim' , 'garlicsim_lib' , 'garlicsim_wx' ]
96-
97163 testing_from_zip = '--from-zip' in argv
98164 if testing_from_zip :
99165 argv .remove ('--from-zip' )
100- result = os .system (
101- '"%s"' % \
102- os .path .realpath (
103- os .path .join (our_path , 'misc' , 'testing' , 'zip' , 'make_zip.py' )
104- )
105- )
106-
107- if result != 0 :
108- exit (result )
109-
110- for package_name in package_names :
111- assert not exists (package_name )
112- assert package_name not in sys .modules
113166
114- for i , package_name in enumerate (package_names ):
115- zip_file = os .path .realpath (
116- os .path .join (our_path , 'misc' , 'testing' , 'zip' , 'build' ,
117- (str (i ) + '.zip' ))
118- )
119- assert zip_file not in sys .path
120- sys .path .append (zip_file )
121- package = __import__ (package_name )
122- assert '.zip' in package .__file__
123- print ('Imported all GarlicSim packages from zip files.' )
124-
125- argv += ['garlicsim/test_garlicsim' ,
126- 'garlicsim_lib/test_garlicsim_lib' ,
127- 'garlicsim_wx/test_garlicsim_wx' ][::- 1 ]
167+ # Adding test packages to arguments to have Nose take tests from them:
168+ argv += \
169+ ['%s/test_%s' % package_name for package_name in package_names ][::- 1 ]
170+ # (Reversing package order for now, to put the shorter tests first.)
171+
172+ ###########################################################################
173+ # This is the heavy line, which actually causes Nose to start running
174+ # tests:
128175 TestProgram (argv = argv )
176+ ###########################################################################
129177
130178 if testing_from_zip :
131- for package_name in package_names :
132- assert package_name in sys .modules
133- package = sys .modules [package_name ]
134- assert '.zip' in package .__file__
179+ ensure_zip_testing_was_legit (package_names , sys , os )
180+
0 commit comments