def testADirectory(self): ''' Pass a single directoty to EmuGlobalContext; The directory contains two files ''' org_path = os.getcwd() nfile1 = os.path.join(self._nested_dir, self._nested_file1) nfile2 = os.path.join(self._nested_dir, self._nested_file2) path = [os.path.join(self._data_path, self._nested_dir)] with emu.EmuGlobalContext(path) as context: cur_path = os.getcwd() self.assertNotEqual( org_path, cur_path, "A directory test: current directory didn't change in with") self.assertTrue( os.path.exists(self._nested_dir), "A directory test: data directory doesn't exist in with") self.assertTrue(os.path.exists(nfile1), "A directory test: file 1 doesn't exist in with") self.assertTrue(os.path.exists(nfile2), "A directory test: file 2 doesn't exist in with") abs_nested_dir = os.path.abspath(self._nested_dir) after_path = os.getcwd() self.assertEqual( org_path, after_path, "A directory test: current directory wasn't restored after with") self.assertFalse( os.path.exists(abs_nested_dir), "A directory test: data directory DOES exist after with")
def justReturn(self, org_path): ''' Returns directly from "with" statement Sub method for testReturn ''' files = [os.path.join(self._data_path, self._data_file)] with emu.EmuGlobalContext(files): cur_path = os.getcwd() self.assertNotEqual( org_path, cur_path, "A file test: current directory didn't changed in with") return
def testEmptyFiles(self): ''' Pass an empty list to EmuGlobalContext ''' org_path = os.getcwd() files = [] with emu.EmuGlobalContext(files) as context: cur_path = os.getcwd() self.assertEqual( org_path, cur_path, "Empty files test: current directory changed in with:" + cur_path) self.assertEqual( org_path, cur_path, "Empty files test: current directory changed after with")
def testWrongFile(self): org_path = os.getcwd() files = [os.path.join(self._data_path, 'does_not_exist')] try: with emu.EmuGlobalContext(files) as context: self.assertTrue(True, "Wrong file test: Shouldn't come in here") except IOError as e: pass # Expected after_path = os.getcwd() self.assertEqual( org_path, after_path, "Wrong file test: current directory wasn't restored after with")
def testAFile(self): ''' Pass a single file to EmuGlobalContext ''' org_path = os.getcwd() files = [os.path.join(self._data_path, self._data_file)] with emu.EmuGlobalContext(files) as context: cur_path = os.getcwd() self.assertNotEqual( org_path, cur_path, "A file test: current directory didn't changed in with") self.assertTrue(os.path.exists(self._data_file), "A file test: data file doesn't exist") abs_data_file = os.path.abspath(self._data_file) after_path = os.getcwd() self.assertEqual( org_path, after_path, "A file test: current directory wasn't restored after with") self.assertFalse(os.path.exists(abs_data_file), "A file test: data file DOES exist after with")
def testComplex(self): ''' Pass a file and a directory to EmuGlobalContext ''' org_path = os.getcwd() paths = [ os.path.join(self._data_path, self._data_file), os.path.join(self._data_path, self._nested_dir) ] nfile1 = os.path.join(self._nested_dir, self._nested_file1) nfile2 = os.path.join(self._nested_dir, self._nested_file2) with emu.EmuGlobalContext(paths) as context: cur_path = os.getcwd() self.assertNotEqual( org_path, cur_path, "Complex test: current directory didn't change in with") self.assertTrue(os.path.exists(self._data_file), "A file test: data file doesn't exist") self.assertTrue( os.path.exists(self._nested_dir), "Complex test: data directory doesn't exist in with") self.assertTrue(os.path.exists(nfile1), "A directory test: file 1 doesn't exist in with") self.assertTrue(os.path.exists(nfile2), "A directory test: file 2 doesn't exist in with") abs_data_file = os.path.abspath(self._data_file) abs_nested_dir = os.path.abspath(self._nested_dir) after_path = os.getcwd() self.assertEqual( org_path, after_path, "A directory test: current directory wasn't restored after with") self.assertFalse(os.path.exists(abs_data_file), "A file test: data file DOES exist after with") self.assertFalse( os.path.exists(abs_nested_dir), "A directory test: data directory DOES exist after with")
def testException(self): ''' Throw an exception in "with" statement Make sure the current working directory will be restored ''' org_path = os.getcwd() files = [os.path.join(self._data_path, self._data_file)] raised = False try: with emu.EmuGlobalContext(files): cur_path = os.getcwd() self.assertNotEqual( org_path, cur_path, "A file test: current directory didn't changed in with") raise DummyException except DummyException: raised = True after_path = os.getcwd() self.assertEqual( org_path, after_path, "Exception test: current directory wasn't restored after with") self.assertTrue(raised, "Exception test: exception was not raised")