Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJCTutorius <charlie@tutorius-dev.(none)>2009-10-17 19:56:35 (GMT)
committer JCTutorius <charlie@tutorius-dev.(none)>2009-10-17 19:56:35 (GMT)
commit3c39e41102b2d0167cc50b15002c4d05c25c42f8 (patch)
tree926c56ea85fbecd04815e4014219af17cf8cfe3f
parent4128281863fff3b86c4b06f04b13af75bfbe1e67 (diff)
Tested save tutorial function, working with test in vaulttest.py
-rw-r--r--tests/vaulttests.py184
-rw-r--r--tutorius/vault.py15
2 files changed, 118 insertions, 81 deletions
diff --git a/tests/vaulttests.py b/tests/vaulttests.py
index 1a53f69..c6f4534 100644
--- a/tests/vaulttests.py
+++ b/tests/vaulttests.py
@@ -28,9 +28,10 @@ import unittest
import os
import shutil
+import zipfile
# from mock import Mock
-from sugar.tutorius import bundler, addon
+from sugar.tutorius import addon
from sugar.tutorius.core import State, FiniteStateMachine
from sugar.tutorius.actions import *
from sugar.tutorius.filters import *
@@ -45,23 +46,10 @@ class VaultInterfaceTest(unittest.TestCase):
Test the high-level interfaces functions of the Vault
"""
- def setUp(self):
- os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
- path = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
- if os.path.isdir(path) != True:
- os.makedirs(path)
-
- # Generate a first test GUID
- self.test_guid = uuid1()
- self.guid_path = os.path.join(bundler._get_store_root(),str(self.test_guid))
- os.mkdir(self.guid_path)
-
- # Create a first dummy .ini file
- self.ini_file_path = os.path.join(self.guid_path, "meta.ini")
-
- ini_file = open(self.ini_file_path, 'wt')
+ def create_test_metadata_file(self, ini_file_path, guid):
+ ini_file = open(ini_file_path, 'wt')
ini_file.write("[GENERAL_METADATA]\n")
- ini_file.write('guid=' + str(self.test_guid) + '\n')
+ ini_file.write('guid=' + str(guid) + '\n')
ini_file.write('name=TestTutorial1\n')
ini_file.write('version=1\n')
ini_file.write('description=This is a test tutorial 1\n')
@@ -73,10 +61,25 @@ class VaultInterfaceTest(unittest.TestCase):
ini_file.write('org.laptop.TutoriusActivity = 1\n')
ini_file.write('org.laptop.Writus = 1\n')
ini_file.close()
+
+ def setUp(self):
+ os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
+ path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
+ if os.path.isdir(path) != True:
+ os.makedirs(path)
+
+ # Generate a first test GUID
+ self.test_guid = uuid1()
+ self.guid_path = os.path.join(sugar.tutorius.vault._get_store_root(),str(self.test_guid))
+ os.mkdir(self.guid_path)
+
+ # Create a first dummy .ini file
+ self.ini_file_path = os.path.join(self.guid_path, "meta.ini")
+ self.create_test_metadata_file(self.ini_file_path, self.test_guid)
# Generate a second test GUID
self.test_guid2 = uuid1()
- self.guid_path2 = os.path.join(bundler._get_store_root(),str(self.test_guid2))
+ self.guid_path2 = os.path.join(sugar.tutorius.vault._get_store_root(),str(self.test_guid2))
os.mkdir(self.guid_path2)
# Create a second dummy .ini file
@@ -97,53 +100,66 @@ class VaultInterfaceTest(unittest.TestCase):
ini_file2.write('org.laptop.Writus = 1\n')
ini_file2.write('org.laptop.Testus = 1\n')
ini_file2.close()
+
+ def test_installTutorials(self):
+ # create dummy tutorial
+ # create a test folder in the file
+ # system outside the Vault
+ test_path = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp', 'testdata')
+ if os.path.isdir(test_path) == True:
+ shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp'))
+ os.makedirs(test_path)
- def tearDown(self):
- shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'test'))
-
-## def test_installTutorials(self):
-##
-## # create dummy tutorial
-## serializer = XMLSerializerTest()
-## serializer.setUp()
-##
-## # create a test folder in the file
-## # system outside the Vault
-## test_path = "/tmp/testdata/"
-## os.makedirs(test_path)
-##
-## # Creat a dummy tutorial .xml file
-## serializer.save_fsm(serializer.fsm, 'TestTutorial.xml', test_path)
-##
-## # Create a dummy tutorial metadata file
-## create_test_metadata_file(test_path + 'Test_Tutorial.ini', serializer.uuid)
-##
-## #Zip these tutorials files in the pkzip file format
-## archive_list = [test_path + 'Test_Tutorial.ini', test_path + 'TestTutorial.xml']
-##
-## zfilename = "TestTutorial.zip"
-##
-## zout = zipfile.ZipFile(zfilename, "w")
-## for fname in archive_list:
-## zout.write(fname)
-## zout.close()
-##
-## # test if the file is a valid pkzip file
-## if zipfile.is_zipfile(zfilename) != True:
-## fail("The zipping of the tutorial files failed.")
-##
-## # test installTutorials function
-## vault = Vault.Vault()
-##
-## install_return = vault.installTutorials(test_path, 'TestTutorial.zip', False)
-## assert install_return != 2, "Tutorial install has returned an error"
-##
-## # check if the tutorial is now in the vault
-## try :
-## serializer.find_tutorial_dir_with_guid(serializer.uuid)
-## except IOError:
-## fail("Cannot find the specified tutorial's GUID in the vault")
+ # Creat a dummy tutorial .xml file
+ serializer = XMLSerializer()
+
+ # Create the sample FSM
+ fsm = FiniteStateMachine("testingMachine")
+ # Add a few states
+ act1 = addon.create('BubbleMessage', message="Hi", pos=[300, 450])
+ ev1 = addon.create('GtkWidgetEventFilter', "0.12.31.2.2", "clicked", "Second")
+ act2 = addon.create('BubbleMessage', message="Second message", pos=[250, 150], tailpos=[1,2])
+ st1 = State("INIT")
+ st1.add_action(act1)
+ st1.add_event_filter(ev1)
+ st2 = State("Second")
+ st2.add_action(act2)
+ fsm.add_state(st1)
+ fsm.add_state(st2)
+ guid = uuid1()
+
+ serializer.save_fsm(fsm, 'TestTutorial1.xml', test_path)
+
+ # Create a dummy tutorial metadata file
+ self.create_test_metadata_file(os.path.join(test_path, 'meta.ini'), guid)
+
+ #Zip these tutorials files in the pkzip file format
+ archive_list = [os.path.join(test_path, 'meta.ini'), os.path.join(test_path, 'TestTutorial1.xml')]
+
+ zfilename = "TestTutorial.zip"
+
+ zout = zipfile.ZipFile(os.path.join(test_path, zfilename), "w")
+ for fname in archive_list:
+ zout.write(fname)
+ zout.close()
+
+ # test if the file is a valid pkzip file
+ if zipfile.is_zipfile(zfilename) != True:
+ fail("The zipping of the tutorial files failed.")
+
+ # test installTutorials function
+ vault = Vault()
+
+ install_return = vault.installTutorials(test_path, 'TestTutorial.zip', False)
+ assert install_return != 2, "Tutorial install has returned an error"
+
+ # check if the tutorial is now in the vault
+ try :
+ serializer.find_tutorial_dir_with_guid(guid)
+ except IOError:
+ fail("Cannot find the specified tutorial's GUID in the vault")
+
def test_query(self):
"""
@@ -187,6 +203,16 @@ class VaultInterfaceTest(unittest.TestCase):
else:
assert False, 'list is empty or name property is wrong'
+
+ def tearDown(self):
+
+ folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data');
+ for file in os.listdir(folder):
+ file_path = os.path.join(folder, file)
+ shutil.rmtree(file_path)
+
+ if (os.path.isdir(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp'))):
+ shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'tmp'))
class SerializerInterfaceTest(unittest.TestCase):
"""
@@ -215,13 +241,9 @@ class XMLSerializerTest(unittest.TestCase):
Tests the transformation of XML to FSM, then back.
"""
def setUp(self):
-## # Make the serializer believe the test is in a activity path
-## os.environ["SUGAR_PREFIX"] = self.testpath
-## os.environ["SUGAR_PROFILE"] = 'test'
-## self.testpath = "test/tmp/testdata/"
- os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
- path = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
+ os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
+ path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
if os.path.isdir(path) != True:
os.makedirs(path)
@@ -255,7 +277,12 @@ class XMLSerializerTest(unittest.TestCase):
Removes the created files, if need be.
"""
if self.remove == True:
- shutil.rmtree(os.path.join(os.getenv("HOME"),".sugar", 'test'))
+ shutil.rmtree(os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path'))
+
+ folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data');
+ for file in os.listdir(folder):
+ file_path = os.path.join(folder, file)
+ shutil.rmtree(file_path)
def test_save(self):
"""
@@ -263,17 +290,15 @@ class XMLSerializerTest(unittest.TestCase):
"Remove" boolean argument specify if the test data must be removed or not
"""
xml_ser = XMLSerializer()
- os.makedirs(os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)))
+ os.makedirs(os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid)))
#rpdb2.start_embedded_debugger('flakyPass')
- xml_ser.save_fsm(self.fsm, bundler.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.bundler._get_store_root(), str(self.uuid)))
+ xml_ser.save_fsm(self.fsm, sugar.tutorius.vault.TUTORIAL_FILENAME, os.path.join(sugar.tutorius.vault._get_store_root(), str(self.uuid)))
def test_save_and_load(self):
"""
Load up the written FSM and compare it with the object representation.
"""
self.test_save()
- ## testpath = "/tmp/testdata/"
- ## rpdb2.start_embedded_debugger('flakyPass')
xml_ser = XMLSerializer()
# This interface needs to be redone... It's not clean because there is
@@ -357,14 +382,14 @@ class TutorialBundlerTests(unittest.TestCase):
"""
def setUp(self):
- os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
- path = os.path.join(sugar.tutorius.bundler._get_store_root(), 'test_bundle_path')
+ os.environ["SUGAR_BUNDLE_PATH"] = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
+ path = os.path.join(sugar.tutorius.vault._get_store_root(), 'test_bundle_path')
if os.path.isdir(path) != True:
os.makedirs(path)
#generate a test GUID
self.test_guid = uuid1()
- self.guid_path = os.path.join(bundler._get_store_root(),str(self.test_guid))
+ self.guid_path = os.path.join(sugar.tutorius.vault._get_store_root(),str(self.test_guid))
os.mkdir(self.guid_path)
self.ini_file = os.path.join(self.guid_path, "meta.ini")
@@ -388,6 +413,11 @@ class TutorialBundlerTests(unittest.TestCase):
os.remove(self.ini_file)
os.rmdir(self.guid_path)
+ folder = os.path.join(os.getenv("HOME"),".sugar", 'default', 'tutorius', 'data');
+ for file in os.listdir(folder):
+ file_path = os.path.join(folder, file)
+ shutil.rmtree(file_path)
+
## def test_add_ressource(self):
## bund = bundler.TutorialBundler(str(self.test_guid))
##
diff --git a/tutorius/vault.py b/tutorius/vault.py
index 8c29d67..cc277c8 100644
--- a/tutorius/vault.py
+++ b/tutorius/vault.py
@@ -125,13 +125,17 @@ class Vault(object):
zfile = zipfile.ZipFile(os.path.join(path, zip_file_name), "r" )
temp_path = os.path.join(_get_store_root(),'temp_extracted_tuto')
+ os.makedirs(temp_path)
for info in zfile.infolist():
fname = info.filename
# decompress each file's data
data = zfile.read(fname)
+ # get only the filename from the full path name
+ fname_splitted = fname.rsplit('/')
+ filename = fname_splitted[fname_splitted.__len__() - 1]
# save the decompressed data to a new file in a temp folder
- fout = open(os.path.join(temp_path, fname), 'w')
+ fout = open(os.path.join(temp_path, filename), 'w')
fout.write(data)
fout.close()
@@ -164,6 +168,9 @@ class Vault(object):
# New tutorial is newer or forceinstall flag is set, can overwrite the existing tutorial
shutil.rmtree(tutorial_path)
shutil.copytree(temp_path, tutorial_path)
+
+ # Remove temp data
+ shutil.rmtree(temp_path)
return 0
@@ -444,9 +451,9 @@ class XMLSerializer(Serializer):
@param guid The GUID of the tutorial that is to be loaded.
"""
# Attempt to find the tutorial's directory in the global directory
- global_dir = os.path.join(_get_store_root(), guid)
+ global_dir = os.path.join(_get_store_root(),str(guid))
# Then in the activty's bundle path
- activity_dir = os.path.join(_get_bundle_root(), guid)
+ activity_dir = os.path.join(_get_bundle_root(), str(guid))
# If they both exist
if os.path.isdir(global_dir) and os.path.isdir(activity_dir):
@@ -623,7 +630,7 @@ class XMLSerializer(Serializer):
fsm_elem = xml_dom.getElementsByTagName("FSM")[0]
- return self._load_xml_fsm(fsm_elem)
+ return self.load_xml_fsm(fsm_elem)
class TutorialBundler(object):