import json


def artifact_file(tmpdir, artifact_content, artifact_filename,
                  artifact_file_created):
    """
    This fixture creates a tempdir and writes the artifact_filename file
    with the artifact_content passed in via the fixture. Then it returns the
    path of the file. If artifact_content is a dict it will do a json dump,
    otherwise if it is just a string we'll write that directly.
    """
    f = tmpdir.join(artifact_filename)
    if artifact_file_created:
        # Only write the artifact if we want to actually create it.
        if isinstance(artifact_content, dict):
            artifact_content = json.dumps(artifact_content, indent=4)
        f.write(artifact_content)
    return f.strpath