import os
import shutil
import tempfile
import unittest

from lobster.cmssw import dataset
from lobster import fs, se

class TestDataset(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        path = os.path.expandvars(
                os.environ.get('LOBSTER_STORAGE', '/hadoop/store/user/') +
                os.environ.get('LOBSTER_USER', os.environ['USER']) + '/')
        if not os.path.exists(path):
            os.makedirs(path)
        cls.workdir = tempfile.mkdtemp(prefix=path)
        os.chmod(cls.workdir, 0777)
        os.makedirs(os.path.join(cls.workdir, 'eggs'))
        for i in range(10):
            with open(os.path.join(cls.workdir, 'eggs', str(i) + '.txt'), 'w') as f:
                f.write('stir-fry')
        os.makedirs(os.path.join(cls.workdir, 'ham'))
        for i in range(5):
            with open(os.path.join(cls.workdir, 'ham', str(i) + '.txt'), 'w') as f:
                f.write('bacon')

    @classmethod
    def tearDownClass(cls):
        shutil.rmtree(cls.workdir)

    def runTest(self):
        s = se.StorageConfiguration({'input': ['file://' + self.workdir]})
        s.activate()

        with fs.default():
            info = dataset.MetaInterface().get_info({'label': 'ham', 'files': 'eggs/'})
            assert len(info.files) == 10

            info = dataset.MetaInterface().get_info({'label': 'ham', 'files': ['eggs/', 'ham/']})
            assert len(info.files) == 15

            info = dataset.MetaInterface().get_info({'label': 'ham', 'files': 'eggs/1.txt'})
            assert len(info.files) == 1

@@ -68,20 +68,19 @@
 er.c
-mssw
+ore
  import 
 data
@@ -75,17 +75,17 @@
  import 
-d
+D
 ataset%0Af
@@ -1077,18 +1077,25 @@
 ion(
-%7B'
+output=%5B%5D, 
 input
-': 
+=
 %5B'fi
@@ -1116,17 +1116,16 @@
 workdir%5D
-%7D
 )%0A      
@@ -1190,75 +1190,39 @@
 o = 
-d
+D
 ataset
-.MetaInterface().get_info(%7B'label': 'ham', 'files': 'eggs/'%7D
+(files='eggs').get_info(
 )%0A  
@@ -1284,85 +1284,48 @@
 o = 
-d
+D
 ataset
-.MetaInterface().get_info(%7B'label': 'ham', '
+(
 files
-': 
+=
 %5B'eggs
-/
 ', 'ham
-/
 '%5D
-%7D
+).get_info(
 )%0A  
@@ -1387,67 +1387,22 @@
 o = 
-d
+D
 ataset
-.MetaInterface().get_info(%7B'label': 'ham', '
+(
 files
-': 
+=
 'egg
@@ -1413,9 +1413,19 @@
 txt'
-%7D
+).get_info(
 )%0A  

Fix tests.