from __future__ import with_statement
import sys, os, unittest, tempfile, shutil
from copy import copy

DATA1 = "tjosan"
DATA1_MD5 = "5558e0551622725a5fa380caffa94c5d"
DATA2 = "tjosan hejsan"
DATA2_MD5 = "923574a1a36aebc7e1f586b7d363005e"

TMPDIR="/tmp"

if __name__ == '__main__':
    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

import workdir
from blobrepo import repository

class TestWorkdir(unittest.TestCase):
    def setUp(self):
        self.workdir = tempfile.mktemp(prefix='workdir_', dir=TMPDIR)
        self.repopath = tempfile.mktemp(prefix='workdir_repo_', dir=TMPDIR)
        repository.create_repository(self.repopath)
        os.mkdir(self.workdir)
        self.wd = workdir.Workdir(self.repopath, "TestSession", None, self.workdir)
        id = self.wd.checkin()
        assert id == 1

    def tearDown(self):
        shutil.rmtree(self.workdir, ignore_errors = True)
        shutil.rmtree(self.repopath, ignore_errors = True)

    def addWorkdirFile(self, path, content):
        filepath = os.path.join(self.workdir, path)
        with open(filepath, "w") as f:
            f.write(content)
    
    def rmWorkdirFile(self, path):
        filepath = os.path.join(self.workdir, path)
        os.unlink(filepath)

    #
    # Actual tests start here
    #

    def testEmpty(self):
        changes = self.wd.get_changes()
        self.assertEqual(changes, ([], [], [], [], []))

    def testGetChangesUnversionedFile(self):
        # Test unversioned file
        self.addWorkdirFile("tjosan.txt", "tjosanhejsan")
        changes = self.wd.get_changes()
        self.assertEqual(changes, ([], ["tjosan.txt"], [], [], []))

    def testGetChangesUnchangedFile(self):        
        self.addWorkdirFile("tjosan.txt", "tjosanhejsan")
        self.wd.checkin()
        changes = self.wd.get_changes()
        self.assertEqual(changes, (["tjosan.txt"], [], [], [], []))

    def testGetChangesMissingFile(self):
        self.addWorkdirFile("tjosan.txt", "tjosanhejsan")
        self.wd.checkin()
        self.rmWorkdirFile("tjosan.txt")
        changes = self.wd.get_changes()
        self.assertEqual(changes, ([], [], [], ["tjosan.txt"], []))


if __name__ == '__main__':
    unittest.main()


@@ -418,568 +418,167 @@
 ass 
-Test
 Workdir
-(unittest.TestCase):%0A    def setUp(self):%0A        self.workdir = tempfile.mktemp(prefix='workdir_', dir=TMPDIR)%0A        self.repopath = tempfile.mktemp(prefix='workdir_repo_', dir=TMPDIR)%0A        repository.create_repository(self.repopath)%0A        os.mkdir(self.workdir)%0A        self.wd = workdir.Workdir(self.repopath, %22TestSession%22, None, self.workdir)%0A        id = self.wd.checkin()%0A        assert id == 1%0A%0A    def tearDown(self):%0A        shutil.rmtree(self.workdir, ignore_errors = True)%0A        shutil.rmtree(self.repopath, ignore_errors = True
+Helper:%0A    def mkdir(self, path):%0A        assert not os.path.isabs(path)%0A        dirpath = os.path.join(self.workdir, path)%0A        os.makedirs(dirpath
 )%0A%0A 
@@ -621,16 +621,55 @@
 ntent):%0A
+        assert not os.path.isabs(path)%0A
         
@@ -812,32 +812,71 @@
 le(self, path):%0A
+        assert not os.path.isabs(path)%0A
         filepath
@@ -936,24 +936,741 @@
 (filepath)%0A%0A
+    def createTmpName(self):%0A        filename = tempfile.mktemp(prefix='workdir_repo_', dir=TMPDIR)%0A        self.remove_at_teardown.append(filename)%0A        return filename%0A%0A%0A%0A%0Aclass TestWorkdir(unittest.TestCase, WorkdirHelper):%0A    def setUp(self):%0A        self.remove_at_teardown = %5B%5D%0A        self.workdir = self.createTmpName()%0A        self.repopath = self.createTmpName()%0A        repository.create_repository(self.repopath)%0A        os.mkdir(self.workdir)%0A        self.wd = workdir.Workdir(self.repopath, %22TestSession%22, None, self.workdir)%0A        id = self.wd.checkin()%0A        assert id == 1%0A%0A    def tearDown(self):%0A        for d in self.remove_at_teardown:%0A            shutil.rmtree(d, ignore_errors = True)%0A%0A
     #%0A    # 
@@ -2586,16 +2586,768 @@
  %5B%5D))%0A%0A%0A
+class TestPartialCheckin(unittest.TestCase, WorkdirHelper):%0A    def setUp(self):%0A        self.remove_at_teardown = %5B%5D%0A        self.workdir = self.createTmpName()%0A        self.repopath = self.createTmpName()%0A        repository.create_repository(self.repopath)%0A        os.mkdir(self.workdir)%0A        self.wd = workdir.Workdir(self.repopath, %22TestSession%22, None, self.workdir)%0A        id = self.wd.checkin()%0A        assert id == 1%0A        self.addWorkdirFile(%22onlyintopdir.txt%22, %22nothing%22)%0A        self.mkdir(%22mysubdir%22)%0A        self.addWorkdirFile(%22mysubdir/insubdir.txt%22, %22nothing2%22)%0A%0A    def tearDown(self):%0A        for d in self.remove_at_teardown:%0A            shutil.rmtree(d, ignore_errors = True)%0A%0A%0A    def testPartialCheckout(self):%0A        pass%0A%0A
 if __nam

work in progress