Repository: getnikola/nikola

Issue:
Getting the title from rest titles is broken.
This used to produce a post with title  "Write your post here." via get_metadata_from_file and now it doesn't anymore.

```
.. slug: asda
.. date: 2015-07-17 13:07:03 UTC
.. tags: 
.. category: 
.. link: 
.. description: 
.. type: text

Write your post here.
=====================


sdfasdfasdf
```

Only found this when fixing a pile of broken tests for the  mock 1.1.4 switch.



Discussion:

Resolving patch:
diff --git a/nikola/post.py b/nikola/post.py
--- a/nikola/post.py
+++ b/nikola/post.py
@@ -853,10 +853,12 @@ def _get_metadata_from_file(meta_data):
     False
     >>> 'title' in g(["",".. title: FooBar"])  # for #520
     True
+    >>> 'title' in g([".. foo: bar","","FooBar", "------"])  # for #1895
+    True
 
     """
     meta = {}
-
+    seen_meta_flag = False
     re_md_title = re.compile(r'^{0}([^{0}].*)'.format(re.escape('#')))
     # Assuming rst titles are going to be at least 4 chars long
     # otherwise this detects things like ''' wich breaks other markups.
@@ -868,7 +870,7 @@ def _get_metadata_from_file(meta_data):
         # and since we are here because it's a 1-file post
         # let's be flexible on what we accept, so, skip empty
         # first lines.
-        if not line and i > 0:
+        if not line and not seen_meta_flag and i > 0:
             break
         if 'title' not in meta:
             match = re_meta(line, 'title')
@@ -888,6 +890,7 @@ def _get_metadata_from_file(meta_data):
         match = re_meta(line)
         if match[0]:
             meta[match[0]] = match[1]
+            seen_meta_flag = True
 
     return meta
 


Test patch:
diff --git a/tests/test_command_import_wordpress.py b/tests/test_command_import_wordpress.py
--- a/tests/test_command_import_wordpress.py
+++ b/tests/test_command_import_wordpress.py
@@ -2,8 +2,6 @@
 from __future__ import unicode_literals, absolute_import
 
 import os
-import sys
-
 
 import unittest
 import mock
@@ -441,21 +439,18 @@ def test_get_configuration_output_path(self):
         self.assertTrue(self.import_command.name in config_path_with_timestamp)
 
     def test_write_content_does_not_detroy_text(self):
-        content = b"""<h1>Installation</h1>
-Follow the instructions <a title="Installing Jenkins" href="https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins">described here</a>.
-
-<h1>Plugins</h1>
-There are many plugins.
-<h2>Violations</h2>
-You can use the <a title="Jenkins Plugin: Violations" href="https://wiki.jenkins-ci.org/display/JENKINS/Violations">Violations</a> plugin."""
+        content = b"""FOO"""
         open_mock = mock.mock_open()
         with mock.patch('nikola.plugins.basic_import.open', open_mock, create=True):
             self.import_command.write_content('some_file', content)
 
-        open_mock.assert_called_once_with('some_file', 'wb+')
-        call_context = open_mock()
-        call_context.write.assert_called_once_with(
-            content.join([b'<html><body>', b'</body></html>']))
+        #import pdb; pdb.set_trace()
+        open_mock.assert_has_calls([
+            mock.call(u'some_file', u'wb+'),
+            mock.call().__enter__(),
+            mock.call().write(b'<html><body><p>FOO</p></body></html>'),
+            mock.call().__exit__(None, None, None)]
+        )
 
     def test_configure_redirections(self):
         """
diff --git a/tests/test_rss_feeds.py b/tests/test_rss_feeds.py
--- a/tests/test_rss_feeds.py
+++ b/tests/test_rss_feeds.py
@@ -91,9 +91,12 @@ def setUp(self):
                     # lxml will complain if the encoding is specified in the
                     # xml when running with unicode strings.
                     # We do not include this in our content.
-                    open_handle = opener_mock()
-                    file_content = [call[1][0]
-                                    for call in open_handle.mock_calls[1:-1]][0]
+                    try:
+                        file_content = [
+                            call[1][0]
+                            for call in opener_mock.mock_calls[2:-1]][0]
+                    except IndexError:
+                        import pdb; pdb.set_trace()
                     splitted_content = file_content.split('\n')
                     self.encoding_declaration = splitted_content[0]
                     content_without_encoding_declaration = splitted_content[1:]
diff --git a/tests/test_utils.py b/tests/test_utils.py
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -18,13 +18,13 @@ class dummy(object):
 
 class GetMetaTest(unittest.TestCase):
     def test_getting_metadata_from_content(self):
-        file_metadata = [".. title: Nikola needs more tests!\n",
-                         ".. slug: write-tests-now\n",
-                         ".. date: 2012/09/15 19:52:05\n",
-                         ".. tags:\n",
-                         ".. link:\n",
-                         ".. description:\n",
-                         "Post content\n"]
+        file_metadata = ".. title: Nikola needs more tests!\n"\
+                        ".. slug: write-tests-now\n"\
+                        ".. date: 2012/09/15 19:52:05\n"\
+                        ".. tags:\n"\
+                        ".. link:\n"\
+                        ".. description:\n"\
+                        "Post content\n"
 
         opener_mock = mock.mock_open(read_data=file_metadata)
         opener_mock.return_value.readlines.return_value = file_metadata
@@ -45,13 +45,13 @@ def test_getting_metadata_from_content(self):
         self.assertTrue(nsm)
 
     def test_get_title_from_rest(self):
-        file_metadata = [".. slug: write-tests-now\n",
-                         ".. date: 2012/09/15 19:52:05\n",
-                         ".. tags:\n",
-                         ".. link:\n",
-                         ".. description:\n",
-                         "Post Title\n",
-                         "----------\n"]
+        file_metadata = ".. slug: write-tests-now\n"\
+                        ".. date: 2012/09/15 19:52:05\n"\
+                        ".. tags:\n"\
+                        ".. link:\n"\
+                        ".. description:\n\n"\
+                        "Post Title\n"\
+                        "----------\n"
 
         opener_mock = mock.mock_open(read_data=file_metadata)
         opener_mock.return_value.readlines.return_value = file_metadata
@@ -72,11 +72,11 @@ def test_get_title_from_rest(self):
         self.assertTrue(nsm)
 
     def test_get_title_from_fname(self):
-        file_metadata = [".. slug: write-tests-now\n",
-                         ".. date: 2012/09/15 19:52:05\n",
-                         ".. tags:\n",
-                         ".. link:\n",
-                         ".. description:\n"]
+        file_metadata = ".. slug: write-tests-now\n"\
+                        ".. date: 2012/09/15 19:52:05\n"\
+                        ".. tags:\n"\
+                        ".. link:\n"\
+                        ".. description:\n"
 
         opener_mock = mock.mock_open(read_data=file_metadata)
         opener_mock.return_value.readlines.return_value = file_metadata
@@ -97,12 +97,12 @@ def test_get_title_from_fname(self):
         self.assertTrue(nsm)
 
     def test_use_filename_as_slug_fallback(self):
-        file_metadata = [".. title: Nikola needs more tests!\n",
-                         ".. date: 2012/09/15 19:52:05\n",
-                         ".. tags:\n",
-                         ".. link:\n",
-                         ".. description:\n",
-                         "Post content\n"]
+        file_metadata = ".. title: Nikola needs more tests!\n"\
+                        ".. date: 2012/09/15 19:52:05\n"\
+                        ".. tags:\n"\
+                        ".. link:\n"\
+                        ".. description:\n\n"\
+                        "Post content\n"
 
         opener_mock = mock.mock_open(read_data=file_metadata)
         opener_mock.return_value.readlines.return_value = file_metadata
@@ -113,7 +113,6 @@ def test_use_filename_as_slug_fallback(self):
 
         with mock.patch('nikola.post.io.open', opener_mock, create=True):
             meta, nsm = get_meta(post, 'Slugify this')
-
         self.assertEqual('Nikola needs more tests!', meta['title'])
         self.assertEqual('slugify-this', meta['slug'])
         self.assertEqual('2012/09/15 19:52:05', meta['date'])
@@ -250,7 +249,7 @@ def test_string_input(self):
         except NameError:  # Python 3
             u = str(S)
 
-        cn = S()      #   no language specified
+        cn = S()      # no language specified
         cr = S('xx')  # real language specified
         cf = S('zz')  # fake language specified