3939import stat
4040import sys
4141import tempfile
42+ import textwrap
4243from distutils .version import LooseVersion
4344from test .framework .utilities import EnhancedTestCase , TestLoaderFiltered
4445from test .framework .package import mock_fpm
@@ -2638,34 +2639,38 @@ def test_toy_build_trace(self):
26382639 def test_toy_build_hooks (self ):
26392640 """Test use of --hooks."""
26402641 hooks_file = os .path .join (self .test_prefix , 'my_hooks.py' )
2641- hooks_file_txt = ' \n ' . join ([
2642- " import os" ,
2643- '' ,
2644- " def start_hook():" ,
2645- " print('start hook triggered')" ,
2646- '' ,
2647- " def parse_hook(ec):" ,
2648- " print('%s %s' % (ec.name, ec.version))" ,
2642+ hooks_file_txt = textwrap . dedent ( """
2643+ import os
2644+
2645+ def start_hook():
2646+ print('start hook triggered')
2647+
2648+ def parse_hook(ec):
2649+ print('%s %s' % (ec.name, ec.version))
26492650 # print sources value to check that raw untemplated strings are exposed in parse_hook
2650- " print(ec['sources'])" ,
2651+ print(ec['sources'])
26512652 # try appending to postinstallcmd to see whether the modification is actually picked up
26522653 # (required templating to be disabled before parse_hook is called)
2653- " ec['postinstallcmds'].append('echo toy')" ,
2654- " print(ec['postinstallcmds'][-1])" ,
2655- '' ,
2656- "def pre_configure_hook(self):" ,
2657- " print('pre-configure: toy.source: %s' % os.path.exists('toy.source'))" ,
2658- '' ,
2659- "def post_configure_hook(self):" ,
2660- " print('post-configure: toy.source: %s' % os.path.exists('toy.source'))" ,
2661- '' ,
2662- "def post_install_hook(self):" ,
2663- " print('in post-install hook for %s v%s' % (self.name, self.version))" ,
2664- " print(', '.join(sorted(os.listdir(self.installdir))))" ,
2665- '' ,
2666- "def end_hook():" ,
2667- " print('end hook triggered, all done!')" ,
2668- ])
2654+ ec['postinstallcmds'].append('echo toy')
2655+ print(ec['postinstallcmds'][-1])
2656+
2657+ def pre_configure_hook(self):
2658+ print('pre-configure: toy.source: %s' % os.path.exists('toy.source'))
2659+
2660+ def post_configure_hook(self):
2661+ print('post-configure: toy.source: %s' % os.path.exists('toy.source'))
2662+
2663+ def post_install_hook(self):
2664+ print('in post-install hook for %s v%s' % (self.name, self.version))
2665+ print(', '.join(sorted(os.listdir(self.installdir))))
2666+
2667+ def module_write_hook(self, module_path, module_txt):
2668+ print('in module-write hook hook for %s' % os.path.basename(module_path))
2669+ return module_txt.replace('Toy C program, 100% toy.', 'Not a toy anymore')
2670+
2671+ def end_hook():
2672+ print('end hook triggered, all done!')
2673+ """ )
26692674 write_file (hooks_file , hooks_file_txt )
26702675
26712676 self .mock_stderr (True )
@@ -2676,26 +2681,44 @@ def test_toy_build_hooks(self):
26762681 self .mock_stderr (False )
26772682 self .mock_stdout (False )
26782683
2684+ test_mod_path = os .path .join (self .test_installpath , 'modules' , 'all' )
2685+ toy_mod_file = os .path .join (test_mod_path , 'toy' , '0.0' )
2686+ if get_module_syntax () == 'Lua' :
2687+ toy_mod_file += '.lua'
2688+
26792689 self .assertEqual (stderr , '' )
2680- expected_output = '\n ' .join ([
2681- "== Running start hook..." ,
2682- "start hook triggered" ,
2683- "== Running parse hook for toy-0.0.eb..." ,
2684- "toy 0.0" ,
2685- "['%(name)s-%(version)s.tar.gz']" ,
2686- "echo toy" ,
2687- "== Running pre-configure hook..." ,
2688- "pre-configure: toy.source: True" ,
2689- "== Running post-configure hook..." ,
2690- "post-configure: toy.source: False" ,
2691- "== Running post-install hook..." ,
2692- "in post-install hook for toy v0.0" ,
2693- "bin, lib" ,
2694- "== Running end hook..." ,
2695- "end hook triggered, all done!" ,
2696- ])
2690+ # There are 4 modules written:
2691+ # Sanitycheck for extensions and main easyblock (1 each), main and devel module
2692+ expected_output = textwrap .dedent ("""
2693+ == Running start hook...
2694+ start hook triggered
2695+ == Running parse hook for toy-0.0.eb...
2696+ toy 0.0
2697+ ['%(name)s-%(version)s.tar.gz']
2698+ echo toy
2699+ == Running pre-configure hook...
2700+ pre-configure: toy.source: True
2701+ == Running post-configure hook...
2702+ post-configure: toy.source: False
2703+ == Running post-install hook...
2704+ in post-install hook for toy v0.0
2705+ bin, lib
2706+ == Running module_write hook...
2707+ in module-write hook hook for {mod_name}
2708+ == Running module_write hook...
2709+ in module-write hook hook for {mod_name}
2710+ == Running module_write hook...
2711+ in module-write hook hook for {mod_name}
2712+ == Running module_write hook...
2713+ in module-write hook hook for {mod_name}
2714+ == Running end hook...
2715+ end hook triggered, all done!
2716+ """ ).strip ().format (mod_name = os .path .basename (toy_mod_file ))
26972717 self .assertEqual (stdout .strip (), expected_output )
26982718
2719+ toy_mod = read_file (toy_mod_file )
2720+ self .assertIn ('Not a toy anymore' , toy_mod )
2721+
26992722 def test_toy_multi_deps (self ):
27002723 """Test installation of toy easyconfig that uses multi_deps."""
27012724 test_ecs_dir = os .path .join (os .path .dirname (os .path .abspath (__file__ )), 'easyconfigs' , 'test_ecs' )
0 commit comments