Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-10-08 08:36:30

0001 """Exercise sourcing the actual environment helper, without ROOT or Condor."""
0002 import os
0003 from pathlib import Path
0004 import shutil
0005 import subprocess
0006 import tempfile
0007 import unittest
0008 
0009 HELPER = Path(__file__).resolve().parents[1]/'example-env.tcsh'
0010 TCSH = shutil.which('tcsh')
0011 
0012 
0013 @unittest.skipUnless(TCSH, 'tcsh is not installed')
0014 class ExampleEnvStatusTests(unittest.TestCase):
0015     def setUp(self):
0016         self.temp = tempfile.TemporaryDirectory()
0017         self.addCleanup(self.temp.cleanup)
0018         self.root = Path(self.temp.name)
0019         self.cwd = self.root/'repo/examples/yall/sample'
0020         self.cwd.mkdir(parents=True)
0021         self.work = self.root/'work'
0022         self.env = {k:v for k,v in os.environ.items()
0023                     if not k.startswith(('LFHCAL_', 'YALL_', 'EIC_'))}
0024         self.env.update(LFHCAL_HOME=str(self.root), LFHCAL_WORK=str(self.work),
0025                         EIC_SHELL='/not-invoked/eic-shell')
0026         (self.root/'activate.tcsh').write_text('/bin/true\n')
0027 
0028     def source(self, argument='sample'):
0029         # Capture status immediately, and prove source did not exit its caller.
0030         script = (f'source "{HELPER}" {argument}\n'
0031                   'set rc = $status\n'
0032                   'echo CALLER_SURVIVED\n'
0033                   'exit $rc\n')
0034         result = subprocess.run([TCSH, '-f', '-c', script], cwd=self.cwd,
0035                                 env=self.env, text=True, capture_output=True)
0036         self.assertIn('CALLER_SURVIVED', result.stdout, result.stdout+result.stderr)
0037         return result
0038 
0039     def test_success_and_work_directories(self):
0040         result = self.source()
0041         self.assertEqual(result.returncode, 0, result.stdout+result.stderr)
0042         self.assertTrue((self.work/'sample').is_dir())
0043         self.assertTrue((self.work/'campaigns').is_dir())
0044 
0045     def test_activation_failure_propagates(self):
0046         (self.root/'activate.tcsh').write_text('/bin/false\n')
0047         self.assertNotEqual(self.source().returncode, 0)
0048         self.assertFalse(self.work.exists())
0049 
0050     def test_missing_argument_propagates(self):
0051         self.assertNotEqual(self.source('').returncode, 0)
0052 
0053     def test_directory_creation_failure_propagates(self):
0054         self.work.write_text('existing file, do not replace')
0055         self.assertNotEqual(self.source().returncode, 0)
0056         self.assertEqual(self.work.read_text(), 'existing file, do not replace')
0057 
0058 
0059 if __name__ == '__main__':
0060     unittest.main()