def setUp(self): self.fileop = FileOperator() self.workdir = tempfile.mkdtemp()
class FileOpsTestCase(unittest.TestCase): def setUp(self): self.fileop = FileOperator() self.workdir = tempfile.mkdtemp() def tearDown(self): if os.path.isdir(self.workdir): shutil.rmtree(self.workdir) def test_ensure_dir(self): td = self.workdir os.rmdir(td) self.fileop.ensure_dir(td) self.assertTrue(os.path.exists(td)) self.fileop.dry_run = True os.rmdir(td) self.fileop.ensure_dir(td) self.assertFalse(os.path.exists(td)) def test_ensure_removed(self): td = self.workdir self.assertTrue(os.path.exists(td)) self.fileop.dry_run = True self.fileop.ensure_removed(td) self.assertTrue(os.path.exists(td)) self.fileop.dry_run = False self.fileop.ensure_removed(td) self.assertFalse(os.path.exists(td)) def test_is_writable(self): sd = 'subdir' ssd = 'subsubdir' path = os.path.join(self.workdir, sd, ssd) os.makedirs(path) path = os.path.join(path, 'test') self.assertTrue(self.fileop.is_writable(path)) if os.name == 'posix': self.assertFalse(self.fileop.is_writable('/etc')) def test_byte_compile(self): path = os.path.join(self.workdir, 'hello.py') dpath = cache_from_source(path, True) self.fileop.write_text_file(path, 'print("Hello, world!")', 'utf-8') self.fileop.byte_compile(path, optimize=False) self.assertTrue(os.path.exists(dpath)) def write_some_files(self): path = os.path.join(self.workdir, 'file1') written = [] self.fileop.write_text_file(path, 'test', 'utf-8') written.append(path) path = os.path.join(self.workdir, 'file2') self.fileop.copy_file(written[0], path) written.append(path) path = os.path.join(self.workdir, 'dir1') self.fileop.ensure_dir(path) return set(written), set([path]) def test_copy_check(self): srcpath = os.path.join(self.workdir, 'file1') self.fileop.write_text_file(srcpath, 'test', 'utf-8') dstpath = os.path.join(self.workdir, 'file2') os.mkdir(dstpath) self.assertRaises(ValueError, self.fileop.copy_file, srcpath, dstpath) os.rmdir(dstpath) if os.name == 'posix': # symlinks available linkpath = os.path.join(self.workdir, 'file3') self.fileop.write_text_file(linkpath, 'linkdest', 'utf-8') os.symlink(linkpath, dstpath) self.assertRaises(ValueError, self.fileop.copy_file, srcpath, dstpath) def test_commit(self): # will assert if record isn't set self.assertRaises(AssertionError, self.fileop.commit) self.fileop.record = True expected = self.write_some_files() actual = self.fileop.commit() self.assertEqual(actual, expected) self.assertFalse(self.fileop.record) def test_rollback(self): # will assert if record isn't set self.assertRaises(AssertionError, self.fileop.commit) self.fileop.record = True expected = self.write_some_files() actual = self.fileop.rollback() self.assertEqual(os.listdir(self.workdir), []) self.assertFalse(self.fileop.record)
class FileOpsTestCase(unittest.TestCase): def setUp(self): self.fileop = FileOperator() self.workdir = tempfile.mkdtemp() def tearDown(self): if os.path.isdir(self.workdir): shutil.rmtree(self.workdir) def test_ensure_dir(self): td = self.workdir os.rmdir(td) self.fileop.ensure_dir(td) self.assertTrue(os.path.exists(td)) self.fileop.dry_run = True os.rmdir(td) self.fileop.ensure_dir(td) self.assertFalse(os.path.exists(td)) def test_ensure_removed(self): td = self.workdir self.assertTrue(os.path.exists(td)) self.fileop.dry_run = True self.fileop.ensure_removed(td) self.assertTrue(os.path.exists(td)) self.fileop.dry_run = False self.fileop.ensure_removed(td) self.assertFalse(os.path.exists(td)) def test_is_writable(self): sd = 'subdir' ssd = 'subsubdir' path = os.path.join(self.workdir, sd, ssd) os.makedirs(path) path = os.path.join(path, 'test') self.assertTrue(self.fileop.is_writable(path)) if os.name == 'posix': self.assertFalse(self.fileop.is_writable('/etc')) def test_byte_compile(self): path = os.path.join(self.workdir, 'hello.py') dpath = cache_from_source(path, True) self.fileop.write_text_file(path, 'print("Hello, world!")', 'utf-8') self.fileop.byte_compile(path, optimize=False) self.assertTrue(os.path.exists(dpath)) def write_some_files(self): path = os.path.join(self.workdir, 'file1') written = [] self.fileop.write_text_file(path, 'test', 'utf-8') written.append(path) path = os.path.join(self.workdir, 'file2') self.fileop.copy_file(written[0], path) written.append(path) path = os.path.join(self.workdir, 'dir1') self.fileop.ensure_dir(path) return set(written), set([path]) def test_commit(self): # will assert if record isn't set self.assertRaises(AssertionError, self.fileop.commit) self.fileop.record = True expected = self.write_some_files() actual = self.fileop.commit() self.assertEqual(actual, expected) self.assertFalse(self.fileop.record) def test_rollback(self): # will assert if record isn't set self.assertRaises(AssertionError, self.fileop.commit) self.fileop.record = True expected = self.write_some_files() actual = self.fileop.rollback() self.assertEqual(os.listdir(self.workdir), []) self.assertFalse(self.fileop.record)