From cd86e00dfd064bc2f9eeb3215d243df670085c2f Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Wed, 7 Mar 2018 14:00:58 -0600 Subject: [PATCH] Allow action option "active" to accept True/False conditions #914 --- test/test_actions.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ test/test_task.py | 26 +--------------------- 2 files changed, 54 insertions(+), 25 deletions(-) diff --git a/test/test_actions.py b/test/test_actions.py index 7f093acff..230024f46 100644 --- a/test/test_actions.py +++ b/test/test_actions.py @@ -22,6 +22,7 @@ import os import sys +import glob import unittest import shutil @@ -601,5 +602,57 @@ def testRegenerateReport(self): Base_Executor(wf).run() + def testActiveActionOption(self): + '''Test the active option of actions''' + # disallow + self.assertRaises(Exception, SoS_Script, ''' +[1] +rep = range(5) +input: for_each = 'rep' +# ff should change and be usable inside run +ff = f"{_rep}.txt" +run: expand=True, active=1,2 +echo {ff} +touch temp/{ff} +''') + # + for active, result in [ + ('0', ['temp/0.txt']), + ('-1', ['temp/4.txt']), + ('(1,2)', ['temp/1.txt', 'temp/2.txt']), + ('[2,3]', ['temp/2.txt', 'temp/3.txt']), + ('(0,2,4)', ['temp/0.txt', 'temp/2.txt', 'temp/4.txt']), + ('slice(1,None)', ['temp/1.txt', 'temp/2.txt', 'temp/3.txt', 'temp/4.txt']), + ('slice(1,-2)', ['temp/1.txt', 'temp/2.txt']), + ('slice(None,None,2)', ['temp/0.txt', 'temp/2.txt', 'temp/4.txt']), + ('True', ['temp/0.txt', 'temp/1.txt', 'temp/2.txt', 'temp/3.txt', 'temp/4.txt']), + ('False', []), + ]: + if os.path.isdir('temp'): + shutil.rmtree('temp') + os.mkdir('temp') + # test first iteration + script = SoS_Script((''' +[1] +rep = range(5) +input: for_each = 'rep' +# ff should change and be usable inside run +ff = f"{_rep}.txt" +run: expand=True, active=%s +echo {ff} +touch temp/{ff} +''' % active).replace('/', os.sep)) + wf = script.workflow() + env.config['sig_mode'] = 'force' + env.config['wait_for_task'] = True + Base_Executor(wf).run() + files = list(glob.glob(os.path.join('temp', '*.txt'))) + self.assertEqual(sorted(files), sorted([x.replace('/', os.sep) for x in result])) + # + # test last iteration + shutil.rmtree('temp') + + + if __name__ == '__main__': unittest.main() diff --git a/test/test_task.py b/test/test_task.py index 3a20134ff..f95fb8e7e 100644 --- a/test/test_task.py +++ b/test/test_task.py @@ -185,7 +185,7 @@ def testPrependPath(self): Base_Executor(wf).run() - def testActiveActionOption(self): + def testActiveTaskOption(self): '''Test the active option of actions''' # disallow self.assertRaises(ParsingError, SoS_Script, ''' @@ -212,30 +212,6 @@ def testActiveActionOption(self): if os.path.isdir('temp'): shutil.rmtree('temp') os.mkdir('temp') - # test first iteration - script = SoS_Script((''' -[1] -rep = range(5) -input: for_each = 'rep' -# ff should change and be usable inside run -ff = f"{_rep}.txt" -run: expand=True, active=%s -echo {ff} -touch temp/{ff} -''' % active).replace('/', os.sep)) - wf = script.workflow() - env.config['sig_mode'] = 'force' - env.config['wait_for_task'] = True - Host.reset() - Base_Executor(wf).run() - files = list(glob.glob(os.path.join('temp', '*.txt'))) - self.assertEqual(sorted(files), sorted([x.replace('/', os.sep) for x in result])) - # - # test last iteration - shutil.rmtree('temp') - # - # test active option for task - os.mkdir('temp') script = SoS_Script((''' [1] rep = range(5)