import os
import subprocess
import shutil
import textwrap

from boto.s3.connection import S3Connection, Bucket, Key


def test_rnaseq_cgl(tmpdir):
    work_dir = str(tmpdir)
    create_config_and_manifest(work_dir)
    subdir = '/mnt/ephemeral/toil-scripts/rnaseq/'
    os.makedirs(os.path.join(subdir, 'workDir'))
    sample = 's3://cgl-pipeline-inputs/rnaseq_cgl/ci/chr6_sample.tar.gz'
    # Call Pipeline
    try:
        base_command = ['toil-rnaseq', 'run',
                        '--config', os.path.join(work_dir, 'toil-rnaseq.config'),
                        os.path.join(subdir, 'jstore'),
                        '--retryCount', '1',
                        '--workDir', os.path.join(subdir, 'workDir')]
        # Run with --samples
        subprocess.check_call(base_command + ['--samples', sample])
        # Run with manifest
        subprocess.check_call(base_command + ['--manifest', os.path.join(work_dir, 'toil-rnaseq-manifest.tsv')])
    finally:
        shutil.rmtree(subdir)
        conn = S3Connection()
        b = Bucket(conn, 'cgl-driver-projects')
        k = Key(b)
        k.key = 'test/ci/chr6_sample.tar.gz'
        k.delete()


def create_config_and_manifest(path):
    """Creates config file for test at path"""
    config_path = os.path.join(path, 'toil-rnaseq.config')
    manifest_path = os.path.join(path, 'toil-rnaseq-manifest.tsv')
    with open(config_path, 'w') as f:
        f.write(generate_config())
    with open(manifest_path, 'w') as f:
        f.write(generate_manifest())


def generate_config():
    return textwrap.dedent("""
        star-index: s3://cgl-pipeline-inputs/rnaseq_cgl/ci/starIndex_chr6.tar.gz
        kallisto-index: s3://cgl-pipeline-inputs/rnaseq_cgl/kallisto_hg38.idx
        rsem-ref: s3://cgl-pipeline-inputs/rnaseq_cgl/ci/rsem_ref_chr6.tar.gz
        output-dir:
        s3-output-dir: s3://cgl-driver-projects/test/ci
        fastqc: true
        cutadapt:
        ssec:
        gtkey:
        wiggle:
        save-bam:
        fwd-3pr-adapter: AGATCGGAAGAG
        rev-3pr-adapter: AGATCGGAAGAG
        ci-test: true
    """[1:])


def generate_manifest():
    return textwrap.dedent("""
        tar\tpaired\tchr6_sample\ts3://cgl-pipeline-inputs/rnaseq_cgl/ci/chr6_sample.tar.gz
        """[1:])

@@ -50,16 +50,39 @@
 textwrap
+%0Afrom uuid import uuid4
 %0A%0Afrom b
@@ -167,20 +167,19 @@
 r):%0A    
-work
+tmp
 _dir = s
@@ -220,20 +220,19 @@
 anifest(
-work
+tmp
 _dir)%0A  
@@ -288,20 +288,19 @@
     
-os.makedirs(
+work_dir = 
 os.p
@@ -316,26 +316,95 @@
 subdir, 
-'workDir')
+str(uuid4()))%0A    if not os.path.exists(work_dir):%0A        os.makedirs(work_dir
 )%0A    sa
@@ -586,36 +586,35 @@
 ', os.path.join(
-work
+tmp
 _dir, 'toil-rnas
@@ -659,27 +659,29 @@
 s.path.join(
-sub
+work_
 dir, 'jstore
@@ -770,39 +770,16 @@
 r', 
-os.path.join(subdir, 'workDir')
+work_dir
 %5D%0A  
@@ -970,28 +970,27 @@
 s.path.join(
-work
+tmp
 _dir, 'toil-

Fix rnaseq unittest (resolves #313)