def test_load_yaml_from_file_non_yaml_file(self):
   with open(self.yaml, 'w') as yaml_file:
     yaml_file.write("This is an invalid YAML file")
     with self.assertRaises(YamlError):
       config = load_yaml(self.env_var,
                          self.default_file,
                          self.yaml)
def initialise_harness_from_file(file_name = None):
  """Initialise interoperability test harness.

  This function creates an instance of
  :class:`prov_interop.harness.HarnessResources` and then configures
  it using configuration loaded from a YAML file (using
  :func:`prov_interop.files.load_yaml`). The file loaded is: 

  - `file_name` if this argument is provided (when called from within
    this module itself, no value is provided). 
  - Else, the file named in an environment variable with name
    ``PROV_HARNESS_CONFIGURATION``, if such an environment variable has
    been defined. 
  - Else, ``localconfig/harness.yaml``.

  The function will not reinitialise the
  :class:`prov_interop.harness.HarnessResources` instance once it has 
  been created and initialised. 

  A valid YAML configuration file, which, when loaded, yields a Python
  dictionary holding the configuration required by
  :class:`prov_interop.harness.HarnessResources` is::

    ---
    test-cases: /home/user/test-cases
    comparators:
      ProvPyComparator: 
        class: prov_interop.provpy.comparator.ProvPyComparator
        executable: prov-compare
        arguments: -f FORMAT1 -F FORMAT2 FILE1 FILE2
        formats: [provx, json]

  :param file_name: Configuration file name (optional)
  :type file_name: str or unicode
  :raises IOError: if the file is not found.
  :raises ConfigError: if the configuration in the file does not
    contain the configuration properties expected by
    :class:`prov_interop.harness.HarnessResources`
  :raises YamlError: if the file is an invalid YAML file
  """
  global harness_resources
  global CONFIGURATION_FILE_ENV
  global DEFAULT_CONFIGURATION_FILE
  if harness_resources is None:
    harness_resources = HarnessResources()
    config = load_yaml(CONFIGURATION_FILE_ENV,
                       DEFAULT_CONFIGURATION_FILE, 
                       file_name)
    harness_resources.configure(config)
    print("Comparators available:")
    for format in harness_resources.format_comparators:
      print((" " + format + ":" + 
            harness_resources.format_comparators[format].__class__.__name__))
    print("Test cases directory:")
    print((harness_resources.test_cases_dir))
    print("Test cases available:")
    num_test_cases = 0
    for (index, format1, _, format2, _) in harness_resources.test_cases_generator():
      num_test_cases += 1
      print((str(index) + ":" + format1 + "->" + format2))
    print("Total: " + str(num_test_cases))
  def configure(self, config_key, env_var, default_file_name):
    """Get the configuration for the converter to be tested within a
    sub-class. 

    The method assumes the converter has been created and stored in an
    instance variable. It loads the contents of a YAML file (using
    :func:`prov_interop.files.load_yaml`) into a Python
    dictionary. The file loaded is: 

    - The value of an entry in
      :class:`prov_interop.harness.HarnessResources` configuration with
      name `config_key`, if any. 
    - Else, the file named in the environment variable named in
      `env_var`, if such an environment variable has been defined. 
    - Else, `default_file_name`.

    Once loaded, a dictionary entry with whose key is the value of
    `config_key` is extracted and used to configure the converter via
    its :meth:`prov_interop.converter.Converter.configure` method. 

    In addition to converter-specific configuration, this
    configuration can also hold:

    - ``skip-tests``: a list of the indices of zero or more tests that
      are to be skipped for this converter. 

    If so, then this list is cached in an instance variable.

    An example configuration, in the form of a Python dictionary, and
    for ProvPy ``prov-convert``, is::

      {
        "ProvPy": {
          "executable": "prov-convert"
          "arguments": "-f FORMAT INPUT OUTPUT"
          "input-formats": ["json"]
          "output-formats": ["provn", "provx", "json"]
          skip-tests: [2, 3, 5]
        }
      }

    The corresponding YAML configuration file is::

      ---
      ProvPy: 
        executable: prov-convert
        arguments: -f FORMAT INPUT OUTPUT
        input-formats: [json]
        output-formats: [provn, provx, json]
        skip-tests: [2, 3, 5]

    :param config_key: Key to access converter-specific configuration
    :type config_key: str or unicode
    :param env_var: Environment variable with configuration file name
    :type env_var: str or unicode
    :param default_file_name: Default configuration file name
    :type file_name: str or unicode
    :raises IOError: if the file is not found
    :raises ConfigError: if there is no entry with value `config_key`
      within the configuration, or if converter-specific
      configuration information is missing
    :raises YamlError: if the file is an invalid YAML file
    """
    config_file_name = None
    if config_key in harness.harness_resources.configuration:
      config_file_name = harness.harness_resources.configuration[config_key]
    config = load_yaml(env_var,
                       default_file_name,
                       config_file_name)
    if config_key not in config:
      raise ConfigError("Missing configuration for " + config_key)
    self.converter.configure(config[config_key])
    if ConverterTestCase.SKIP_TESTS in self.converter.configuration:
      self.skip_tests = self.converter.configuration[
        ConverterTestCase.SKIP_TESTS]
 def test_load_yaml_from_file_missing_file(self):
   with self.assertRaises(IOError):
     config = load_yaml(self.env_var,
                        self.default_file,
                        "nosuchfile.yaml")
 def test_load_yaml_from_default(self):
   shutil.move(self.yaml, self.default_file)
   self.yaml = self.default_file
   config = load_yaml(self.env_var,
                      self.default_file)
   self.assertEqual(12345, config["counter"])
 def test_load_yaml_from_env(self):
   os.environ[self.env_var] = self.yaml
   config = load_yaml(self.env_var,
                      self.default_file,
                      self.yaml)
   self.assertEqual(12345, config["counter"])
 def test_load_yaml_from_file(self):
   config = load_yaml(self.env_var,
                      self.default_file,
                      self.yaml)
   self.assertEqual(12345, config["counter"])