def test_saves_to_filepath(self): temp_name = self.get_temp_file_name() self.assertFalse(os.path.exists(temp_name)) a_nfile = NFile(temp_name) self.assertFalse(os.path.exists(a_nfile._file_path)) a_nfile.save("empty content") self.assertTrue(os.path.exists(a_nfile._file_path))
def test_file_is_read_properly(self): to_load_file = tempfile.NamedTemporaryFile() load_text = "Something to load" to_load_file.write(load_text) to_load_file.seek(0) # FIXME: buffer errors could break this a_nfile = NFile(to_load_file.name) content = a_nfile.read() self.assertEqual(content, load_text)
def test_actual_content_is_saved(self): file_content = "empty content" temp_name = self.get_temp_file_name() self.assertFalse(os.path.exists(temp_name)) a_nfile = NFile(temp_name) a_nfile.save(file_content) a_file = open(temp_name, "r") a_file_content = a_file.read() self.assertEqual(a_file_content, file_content)
def test_move_changes_filepath(self): temp_name = self.get_temp_file_name() new_temp_name = u"%s_new" % temp_name self._trash_files.append(new_temp_name) test_content = "zero" temp_file = open(temp_name, "w") temp_file.write(test_content) temp_file.close() a_nfile = NFile(temp_name) a_nfile.move(new_temp_name) self.assertEqual(a_nfile._file_path, new_temp_name)
def test_copy_flag_saves_to_path_only(self): temp_name = self.get_temp_file_name() temp_name_path = self.get_temp_file_name() #Since we cant assure the two paths are differents we do this temp_name_path = u"%s_really_unique" % temp_name_path self._trash_files.append(temp_name_path) self.assertNotEqual(temp_name, temp_name_path) a_nfile = NFile(temp_name) a_nfile.save("empty content", path=temp_name_path) self.assertFalse(os.path.exists(temp_name)) self.assertTrue(os.path.exists(temp_name_path))
def test_path_is_set_as_new_filepath(self): temp_name = self.get_temp_file_name() temp_name_path = self.get_temp_file_name() #Since we cant assure the two paths are differents we do this temp_name_path = u"%s_really_unique" % temp_name_path self._trash_files.append(temp_name_path) self.assertNotEqual(temp_name, temp_name_path) a_nfile = NFile(temp_name) self.assertNotEqual(temp_name_path, a_nfile._file_path) a_nfile.save("empty content", path=temp_name_path) self.assertEqual(temp_name_path, a_nfile._file_path)
def test_copy_flag_prevets_filepath_change(self): temp_name = self.get_temp_file_name() temp_name_path = self.get_temp_file_name() #Since we cant assure the two paths are differents we do this temp_name_path = u"%s_really_unique" % temp_name_path self._trash_files.append(temp_name_path) self.assertNotEqual(temp_name, temp_name_path) a_nfile = NFile(temp_name) self.assertNotEqual(temp_name_path, a_nfile._file_path) a_nfile.save("empty content", path=temp_name_path, copy=True) self.assertNotEqual(temp_name_path, a_nfile._file_path)
def test_file_is_moved(self): temp_name = self.get_temp_file_name() new_temp_name = u"%s_new" % temp_name self._trash_files.append(new_temp_name) test_content = "zero" temp_file = open(temp_name, "w") temp_file.write(test_content) temp_file.close() a_nfile = NFile(temp_name) a_nfile.move(new_temp_name) self.assertTrue(os.path.exists(new_temp_name)) read_test_content = open(new_temp_name, "r").read() self.assertEqual(read_test_content, test_content) self.assertFalse(os.path.exists(temp_name))
def get_file(self, nfile_path=None): print("\n\nnfile_path", nfile_path) if nfile_path is None: return NFile(nfile_path) if os.path.isdir(nfile_path): return None if nfile_path not in self.__tree: nfile = NFile(nfile_path) self.__add_file(nfile) elif nfile_path in self.__tree: nfile = self.__tree[nfile_path] else: nfile = NFile() nfile.savedAsNewFile.connect(self.__file_copied) return nfile
def get_file(self, nfile_path=None): if nfile_path is None: return NFile(nfile_path) if os.path.isdir(nfile_path): return None if nfile_path not in self.__tree: nfile = NFile(nfile_path) self.__add_file(nfile) elif nfile_path in self.__tree: nfile = self.__tree[nfile_path] else: nfile = NFile() nfile.saveAsNewFile['PyQt_PyObject', 'QString', 'QString'].connect(self.__file_copied) return nfile
def get_file(self, nfile_path=None): if nfile_path is None: return NFile(nfile_path) if os.path.isdir(nfile_path): return None if nfile_path not in self.__tree: nfile = NFile(nfile_path) self.__add_file(nfile) elif nfile_path in self.__tree: nfile = self.__tree[nfile_path] else: nfile = NFile() self.connect( nfile, SIGNAL("savedAsNewFile(PyQt_PyObject, QString, QString)"), self.__file_copied) return nfile
def test_knows_if_exists(self): """If the file exists, can NFile tell?""" existing_nfile = NFile(self._existing_file.name) self.assertTrue(existing_nfile._exists())
def test_creates_if_doesnt_exist(self): temp_name = self.get_temp_file_name() self.assertFalse(os.path.exists(temp_name)) a_nfile = NFile(temp_name) a_nfile.save("empty content") self.assertTrue(os.path.exists(temp_name))
def test_file_read_blows_on_nonexistent_file(self): temp_name = self.get_temp_file_name() a_nfile = NFile(temp_name) self.assertRaises(NinjaIOException, a_nfile.read)
def test_file_read_blows_when_nonexistent_path(self): a_nfile = NFile() self.assertRaises(NinjaNoFileNameException, a_nfile.read)
def test_filepath_changes_even_if_inexistent(self): temp_name = self.get_temp_file_name() a_nfile = NFile() a_nfile.move(temp_name) self.assertEqual(a_nfile._file_path, temp_name)
def test_save_no_filename_raises(self): """If there is no filename associated to the nfile we should get error""" no_filename_file = NFile() self.assertRaises(NinjaNoFileNameException, no_filename_file.save, ("dumb content", ))
def test_knows_if_desnt_exists(self): """If the file does not exists, can NFile tell?""" existing_nfile = NFile(self._non_existing_file.name) self._non_existing_file.close() self.assertFalse(existing_nfile._exists())