#!/usr/bin/env python3

# Tests check_format.py. This must be run in a context where the clang
# version and settings are compatible with the one in the Envoy
# docker. Normally this is run via check_format_test.sh, which
# executes it in under docker.

from __future__ import print_function

from run_command import run_command
import argparse
import logging
import os
import shutil
import sys
import tempfile

curr_dir = os.path.dirname(os.path.realpath(__file__))
tools = os.path.dirname(curr_dir)
src = os.path.join(tools, 'testdata', 'check_format')
check_format = sys.executable + " " + os.path.join(curr_dir, 'check_format.py')
errors = 0


# Runs the 'check_format' operation, on the specified file, printing
# the comamnd run and the status code as well as the stdout, and returning
# all of that to the caller.
def run_check_format(operation, filename):
    command = check_format + " " + operation + " " + filename
    status, stdout, stderr = run_command(command)
    return (command, status, stdout + stderr)


def get_input_file(filename, extra_input_files=None):
    files_to_copy = [filename]
    if extra_input_files is not None:
        files_to_copy.extend(extra_input_files)
    for f in files_to_copy:
        infile = os.path.join(src, f)
        directory = os.path.dirname(f)
        if not directory == '' and not os.path.isdir(directory):
            os.makedirs(directory)
        shutil.copyfile(infile, f)
    return filename