import os
from unittest import TestCase
from tempfile import TemporaryDirectory

from triptan.core import Triptan


class TriptanInitializationTest(TestCase):
    """
        Asserts that triptan can setup the necessary data correctly.
    """

    def test_init_file_structure(self):
        """
            Assert the file structure is created correctly.
        """
        with TemporaryDirectory() as tmpd:
            Triptan.setup(
                tmpd,
                'triptan.yml',
                {'revisions_location': 'revisions'}
            )
            assert os.path.exists(os.path.join(tmpd, 'triptan.yml'))
            assert os.path.exists(os.path.join(tmpd, 'revisions'))


class TriptanTest(TestCase):
    """
        Assert the core functionality is working.
    """

    def setUp(self):
        """
            Create a temporary directory and set triptan up with it.
        """
        self.path = TemporaryDirectory()
        Triptan.setup(
            self.path.name,
            'triptan.yml',
            {'revisions_location': 'revisions'}
        )
        self.triptan = Triptan(self.path.name, 'triptan.yml')

    def test_default_revision(self):
        """
            Assert the default revision is -1.
        """
        assert self.triptan.current_revision == -1

    def test_revision_creation(self):
        """
            Assert that revisions are correctly created.
        """
        self.triptan.new_revision("test revision")
        rev_path = os.path.join(self.path.name, 'revisions/revision-000.py')
        assert os.path.exists(rev_path)

        self.triptan.new_revision("another")
        rev_path = os.path.join(self.path.name, 'revisions/revision-001.py')
        assert os.path.exists(rev_path)

@@ -54,34 +54,49 @@
  import 
-TemporaryDirectory
+mkdtemp%0Afrom shutil import rmtree
 %0A%0Afrom t
@@ -124,16 +124,158 @@
 iptan%0A%0A%0A
+class TriptanBaseTestCase(TestCase):%0A%0A    def setUp(self):%0A        self.path = mkdtemp()%0A%0A    def tearDown(self):%0A        rmtree(self.path)%0A%0A%0A
 class Tr
@@ -294,24 +294,35 @@
 izationTest(
+TriptanBase
 TestCase):%0A 
@@ -534,55 +534,8 @@
 %22%22%22%0A
-        with TemporaryDirectory() as tmpd:%0A    
     
@@ -569,22 +569,19 @@
     
-    tmpd,%0A    
+self.path,%0A
     
@@ -595,36 +595,32 @@
  'triptan.yml',%0A
-    
             %7B're
@@ -655,34 +655,26 @@
 s'%7D%0A        
-   
- 
 )%0A
-    
         asse
@@ -704,20 +704,25 @@
 th.join(
-tmpd
+self.path
 , 'tript
@@ -731,20 +731,16 @@
 .yml'))%0A
-    
         
@@ -778,12 +778,17 @@
 oin(
-tmpd
+self.path
 , 'r
@@ -819,16 +819,27 @@
 tanTest(
+TriptanBase
 TestCase
@@ -1035,37 +1035,37 @@
    s
-elf.path = TemporaryDirectory
+uper(TriptanTest, self).setUp
 ()%0A 
@@ -1107,21 +1107,16 @@
 elf.path
-.name
 ,%0A      
@@ -1234,21 +1234,16 @@
 elf.path
-.name
 , 'tript
@@ -1615,37 +1615,32 @@
 h.join(self.path
-.name
 , 'revisions/rev
@@ -1785,13 +1785,8 @@
 path
-.name
 , 'r

make tests compatible with py2.7