edgimar avatar edgimar committed 1e27937

- handle case when org-file contains no headlines

Comments (0)

Files changed (2)

     last_indent_level = 0
     seen_first_task = False
     task_notes = None
+    task_title = None
     for n, line in enumerate(f):
         matches = headline_regex.findall(line)
         line = line.rstrip("\n")
     # add the last task to the tree, since the for loop won't be iterated
     # again after the last line of the file (tasks are added at beginning
     # of the for loop)
-    tasks_tree.last(indent_level).add_subtask(
-            title=task_title,
-            task_notes=task_notes,
-            task_status=task_status)
+    if task_title is not None:
+        tasks_tree.last(indent_level).add_subtask(
+                title=task_title,
+                task_notes=task_notes,
+                task_status=task_status)
+    else: # there are no headlines in the org-file; create a dummy headline
+        tasks_tree.last(0).add_subtask(
+                title="",
+                task_notes=task_notes,
+                task_status=None)
 
     f.close()
     return tasks_tree
         # a dummy headline will be added to contain the initial text
         self.assertEqual(str(tasktree), "* \n" + org_text)
 
+    def test_no_headlines(self):
+        """
+        Test the cases where there are no headlines at all in the file.
+        
+        """
+        # text should have trailing "\n" character, like most textfiles
+        org_text1 = textwrap.dedent("""\
+
+            Some non-headline text...
+            Another line of it.
+            """)
+        org_text2 = "" # empty file
+
+        for org_text in [org_text1, org_text2]:
+            tasktree = m.parse_text(org_text)
+            # a dummy headline will be added to contain the initial text
+            self.assertEqual(str(tasktree), "* \n" + org_text)
+
     def test_add_subtrees(self):
         org_text1 = textwrap.dedent("""\
             * Headline A1
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.