class JailSetup(unittest.TestCase): """Manage a temporary working directory.""" dirstack = None tempdir = None def setUp(self): self.dirstack = ChdirStack() 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 mkfile(self, name, body=''): with open(name, 'wt') as file: file.write(body)
def setUp(self): self.dirstack = ChdirStack() 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 = ChdirStack() # Create a sandbox self.testdir = realpath(tempfile.mkdtemp()) os.chdir(self.testdir) # Create some dirs os.mkdir('foo') os.mkdir(join('foo', 'bar'))
class ChdirStackTests(unittest.TestCase): def setUp(self): # Save cwd self.cwd = os.getcwd() # Create a dirstack self.dirstack = ChdirStack() # 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 testPushEmptyDir(self): self.dirstack.push('') self.assertEqual(self.dirstack.stack, [self.testdir]) self.assertEqual(os.getcwd(), self.testdir) 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)
class ChdirStackTests(unittest.TestCase): def setUp(self): # Save cwd self.cwd = os.getcwd() # Create a dirstack self.dirstack = ChdirStack() # 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 testPushEmptyDir(self): self.dirstack.push('') self.assertEqual(self.dirstack.stack, [self.testdir]) self.assertEqual(os.getcwd(), self.testdir) 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)