def _archive_completed(self, filepath): """ Args: filepath (str): absolute path to a .mtask file. """ (active_ast, archive_ast) = self._archive_completed_as_ast(filepath) archive_path = self.get_archived_path(filepath) tempdir = tempfile.mkdtemp() try: # create tempfile objects active_taskfile = taskfiles.TaskFile( '{}/active.mtask'.format(tempdir)) archive_taskfile = taskfiles.TaskFile( '{}/archive.mtask'.format(tempdir)) # write tempfiles active_taskfile.write(active_ast) archive_taskfile.write(archive_ast) # (if successful) overwrite real files active_taskfile.copyfile(filepath) archive_taskfile.copyfile(archive_path) finally: # delete tempdir if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def test(self): project = projects.Project.from_path(_sample_project_dir) result = set(project.iter_taskfiles()) expects = { taskfiles.TaskFile( '{}/work.mtask'.format(_sample_project_dir)), taskfiles.TaskFile( '{}/home.mtask'.format(_sample_project_dir)), taskfiles.TaskFile( '{}/.taskmage/home.mtask'.format(_sample_project_dir)), } assert result == expects
def test(self): sample_project_dir = '{}/resources/sample_project'.format( _tests_dir) project = projects.Project.from_path(sample_project_dir) def is_home_dot_mtask(taskfile): return taskfile.filepath.endswith('home.mtask') result = set(project.filter_taskfiles([is_home_dot_mtask])) expects = { taskfiles.TaskFile('{}/home.mtask'.format(sample_project_dir)), taskfiles.TaskFile( '{}/.taskmage/home.mtask'.format(sample_project_dir)), } assert result == expects
def get_taskfile(data): """ Get a TaskFile object, with fake-read-data. Args: data (object): a native-python collection. it will be encoded as json. """ json_data = json.dumps(data) taskfile = taskfiles.TaskFile('/var/tmp/fakefile.mtask') taskfile.read = mock.Mock(return_value=json_data) return taskfile
def test(self): """ This is a very evil test.. I need to figure out how to clean this up. """ tempdir = tempfile.mkdtemp() filepath = '{}/file.mtask'.format(tempdir) taskfile = taskfiles.TaskFile(filepath) try: mock_open = mock.mock_open() with mock.patch(ns + '.open', mock_open, create=True): taskfile.write(self.ast_tree) written_data = mock_open().write.call_args[0][0] data = json.loads(written_data) assert data == self.mtask_tree finally: if os.path.isdir(tempdir): shutil.rmtree(tempdir)
def iter_taskfiles(self): """ Iterates over all `*.mtask` files in project (both completed and uncompleted). Returns: Iterable: iterable of all project taskfiles .. code-block:: python [ TaskFile('/path/to/todos/file1.mtask'), TaskFile('/path/to/todos/file2.mtask'), TaskFile('/path/to/todos/file3.mtask'), ... ] """ for (root, dirnames, filenames) in os.walk(self.root): for filename in filenames: if not filename.endswith('.mtask'): continue filepath = '{}/{}'.format(root, filename) yield taskfiles.TaskFile(filepath)
def test_inequality(self): taskfile_a = taskfiles.TaskFile('/var/tmp/a.mtask') taskfile_b = taskfiles.TaskFile('/var/tmp/b.mtask') assert taskfile_a != taskfile_b
def test(self): taskfile = taskfiles.TaskFile('/var/tmp/file.mtask') assert str(taskfile) == taskfile.filepath
def test_taskfile_hash_is_different_from_string(self): taskfile = taskfiles.TaskFile('todo.mtask') assert hash(taskfile) != hash(taskfile.filepath)
def test_taskfiles_with_different_file_do_not_share_hash_value(self): taskfile_a = taskfiles.TaskFile('todo.mtask') taskfile_b = taskfiles.TaskFile('other.mtask') assert hash(taskfile_a) != hash(taskfile_b)
def test_taskfiles_with_same_file_share_hash_value(self): taskfile_a = taskfiles.TaskFile('todo.mtask') taskfile_b = taskfiles.TaskFile('todo.mtask') assert hash(taskfile_a) == hash(taskfile_b)