from __future__ import absolute_import
import unittest

import os
import tempfile
from six import StringIO
from cwltool.main import main
import shutil
import pytest
from cwltool.utils import onWindows

from .util import get_data
import bagit

@pytest.mark.skipif(onWindows(),
                    reason="On Windows this would invoke a default docker container")
class TestProvenance(unittest.TestCase):
    folder = None

    def setUp(self):
        self.folder = tempfile.mkdtemp("ro")
        if os.environ.get("DEBUG"):
            print("%s folder: %s" % (this, self.folder))

    def tearDown(self):
        if self.folder and not os.environ.get("DEBUG"):
            shutil.rmtree(self.folder)

    def test_hello_workflow(self):
        self.assertEquals(main(['--provenance', self.folder, get_data('tests/wf/hello-workflow.cwl'),
            "--usermessage", "Hello workflow"]), 0)
        self.check_ro()

    def test_hello_single_tool(self):
        self.assertEquals(main(['--provenance', self.folder, get_data('tests/wf/hello_single_tool.cwl'), 
            "--message", "Hello tool"]), 0)
        self.check_ro()            

    def test_revsort_workflow(self):
        self.assertEquals(main(['--no-container', '--provenance', self.folder, get_data('tests/wf/revsort.cwl'),
            get_data('tests/wf/revsort-job.json')]), 0)
        self.check_ro()            

    def check_ro(self):
        # Our folders
        for d in ("data", "snapshot", "workflow", "metadata", os.path.join("metadata", "provenance")):
            f = os.path.join(self.folder, d)
            self.assertTrue(os.path.isdir(f))
        
        # check bagit structure
        for f in ("bagit.txt", "bag-info.txt", "manifest-sha1.txt", "tagmanifest-sha1.txt", "tagmanifest-sha256.txt"):
            f = os.path.join(self.folder, f)
            self.assertTrue(os.path.isfile(f))
        bag = bagit.Bag(self.folder)
        self.assertTrue(bag.has_oxum())        
        (only_manifest, only_fs) = bag.compare_manifests_with_fs()
        self.assertFalse(list(only_manifest), "Some files only in manifest")
        self.assertFalse(list(only_fs), "Some files only on file system")
        missing_tagfiles = bag.missing_optional_tagfiles()
        self.assertFalse(list(missing_tagfiles), "Some files only in tagmanifest")
        bag.validate()


@@ -195,16 +195,70 @@
 indows%0A%0A
+# Module to be tested%0Afrom cwltool import provenance%0A%0A
 from .ut
@@ -288,16 +288,47 @@
 rt bagit
+%0Aimport posixpath%0Aimport ntpath
 %0A%0A@pytes
@@ -2419,12 +2419,1226 @@
 validate()%0A%0A
+class TestConvertPath(unittest.TestCase):%0A%0A    def nt_to_posix(self):%0A        self.assertEquals(%22a/b/c%22, %0A            provenance._convert_path(r%22a%5Cb%5Cc%22, ntpath, posixpath))%0A    %0A    def posix_to_nt(self):            %0A        self.assertEquals(r%22a%5Cb%5Cc%22, %0A            provenance._convert_path(%22a/b/c%22, posixpath, ntpath))%0A%0A    def posix_to_posix(self):            %0A        self.assertEquals(%22a/b/c%22,%0A            provenance._convert_path(%22a/b/c%22, posixpath, posixpath))%0A%0A    def nt_to_nt(self):%0A        self.assertEquals(r%22a%5Cb%5Cc%22, %0A            provenance._convert_path(r%22a%5Cb%5Cc%22, ntpath, ntpath))%0A%0A    def posix_to_nt_absolute_fails(self):            %0A        with self.assertRaises(ValueError):        %0A            provenance._convert_path(%22/absolute/path%22, posixpath, ntpath)%0A    %0A    def nt_to_posix_absolute_fails(self):            %0A        with self.assertRaises(ValueError):        %0A            provenance._convert_path(r%22D:%5Cabsolute%5Cpath%22, ntpath, posixpath)%0A%0Aclass TestWritableBagFile(unittest.TestCase):%0A    def setUp(self):%0A        self.ro = provenance.ResearchObject()%0A%0A    def absolute_path_fails(self):%0A        with self.assertRaises(ValueError):%0A            self.ro.write_bag_file(%22/absolute/path/fails%22)

test _convert_path