[gitg/wip/commit: 6/28] Added tests for libgitg



commit 77742a2792aa6ee4440811b7658d6eceaeb67df5
Author: Jesse van den Kieboom <jessevdk gmail com>
Date:   Sat Jun 29 14:46:33 2013 +0200

    Added tests for libgitg

 configure.ac                  |    1 +
 libgitg/Makefile.am           |    2 +
 libgitg/tests/Makefile.am     |   27 ++++
 libgitg/tests/main.vala       |  103 +++++++++++++
 libgitg/tests/repository.vala |  340 +++++++++++++++++++++++++++++++++++++++++
 libgitg/tests/test-stage.vala |   76 +++++++++
 libgitg/tests/test.vala       |   54 +++++++
 7 files changed, 603 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 7c250d0..e40e2fd 100644
--- a/configure.ac
+++ b/configure.ac
@@ -284,6 +284,7 @@ Makefile
 libgd/Makefile
 libgitg/Makefile
 libgitg/libgitg-1.0.pc
+libgitg/tests/Makefile
 libgitg-ext/Makefile
 libgitg-ext/libgitg-ext-1.0.pc
 gitg/Makefile
diff --git a/libgitg/Makefile.am b/libgitg/Makefile.am
index d2940f8..a61e484 100644
--- a/libgitg/Makefile.am
+++ b/libgitg/Makefile.am
@@ -1,3 +1,5 @@
+SUBDIRS = tests
+
 lib_LTLIBRARIES = libgitg-1.0.la
 
 AM_CPPFLAGS =                                                          \
diff --git a/libgitg/tests/Makefile.am b/libgitg/tests/Makefile.am
new file mode 100644
index 0000000..3dc4136
--- /dev/null
+++ b/libgitg/tests/Makefile.am
@@ -0,0 +1,27 @@
+AM_CPPFLAGS = -g -I$(top_srcdir) -I$(top_srcdir)/gitg -I$(top_srcdir)/libgitg $(GITG_DEBUG_FLAGS) 
$(GITG_CFLAGS)
+
+noinst_PROGRAMS = $(TEST_PROGS)
+
+AM_VALAFLAGS = $(GITG_PLUGIN_VALAFLAGS) --pkg posix
+
+TEST_PROGS = test-libgitg
+
+test_libgitg_SOURCES =                         \
+       test.vala                               \
+       main.vala                               \
+       repository.vala                         \
+       test-stage.vala
+
+test_libgitg_LDADD = $(GITG_PLUGIN_LIBS)
+test_libgitg_CFLAGS = $(GITG_PLUGIN_CFLAGS) -w
+
+TESTS = $(TEST_PROGS)
+
+BUILT_SOURCES =                                        \
+       test_libgitg_vala.stamp
+
+CLEANFILES =                                   \
+       $(test_libgitg_SOURCES:.vala=.c)        \
+       $(BUILT_SOURCES)
+
+-include $(top_srcdir)/git.mk
diff --git a/libgitg/tests/main.vala b/libgitg/tests/main.vala
new file mode 100644
index 0000000..9ac80e0
--- /dev/null
+++ b/libgitg/tests/main.vala
@@ -0,0 +1,103 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2013 - Jesse van den Kieboom
+ *
+ * gitg 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.
+ *
+ * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class Gitg.Test.Main
+{
+       private static TestCase[] s_cases;
+
+       public static void main(string[] args)
+       {
+               Gitg.init();
+               GLib.Test.init(ref args);
+
+               add(new Stage());
+
+               GLib.Test.run();
+       }
+
+       class TestCase : Object
+       {
+               private Gitg.Test.Test d_test;
+               private string d_name;
+               private uint d_signal_id;
+
+               public TestCase(Gitg.Test.Test test, string name, uint signal_id)
+               {
+                       d_test = test;
+                       d_name = name;
+                       d_signal_id = signal_id;
+               }
+
+               public string name
+               {
+                       get { return d_name; }
+               }
+
+               public void set_up()
+               {
+                       d_test.set_up();
+               }
+
+               public void tear_down()
+               {
+                       d_test.tear_down();
+               }
+
+               public void test()
+               {
+                       GLib.Signal.emit(d_test, d_signal_id, 0);
+               }
+       }
+
+       private static void add(Test test)
+       {
+               var ids = GLib.Signal.list_ids(test.get_type());
+
+               var suite = test.suite;
+
+               GLib.TestSuite.get_root().add_suite(suite);
+
+               foreach (var id in ids)
+               {
+                       GLib.SignalQuery q;
+
+                       GLib.Signal.query(id, out q);
+
+                       if (q.n_params != 0 || !q.signal_name.has_prefix("test-"))
+                       {
+                               continue;
+                       }
+
+                       var c = new TestCase(test,
+                                            q.signal_name[5:-1],
+                                            q.signal_id);
+
+                       s_cases += c;
+
+                       var tc = new GLib.TestCase(c.name,
+                                                  c.set_up,
+                                                  c.test,
+                                                  c.tear_down);
+
+                       suite.add(tc);
+               }
+       }
+}
+
+// ex:set ts=4 noet
diff --git a/libgitg/tests/repository.vala b/libgitg/tests/repository.vala
new file mode 100644
index 0000000..8245355
--- /dev/null
+++ b/libgitg/tests/repository.vala
@@ -0,0 +1,340 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2013 - Jesse van den Kieboom
+ *
+ * gitg 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.
+ *
+ * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class Gitg.Test.Repository : Gitg.Test.Test
+{
+       protected Gitg.Repository? d_repository;
+       private Ggit.Commit? d_last_commit;
+
+       struct File
+       {
+               public string filename;
+               public string? contents;
+       }
+
+       private File[] files_from_varargs(string? filename, va_list l)
+       {
+               File[] files = new File[] {};
+
+               while (filename != null)
+               {
+                       string contents = l.arg();
+
+                       files += File() {
+                               filename = filename,
+                               contents = contents
+                       };
+
+                       filename = l.arg();
+               }
+
+               return files;
+       }
+
+       private File[] filenames_from_varargs(string? filename, va_list l)
+       {
+               File[] files = new File[] {};
+
+               while (filename != null)
+               {
+                       files += File() {
+                               filename = filename,
+                               contents = null
+                       };
+
+                       filename = l.arg();
+               }
+
+               return files;
+       }
+
+       private void write_files(File[] files)
+       {
+               var wd = d_repository.get_workdir();
+
+               foreach (var f in files)
+               {
+                       var fp = wd.get_child(f.filename);
+
+                       try
+                       {
+                               var os = fp.replace(null, false, GLib.FileCreateFlags.NONE, null);
+                               os.write(f.contents.data);
+                               os.close();
+                       }
+                       catch (GLib.Error e)
+                       {
+                               assert_noerror(e);
+                       }
+               }
+       }
+
+       protected void commit(string? filename, ...)
+       {
+               if (d_repository == null)
+               {
+                       return;
+               }
+
+               var files = files_from_varargs(filename, va_list());
+               write_files(files);
+
+               // use the index to stage the files
+
+               Ggit.Index index;
+
+               try
+               {
+                       index = d_repository.get_index();
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               foreach (var f in files)
+               {
+                       try
+                       {
+                               index.add_path(f.filename);
+                       }
+                       catch (GLib.Error e)
+                       {
+                               assert_noerror(e);
+                       }
+               }
+
+               Ggit.OId treeoid;
+
+               try
+               {
+                       index.write();
+                       treeoid = index.write_tree();
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               // create commit
+               Ggit.Signature sig;
+
+               try
+               {
+                       sig = new Ggit.Signature.now("gitg tester",
+                                                    "gitg-tester gnome org");
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               Ggit.Tree tree;
+
+               try
+               {
+                       tree = (Ggit.Tree)d_repository.lookup(treeoid, typeof(Ggit.Tree));
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               Ggit.OId commitoid;
+
+               try
+               {
+                       Ggit.Commit[] parents;
+
+                       if (d_last_commit != null)
+                       {
+                               parents = new Ggit.Commit[] {d_last_commit};
+                       }
+                       else
+                       {
+                               parents = new Ggit.Commit[] {};
+                       }
+
+                       commitoid = d_repository.create_commit("HEAD",
+                                                              sig,
+                                                              sig,
+                                                              null,
+                                                              "Initial commit",
+                                                              tree,
+                                                              parents);
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               try
+               {
+                       d_last_commit = (Ggit.Commit)d_repository.lookup(commitoid,
+                                                                        typeof(Ggit.Commit));
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+               }
+       }
+
+       protected void workdir_remove(string? filename, ...)
+       {
+               if (d_repository == null)
+               {
+                       return;
+               }
+
+               var files = filenames_from_varargs(filename, va_list());
+               var wd = d_repository.get_workdir();
+
+               foreach (var f in files)
+               {
+                       var fs = wd.get_child(f.filename);
+
+                       try
+                       {
+                               fs.delete();
+                       }
+                       catch (GLib.Error e)
+                       {
+                               assert_noerror(e);
+                       }
+               }
+       }
+
+       protected void workdir_modify(string? filename, ...)
+       {
+               if (d_repository == null)
+               {
+                       return;
+               }
+
+               var files = files_from_varargs(filename, va_list());
+               write_files(files);
+       }
+
+       protected void index_modify(string? filename, ...)
+       {
+               if (d_repository == null)
+               {
+                       return;
+               }
+
+               var files = files_from_varargs(filename, va_list());
+
+               Ggit.OId id;
+
+               Ggit.Index index;
+
+               try
+               {
+                       index = d_repository.get_index();
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               // Stage modifications in the index
+               foreach (var f in files)
+               {
+                       try
+                       {
+                               id = d_repository.create_blob_from_buffer(f.contents.data);
+                       }
+                       catch (GLib.Error e)
+                       {
+                               assert_noerror(e);
+                               continue;
+                       }
+
+                       try
+                       {
+                               var entry = d_repository.create_index_entry_for_path(f.filename, id);
+                               index.add(entry);
+                       }
+                       catch (GLib.Error e)
+                       {
+                               assert_noerror(e);
+                       }
+               }
+
+               try
+               {
+                       index.write();
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+               }
+       }
+
+       protected override void set_up()
+       {
+               string wd;
+
+               try
+               {
+                       wd = GLib.DirUtils.make_tmp("gitg-test-XXXXXX");
+               }
+               catch (GLib.Error e)
+               {
+                       assert_noerror(e);
+                       return;
+               }
+
+               var f = GLib.File.new_for_path(wd);
+
+               try
+               {
+                       d_repository = (Gitg.Repository)Ggit.Repository.init_repository(f, false);
+               }
+               catch (GLib.Error e)
+               {
+                       GLib.DirUtils.remove(wd);
+                       assert_noerror(e);
+               }
+       }
+
+       protected override void tear_down()
+       {
+               if (d_repository == null)
+               {
+                       return;
+               }
+
+               var wd = d_repository.get_workdir();
+
+               // nasty stuff, but I'm not going to implement recursive remove by hand
+               // and glib doesn't provide anything for it
+               Posix.system("rm -rf '%s'".printf(wd.get_path()));
+
+               d_repository = null;
+       }
+}
+
+// ex:set ts=4 noet
diff --git a/libgitg/tests/test-stage.vala b/libgitg/tests/test-stage.vala
new file mode 100644
index 0000000..a873e6f
--- /dev/null
+++ b/libgitg/tests/test-stage.vala
@@ -0,0 +1,76 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2013 - Jesse van den Kieboom
+ *
+ * gitg 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.
+ *
+ * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class Gitg.Test.Stage : Gitg.Test.Repository
+{
+       protected override void set_up()
+       {
+               base.set_up();
+
+               // Configure repository
+               commit("a", "hello world\n",
+                      "b", "gitg test file\n",
+                      "c", "hello\n");
+
+               workdir_remove("c");
+               workdir_modify("a", "changed world\n");
+               workdir_modify("b", "changed test\n");
+
+               index_modify("b", "staged changes\n");
+       }
+
+       protected virtual signal void test_index_files()
+       {
+               var stage = d_repository.get_stage();
+               var e = stage.file_status();
+
+               var loop = new GLib.MainLoop();
+
+               e.next_files.begin(-1, (obj, res) => {
+                       var files = e.next_files.end(res);
+
+                       assert(files.length == 3);
+
+                       foreach (var f in files)
+                       {
+                               assert(f.path == "a" || f.path == "b" || f.path == "c");
+
+                               switch (f.path)
+                               {
+                               case "a":
+                                       assert(f.flags == Ggit.StatusFlags.WORKING_TREE_MODIFIED);
+                                       break;
+                               case "b":
+                                       assert(f.flags == (Ggit.StatusFlags.WORKING_TREE_MODIFIED |
+                                                          Ggit.StatusFlags.INDEX_MODIFIED));
+                                       break;
+                               case "c":
+                                       assert(f.flags == Ggit.StatusFlags.WORKING_TREE_DELETED);
+                                       break;
+                               }
+                       }
+
+                       loop.quit();
+               });
+
+               loop.run();
+       }
+}
+
+// ex:set ts=4 noet
diff --git a/libgitg/tests/test.vala b/libgitg/tests/test.vala
new file mode 100644
index 0000000..1363187
--- /dev/null
+++ b/libgitg/tests/test.vala
@@ -0,0 +1,54 @@
+/*
+ * This file is part of gitg
+ *
+ * Copyright (C) 2013 - Jesse van den Kieboom
+ *
+ * gitg 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.
+ *
+ * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class Gitg.Test.Test : Object
+{
+       protected void assert_noerror(GLib.Error e)
+       {
+               if (e != null)
+               {
+                       GLib.log(null,
+                                GLib.LogLevelFlags.FLAG_FATAL,
+                                "ERROR: (error != null): %s\n",
+                                e.message);
+               }
+       }
+
+       private GLib.TestSuite d_suite;
+
+       construct
+       {
+               d_suite = new GLib.TestSuite(get_type().name());
+       }
+
+       public GLib.TestSuite suite
+       {
+               get { return d_suite; }
+       }
+
+       public virtual void set_up()
+       {
+       }
+
+       public virtual void tear_down()
+       {
+       }
+}
+
+// ex:set ts=4 noet


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