class JailSetup(unittest.TestCase): """Manage a temporary working directory.""" dirstack = None tempdir = None def setUp(self): self.dirstack = DirStack() try: self.tempdir = realpath(self.mkdtemp()) self.dirstack.push(self.tempdir) except: self.cleanUp() raise def tearDown(self): self.cleanUp() def cleanUp(self): if self.dirstack is not None: while self.dirstack: self.dirstack.pop() if self.tempdir is not None: if isdir(self.tempdir): shutil.rmtree(self.tempdir) def mkdtemp(self): return tempfile.mkdtemp()
def setUp(self): self.dirstack = DirStack() try: self.tempdir = realpath(self.mkdtemp()) self.dirstack.push(self.tempdir) except: self.cleanUp() raise
def setUp(self): # Save cwd self.cwd = os.getcwd() # Create a dirstack self.dirstack = DirStack() # Create a sandbox self.testdir = realpath(tempfile.mkdtemp()) os.chdir(self.testdir) # Create some dirs os.mkdir("foo") os.mkdir(join("foo", "bar"))
class DirStackTests(unittest.TestCase): def setUp(self): # Save cwd self.cwd = os.getcwd() # Create a dirstack self.dirstack = DirStack() # Create a sandbox self.testdir = realpath(tempfile.mkdtemp()) os.chdir(self.testdir) # Create some dirs os.mkdir("foo") os.mkdir(join("foo", "bar")) def tearDown(self): os.chdir(self.cwd) if os.path.isdir(self.testdir): shutil.rmtree(self.testdir) def testFixture(self): self.assertEqual(os.listdir(os.getcwd()), ["foo"]) def testPushDir(self): self.dirstack.push("foo") self.assertEqual(self.dirstack.stack, [self.testdir]) self.assertEqual(os.getcwd(), join(self.testdir, "foo")) self.dirstack.push("bar") self.assertEqual(self.dirstack.stack, [self.testdir, join(self.testdir, "foo")]) self.assertEqual(os.getcwd(), join(self.testdir, "foo", "bar")) def testPopDir(self): self.dirstack.push("foo") self.dirstack.push("bar") self.assertEqual(os.getcwd(), join(self.testdir, "foo", "bar")) self.dirstack.pop() self.assertEqual(os.getcwd(), join(self.testdir, "foo")) self.dirstack.pop() self.assertEqual(os.getcwd(), self.testdir) self.assertEqual(self.dirstack.stack, []) def testPushBadDir(self): self.assertRaises(OSError, self.dirstack.push, "peng") def testPopEmptyStack(self): self.assertEqual(self.dirstack.stack, []) self.dirstack.pop() self.assertEqual(self.dirstack.stack, []) def testStackLen(self): self.assertEqual(len(self.dirstack), 0) self.dirstack.push("foo") self.assertEqual(len(self.dirstack), 1) self.dirstack.push("bar") self.assertEqual(len(self.dirstack), 2) self.dirstack.pop() self.assertEqual(len(self.dirstack), 1) self.dirstack.pop() self.assertEqual(len(self.dirstack), 0) self.dirstack.pop() self.assertEqual(len(self.dirstack), 0)