def test_file_write_append_not_exists(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act with fs.mock(): with open('/dir1/filenameY', 'a') as f: f.write(u'content 1 to filename Y in dir1') # Assert with fs.mock(): with open('/dir1/filenameY') as f: self.assertTrue(os.path.exists('/dir1/filenameY')) self.assertEqual(f.read(), u'content 1 to filename Y in dir1')
def test_expanduser(self): # Arrange fs = FileSystem({}, user = "******") # Act & Assert with fs.mock(): self.assertEqual(os.path.expanduser('~/teve'), '/home/douglas/teve')
def test_getcwd(self): # Arrange fs = FileSystem({}, cwd = '/dir42') # Act & Assert with fs.mock(): self.assertEqual(os.getcwd(), '/dir42')
def test_file_write(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act with fs.mock(): with open('/dir1/filename3', 'w') as f: f.write(u'content 1 to filename 3 in dir1\n') f.write(u'content 2 to filename 3 in dir1\n') # Assert with fs.mock(): self.assertTrue(os.path.exists('/dir1/filename3')) with open('/dir1/filename3') as f: self.assertEqual(f.read(), u'content 1 to filename 3 in dir1\ncontent 2 to filename 3 in dir1\n')
def test_abspath(self): # Arrange fs = FileSystem({}, cwd = '/dir1') # Act & Assert with fs.mock(): self.assertEqual(os.path.abspath('teve'), '/dir1/teve') self.assertEqual(os.path.abspath('/teve'), '/teve')
def test_is_file_exists(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): self.assertEqual(os.path.exists('filename1'), True)
def test_is_file_with_file(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): self.assertTrue(os.path.isfile('filename1')) self.assertFalse(os.path.isfile('filename2'))
def test_is_path_exists_with_dir(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): self.assertTrue(os.path.exists('dir1')) self.assertFalse(os.path.exists('dir2'))
def test_file_open_not_exists(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with self.assertRaises(FileNotFoundError): open('filename2')
def test_file_open(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with open('filename1') as f: self.assertEqual(f.read(), data['filename1'])
def test_mkdir_in_not_existing_dir(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with self.assertRaises(FileNotFoundError): os.mkdir('/dirX/dir3/')
def test_mkdir(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): os.mkdir('/dir1/dir3') self.assertTrue(os.path.exists('/dir1/dir3/'))
def test_mode_w_not_readable(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with open('/dir1/filenameZ', 'w') as f, self.assertRaises(UnsupportedOperation): f.read()
def test_mode_r_not_writable(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with open('/dir1/dir2/filename2', 'r') as f, self.assertRaises(UnsupportedOperation): f.write('test')
def test_file_write_dir_not_exists(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with self.assertRaises(FileNotFoundError): open('/dirX/filenameX', 'w')
def test_mkdir_existing(self): # Arrange data = copy(init_data) fs = FileSystem(data) # Act & Assert with fs.mock(): with self.assertRaises(FileExistsError): os.mkdir('/dir1/dir2/') self.assertTrue(os.path.exists('/dir1/dir2/filename2'))