[libgames-support] Fix critical in HistoryFileImporter when no scores of any kind exist



commit 147eebba367cc121ec14678a8154b35f21510f26
Author: Michael Catanzaro <mcatanzaro igalia com>
Date:   Sun Feb 14 21:13:56 2016 -0600

    Fix critical in HistoryFileImporter when no scores of any kind exist
    
    If a new scores directory does not exist, and an old scores history file
    does not exist either, we try to use a null FileStream.

 games/scores/history-file-importer.vala |    5 ++++-
 tests/test-scores.vala                  |   29 +++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 1 deletions(-)
---
diff --git a/games/scores/history-file-importer.vala b/games/scores/history-file-importer.vala
index 62067e9..d473c9d 100644
--- a/games/scores/history-file-importer.vala
+++ b/games/scores/history-file-importer.vala
@@ -59,9 +59,12 @@ public class HistoryFileImporter : Importer
     protected override void importOldScores (Context context, File new_scores_dir) throws GLib.Error
     {
         var history_filename = Path.build_filename (new_scores_dir.get_path (), "..", "history", null);
+        var stream = FileStream.open (history_filename, "r");
+        if (stream == null)
+            return;
+
         debug ("Importing scores from %s", history_filename);
 
-        var stream = FileStream.open (history_filename, "r");
         string line;
         while ((line = stream.read_line ()) != null)
         {
diff --git a/tests/test-scores.vala b/tests/test-scores.vala
index 72a14c2..2fa8e39 100644
--- a/tests/test-scores.vala
+++ b/tests/test-scores.vala
@@ -252,6 +252,34 @@ private void test_import_from_history_file ()
     }
 }
 
+private void test_import_from_nonexistent_history_file ()
+{
+    try
+    {
+        var test_directory = File.new_for_path (get_test_directory_name ());
+        test_directory.make_directory_with_parents ();
+    }
+    catch (Error e)
+    {
+        error (e.message);
+    }
+
+    (void) new Context.with_importer (
+        "libgames-support-test",
+        "",
+        null,
+        (key) => {
+            return null;
+        },
+        Games.Scores.Style.POINTS_GREATER_IS_BETTER,
+        new Games.Scores.HistoryFileImporter ((line, out score, out category) => {
+            score = null;
+            category = null;
+        }));
+
+    /* No error */
+}
+
 public int main (string args[])
 {
     /* Start fresh.... */
@@ -263,6 +291,7 @@ public int main (string args[])
     test_suite.add (new TestCase ("Save Score to File", () => {}, test_save_score_to_file, delete_scores));
     test_suite.add (new TestCase ("Import from Score Directory", () => {}, test_import_from_score_directory, 
delete_scores));
     test_suite.add (new TestCase ("Import from History File", () => {}, test_import_from_history_file, 
delete_scores));
+    test_suite.add (new TestCase ("Import from Nonexistent History File", () => {}, 
test_import_from_nonexistent_history_file, delete_scores));
     return Test.run ();
 }
 


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