from aegis.Core import Plotter
import pytest, os, shutil, warnings
from subprocess import call
import random, string

@pytest.fixture(scope="module")
def rec_names(request):
    """ Get record filenames generated by TestRun::test_execute."""
    listing = os.listdir(os.path.join(os.path.abspath("."),"aegis/Core/Test"))
    rec_names = []
    for filename in listing:
        if ".rec" in filename:
            rec_names.append(filename)
    return rec_names

def test_plot(rec_names):
    """Test that 'aegis plot' executes without error."""
    if not rec_names:
        s = "No records found. This tests depends on previous execution of test_4_Run::TestRun::test_execute that generates the record files needed for this test. Plotter test not run."
        warnings.warn(UserWarning(s))
    dirname = ''.join([random.choice(string.ascii_letters + string.digits) for i in xrange(10)])
    outpath = os.path.join(os.path.abspath("."), "aegis/Core/Test", dirname)
    os.makedirs(outpath)
    for rec in rec_names:
        rec_path = os.path.join(os.path.abspath("."), "aegis/Core/Test", rec)
        call(["aegis","read","--csv",rec_path,"-o",outpath])
        a = Plotter(os.path.join(outpath,"test_csv_files"), outpath=outpath)
        a.generate_figures()
        a.save_figures()
    shutil.rmtree(outpath)
