import json


def dump_manifest_to_json(manifest, test_name, test_dir, create_files=True):
    """
    Create a JSON manifest file from a dictionary.

    :param manifest: The manifest dictionary.
    :param test_name: Name of the test.
    :param test_dir: Directory to contain the JSON file.
    :param create_files: Whether to create the source files listed in the
           manifest 'source_files' entry.
    :return: Path of the JSON file.
    """
    test_file_name = test_dir.join('{}.json'.format(test_name))
    with open(test_file_name.strpath, 'wt') as fh:
        json.dump(manifest, fh, indent=2)

    # Create all the partition source files
    if create_files:
        [test_dir.join(name).write(name) for name in
         manifest.get('source_files', [])]

    return test_file_name.strpath