import pytest

import os
import glob
import yaml
import subprocess

project_root_path = os.path.realpath(os.path.join(__file__, '..', '..'))
script_path = os.path.join(project_root_path, 'order-confirmation-mail-parser')
test_data_path = os.path.join(project_root_path, 'tests', 'data')

def test_integration():
    for mail_path in glob.glob(os.path.join(test_data_path, '*', 'mail_*.eml')):
        with open(mail_path, 'r') as mail_file:
            process = subprocess.Popen([script_path], stdin = subprocess.PIPE, stdout = subprocess.PIPE)
            script_stdout, script_stderr = process.communicate(input = mail_file.read())
            result = yaml.load(script_stdout)
            with open(mail_path.replace('.eml', '.yml'), 'r') as expected_result_file:
                expected_result = yaml.load(expected_result_file.read())
                assert result == expected_result, 'unexpected result for ' + os.path.basename(mail_path)