[glom/glom-1-14] Fix document saving (recently broken by me) and add a test for it.



commit 63be021f6ad6951d2eb20d87a2e7b3377610e4cb
Author: Murray Cumming <murrayc murrayc com>
Date:   Sat May 1 10:55:18 2010 +0200

    Fix document saving (recently broken by me) and add a test for it.
    
    * glom/libglom/document/bakery/document.cc: write_to_disk():
    Do not fail if the parent directory already exists.
    * Makefile_tests.am:
    * tests/test_document_autosave.cc: Added a test of document saving and
    autosaving.

 ChangeLog                                |   10 +++
 Makefile_tests.am                        |    4 +
 glom/libglom/document/bakery/document.cc |    9 ++-
 tests/test_document_autosave.cc          |  111 ++++++++++++++++++++++++++++++
 4 files changed, 132 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 8718663..3a65441 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2010-05-01  Murray Cumming  <murrayc murrayc com>
 
+	Fix document saving (recently broken by me) and add a test for it.
+
+	* glom/libglom/document/bakery/document.cc: write_to_disk():
+	Do not fail if the parent directory already exists.
+	* Makefile_tests.am:
+	* tests/test_document_autosave.cc: Added a test of document saving and 
+	autosaving.
+
+2010-05-01  Murray Cumming  <murrayc murrayc com>
+
 	Move tests out of sub-directories, simplifying the build file.
 
 	* tests/Makefile_test.am:
diff --git a/Makefile_tests.am b/Makefile_tests.am
index bb8b70e..9576ff0 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -22,6 +22,7 @@ check_PROGRAMS =						\
 	glom/utility_widgets/test_flowtable			\
 	glom/test_pyembed					\
 	tests/test_document_load			\
+	tests/test_document_autosave			\
 	tests/test_parsing_time			\
 	tests/test_signal_reemit			\
 	tests/test_load_python_library\
@@ -36,6 +37,7 @@ check_PROGRAMS =						\
 	tests/test_selfhosting_new_empty
 
 TESTS =	tests/test_document_load	\
+  tests/test_document_autosave	\
 	tests/test_parsing_time	\
 	tests/test_signal_reemit \
 	tests/test_dtd_file_validation.sh \
@@ -70,6 +72,7 @@ glom_libglom_test_sharedptr_layoutitem_SOURCES = glom/libglom/test_sharedptr_lay
 glom_test_pyembed_SOURCES = glom/test_pyembed.cc
 
 tests_test_document_load_SOURCES = tests/test_document_load.cc
+tests_test_document_autosave_SOURCES = tests/test_document_autosave.cc
 tests_test_parsing_time_SOURCES = tests/test_parsing_time.cc
 tests_test_signal_reemit_SOURCES = tests/test_signal_reemit.cc
 tests_test_load_python_library_SOURCES = tests/test_load_python_library.cc
@@ -129,6 +132,7 @@ glom_test_pyembed_LDADD = $(LIBGLOM_LIBS) $(PYTHON_LIBS)
 
 tests_test_parsing_time_LDADD = $(tests_ldadd)
 tests_test_document_load_LDADD = $(tests_ldadd) $(LIBGLOM_LIBS)
+tests_test_document_autosave_LDADD = $(tests_ldadd) $(LIBGLOM_LIBS)
 tests_test_signal_reemit_LDADD = $(LIBGLOM_LIBS)
 tests_test_load_python_library_LDADD = $(LIBGLOM_LIBS)
 tests_test_python_module_LDADD = $(tests_ldadd) $(PYTHON_LIBS)
diff --git a/glom/libglom/document/bakery/document.cc b/glom/libglom/document/bakery/document.cc
index 7b46bc4..5c71d46 100644
--- a/glom/libglom/document/bakery/document.cc
+++ b/glom/libglom/document/bakery/document.cc
@@ -315,8 +315,13 @@ bool Document::write_to_disk()
       }
       catch(const Gio::Error& ex)
       {
-        std::cerr << "Bakery::Document::write_to_disk(): Error from Gio::File::make_directory_with_parents(): uri=" << m_file_uri << "error=" << ex.what() << std::endl;
-        return false;
+        //If it exists already then that's good.
+        //Otherwise something unexpected happened.
+        if(ex.code() != Gio::Error::EXISTS)
+        {
+          std::cerr << "Bakery::Document::write_to_disk(): Error from Gio::File::make_directory_with_parents(): parent of uri=" << m_file_uri << "error=" << ex.what() << std::endl;
+          return false;
+        }
       }
 
 
diff --git a/tests/test_document_autosave.cc b/tests/test_document_autosave.cc
new file mode 100644
index 0000000..796ea3e
--- /dev/null
+++ b/tests/test_document_autosave.cc
@@ -0,0 +1,111 @@
+/* Glom
+ *
+ * Copyright (C) 2010 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <libglom/document/document.h>
+#include <libglom/init.h>
+#include <giomm/file.h>
+
+Glib::ustring file_uri;
+
+void cleanup()
+{
+  try
+  {
+    //TODO: Catch exceptions:
+    Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(file_uri);
+    file->remove();
+  }
+  catch(const Gio::Error& ex)
+  {
+    //It's OK if it's not found - we just want to make sure it doesn't exist.
+    if(ex.code() == Gio::Error::NOT_FOUND)
+      return;
+
+    std::cerr << "Exception from Gio::File::remove(): " << ex.what();
+    exit(EXIT_FAILURE);
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << "Exception from Gio::File::remove(): " << ex.what();
+    exit(EXIT_FAILURE);
+  }
+}
+
+int main()
+{
+  Glom::libglom_init();
+
+  //For instance, /tmp/testfile.glom");
+  const std::string temp_filename = "testglom";
+  const std::string temp_filepath = Glib::build_filename(Glib::get_tmp_dir(), 
+    temp_filename);
+  file_uri = Glib::filename_to_uri(temp_filepath);
+  
+  //Make sure that the file does not exist yet:
+  cleanup();
+
+  const Glib::ustring test_title = "test_title";
+  
+  //Test manual saving:
+  {
+    Glom::Document document;
+    document.set_file_uri(file_uri);
+    document.set_hosting_mode(Glom::Document::HOSTING_MODE_POSTGRES_CENTRAL);
+    document.set_database_title(test_title);
+    const bool saved = document.save();
+    g_assert(saved);
+  }
+  {
+    Glom::Document document;
+    document.set_file_uri(file_uri);
+    int failure_code = 0;
+    const bool test = document.load(failure_code);
+    g_assert(test);
+    
+    g_assert( document.get_database_title() == test_title );
+  }
+
+  cleanup();
+
+  //Test autosaving:
+  {
+    Glom::Document document;
+    document.set_file_uri(file_uri);
+    document.set_hosting_mode(Glom::Document::HOSTING_MODE_POSTGRES_CENTRAL);
+    document.set_allow_autosave();
+    document.set_database_title(test_title);
+    g_assert( !document.get_modified() );
+  }
+  {
+    Glom::Document document;
+    document.set_file_uri(file_uri);
+    int failure_code = 0;
+    const bool test = document.load(failure_code);
+    g_assert(test);
+    
+    g_assert( document.get_database_title() == test_title );
+  }
+  
+  cleanup();
+  
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]