def testChange(self): home = os.getenv('HOME') self.assertNotEqual(home, os.getcwd(), "Test does not work from 'HOME' directory.") obj = mooseutils.CurrentWorkingDirectory(home) self.assertEqual(obj.external, os.getcwd()) self.assertEqual(obj.internal, home) with mooseutils.CurrentWorkingDirectory(home) as cwd: self.assertEqual(os.getcwd(), home) self.assertEqual(cwd.internal, os.getcwd()) self.assertNotEqual(home, os.getcwd()) self.assertEqual(cwd.external, os.getcwd())
def make_formatter(filename, root, plugin_dirs): """ Create the `Formatter` object from the [Formatter] block of the `pyhit.Node` of *root*. By default, a `BasicFormatter` is created. Refer to `make_controllers` function for information on the supplied input arguments. """ # Locate/create the [Formatter] node f_node = moosetree.find(root, func=lambda n: n.fullpath == '/Formatter') if f_node is None: f_node = root.append('Formatter', type='BasicFormatter') # Factory for building Formatter objects f_factory = factory.Factory(plugin_dirs=plugin_dirs, plugin_types=(Formatter, )) f_factory.load() if f_factory.status() > 0: msg = "An error occurred registering the Formatter type, see console message(s) for details." raise RuntimeError(msg) # Create the Formatter object by parsing the input file formatters = list() f_parser = factory.Parser(f_factory, formatters) with mooseutils.CurrentWorkingDirectory(os.path.dirname(filename)): f_parser._parseNode(filename, f_node) if f_parser.status() > 0: msg = "An error occurred during parsing of the root level parameters for creation of the Formatter object, see console message(s) for details." raise RuntimeError(msg) return formatters[0]
def setup_environment(filename, root): """ Update environment from the [Environment] block. """ e_node = moosetree.find(root, func=lambda n: n.fullpath == '/Environment') if e_node is not None: for name, value in e_node.params(): if name not in os.environ: with mooseutils.CurrentWorkingDirectory( os.path.dirname(filename)): path = mooseutils.eval_path(value) if os.path.exists(path): value = os.path.abspath(path) os.environ[name] = value
def testException(self): home = os.getenv('HOME') self.assertNotEqual(home, os.getcwd(), "Test does not work from 'HOME' directory.") with self.assertRaises(Exception) as e: with mooseutils.CurrentWorkingDirectory(home) as cwd: self.assertEqual(os.getcwd(), home) self.assertEqual(cwd.internal, os.getcwd()) raise Exception('foo') self.assertNotEqual(home, os.getcwd()) self.assertEqual(cwd.external, os.getcwd()) self.assertIn('foo', str(e.exception))
def make_controllers(filename, root, plugin_dirs): """ Create the `Controller` object from the [Controllers] block of the `pyhit.Node` of *root*. The *filename* is provided for error reporting and setting the current working directory for creating object defined in the configuration file. It should be the file used for generating the tree structure in *root*. The *plugin_dirs* should contain a list of absolute paths to include when registering Controller objects with the factory. By default, regardless of the contents of *root*, all registered Controller objects are created. """ # Locate/create the [Controllers] node c_node = moosetree.find(root, func=lambda n: n.fullpath == '/Controllers') if c_node is None: c_node = root.append('Controllers') # Factory for building Controller objects c_factory = factory.Factory(plugin_dirs=plugin_dirs, plugin_types=(Controller, )) c_factory.load() if c_factory.status() > 0: msg = "An error occurred registering the Controller type, see console message(s) for details." raise RuntimeError(msg) # All Controller object type found by the Factory are automatically included with the default # configuration. This adds them to the configuration tree so they will be built by the factory c_types = set(child['type'] for child in c_node) for name in [ key for key, value in c_factory._registered_types.items() if value.AUTO_BUILD ]: if name not in c_types: c_node.append(f"_moosetools_{name}", type=name) # Use the Parser to create the Controller objects controllers = list() c_parser = factory.Parser(c_factory, controllers) with mooseutils.CurrentWorkingDirectory(os.path.dirname(filename)): c_parser.parse(filename, c_node) if c_parser.status() > 0: msg = "An error occurred during parsing of the Controller block, see console message(s) for details." raise RuntimeError(msg) return tuple(controllers)
def make_harness(filename, root, cli_args): """ Create the `TestHarness` object from top-level parameters in the `pyhit.Node` of *root*. The *filename* is provided for error reporting and should be the file used for generating the tree structure in *root*. The *cli_args* input is passed to the created `TestHarness` object via the `applyCommandLineArguments` method. It is expected that the file that produced *root* has top-level parameters, which are used to create a `TestHarness` object. """ # Top-level parameters are used to build the TestHarness object. Creating custom `TestHarness` # objects is not-supported, so don't allow "type" to be set. if 'type' in root: msg = "The 'type' parameter must NOT be defined in the top-level of the configuration." raise RuntimeError(msg) root['type'] = 'TestHarness' # Build a factory capable of creating the TestHarness object f = factory.Factory() f.register('TestHarness', TestHarness) if f.status() > 0: msg = "An error occurred during registration of the TestHarness type, see console message(s) for details." raise RuntimeError(msg) # Setup the environment variables setup_environment(filename, root) # Use the Parser is used to correctly convert HIT to InputParameters w = list() p = factory.Parser(f, w) with mooseutils.CurrentWorkingDirectory(os.path.dirname(filename)): p._parseNode(filename, root) if p.status() > 0: msg = "An error occurred during parsing of the root level parameters for creation of the TestHarness object, see console message(s) for details." raise RuntimeError(msg) # Apply the command line arguments to update TestHarness object parameters harness = w[0] harness.applyCommandLineArguments(cli_args) if harness.status() > 0: msg = "An error occurred applying the command line arguments to the TestHarness object, see console message(s) for details." raise RuntimeError(msg) return harness