import os
import pytest
import boto3


from gypsy.scripts import DEFAULT_CONF_FILE
from gypsy.utils import _copy_file


DATA_DIR = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data')
BUCKET = os.getenv('GYPSY_BUCKET')


@pytest.yield_fixture(scope='module')
def invalid_cli_config_file():
    path = os.path.join(DATA_DIR, 'invalid_cli_config.txt')

    with open(path, 'w') as f:
        f.write('\n\n\n\nnodata\n')

    yield path

    os.remove(path)

@pytest.fixture(scope='session')
def s3_bucket_path():
    return 's3://' + BUCKET

@pytest.fixture(scope='session')
def s3_bucket_conn():
    s3 = boto3.resource('s3')
    bucket_conn = s3.Bucket(BUCKET)

    return bucket_conn

@pytest.yield_fixture(scope='function')
def s3_config_output_dir(s3_bucket_path, s3_bucket_conn):
    prefix = 'test/generate-config/gypsy-output'
    out_dir = '%s/%s' % (s3_bucket_path, prefix)

    yield out_dir

    for key in s3_bucket_conn.objects.filter(Prefix=prefix):
        key.delete()

@pytest.yield_fixture(scope='function')
def s3_prep_output_dir(s3_bucket_path, s3_bucket_conn):
    prefix = 'test/prep/gypsy-output'
    files = [
        (os.path.join(DATA_DIR, 'raw_standtable.csv'),
         '/'.join([prefix, 'raw-data.csv'])),
        (DEFAULT_CONF_FILE,
         '/'.join([prefix, 'config.json'])),
    ]
    for item in files:
        _copy_file(item[0], item[1], bucket_conn=s3_bucket_conn)

    out_dir = '%s/%s' % (s3_bucket_path, prefix)
    data_path = '/'.join([s3_bucket_path, files[0][1]])

    yield {
        'out-dir': out_dir,
        'data-path': data_path
    }

    for key in s3_bucket_conn.objects.filter(Prefix=prefix):
        key.delete()


@pytest.yield_fixture(scope='function')
def s3_simulate_output_dir(s3_for_prep, s3_bucket_path, s3_bucket_conn):
    # TODO: copy data to s3
    prefix = 'test/simulate/gypsy-output'
    out_dir = '%s/%s' % (s3_bucket_path, prefix)

    yield out_dir

    for key in s3_bucket_conn.objects.filter(Prefix=prefix):
        key.delete()

@@ -560,36 +560,37 @@
 .fixture(scope='
-sess
+funct
 ion')%0Adef s3_buc
@@ -1121,24 +1121,73 @@
 psy-output'%0A
+    out_dir = '%25s/%25s' %25 (s3_bucket_path, prefix)%0A
     files = 
@@ -1368,16 +1368,17 @@
 ,%0A    %5D%0A
+%0A
     for 
@@ -1460,58 +1460,8 @@
 onn)
-%0A%0A    out_dir = '%25s/%25s' %25 (s3_bucket_path, prefix)
 %0A   
@@ -1747,21 +1747,8 @@
 dir(
-s3_for_prep, 
 s3_b
@@ -1895,34 +1895,550 @@
 prefix)%0A
-%0A    yield out_dir
+    files = %5B%0A        (os.path.join(DATA_DIR, 'raw_standtable_prepped.csv'),%0A         '/'.join(%5Bprefix, 'raw-data-prepped.csv'%5D)),%0A        (os.path.join(DATA_DIR, 'raw_standtable.csv'),%0A         '/'.join(%5Bprefix, 'raw-data.csv'%5D)),%0A        (DEFAULT_CONF_FILE,%0A         '/'.join(%5Bprefix, 'config.json'%5D)),%0A    %5D%0A%0A    for item in files:%0A        _copy_file(item%5B0%5D, item%5B1%5D, bucket_conn=s3_bucket_conn)%0A    data_path = '/'.join(%5Bs3_bucket_path, files%5B0%5D%5B1%5D%5D)%0A%0A    yield %7B%0A        'out-dir': out_dir,%0A        'data-path': data_path,%0A    %7D
 %0A%0A    fo

Add fixture for s3 simulation test