File: tests.vala

package info (click to toggle)
minder 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,624 kB
  • sloc: xml: 799; sh: 104; python: 87; ansic: 78; makefile: 5; tcl: 4
file content (237 lines) | stat: -rw-r--r-- 6,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright (c) 2014 gnome-pomodoro contributors
 *
 * This code is partly borrowed from libgee's test suite,
 * at https://git.gnome.org/browse/libgee and from gnome-break-timer
 * https://git.gnome.org/browse/gnome-break-timer
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

using GLib;

namespace MinderTest {

  public delegate void TestCaseFunc();

  public GLib.SettingsSchemaSource schema_source;

  private class TestSuiteAdaptor {

    public string name;

    private TestCaseFunc func;
    private TestSuite    test_suite;

    public TestSuiteAdaptor( string name, owned TestCaseFunc test_case_func, TestSuite test_suite ) {
      this.name = name;
      this.func = (owned) test_case_func;
      this.test_suite = test_suite;
    }

    public void setup( void* fixture ) {
      this.test_suite.setup();
    }

    public void run( void* fixture ) {
      this.func();
    }

    public void teardown( void* fixture ) {
      this.test_suite.teardown();
    }

    public GLib.TestCase get_g_test_case () {
      return new GLib.TestCase( this.name, this.setup, this.run, this.teardown );
    }
  }

  public abstract class TestSuite : GLib.Object {

    private GLib.TestSuite     g_test_suite;
    private TestSuiteAdaptor[] adaptors = new TestSuiteAdaptor[0];

    construct {
      this.g_test_suite = new GLib.TestSuite( this.get_name() );
    }

    public string get_name() {
      return this.get_type().name();
    }

    public GLib.TestSuite get_g_test_suite() {
      return this.g_test_suite;
    }

    public void add_test( string name, owned TestCaseFunc func ) {
      var adaptor = new TestSuiteAdaptor( name, (owned) func, this );
      this.adaptors += adaptor;
      this.g_test_suite.add( adaptor.get_g_test_case() );
    }

    public virtual void setup() {}

    public virtual void teardown() {}

  }

  public class TestRunner : GLib.Object {

    private GLib.TestSuite root_suite;
    private GLib.File      tmp_dir;
    private const string   SCHEMA_FILE_NAME = "com.github.phase1geo.minder.gschema.xml";

    public TestRunner( GLib.TestSuite? root_suite = null ) {
      if( root_suite == null ) {
        this.root_suite = GLib.TestSuite.get_root();
      } else {
        this.root_suite = root_suite;
      }
    }

    public void add( TestSuite test_suite ) {
      this.root_suite.add_suite( test_suite.get_g_test_suite() );
    }

    private void setup_settings () {

      Environment.set_variable( "GSETTINGS_BACKEND", "memory", true );
      Environment.set_variable( "GSETTINGS_SCHEMA_DIR", this.tmp_dir.get_path (), true );

      // prepare temporary settings
      var target_schema_path = this.tmp_dir.get_path();

      try {
        var top_builddir       = TestRunner.get_top_builddir();
        var source_schema_file = GLib.File.new_for_path( Path.build_filename( top_builddir, "data", SCHEMA_FILE_NAME ) );
        var target_schema_file = GLib.File.new_for_path( Path.build_filename( target_schema_path, SCHEMA_FILE_NAME ) );
        source_schema_file.copy( target_schema_file, GLib.FileCopyFlags.OVERWRITE );
      } catch( GLib.Error error ) {
        GLib.error( "Error copying schema file: %s", error.message );
      }

      var compile_schemas_result = 0;
      try {
        GLib.Process.spawn_command_line_sync(
          "glib-compile-schemas %s".printf (target_schema_path),
          null,
          null,
          out compile_schemas_result
        );
      } catch( GLib.SpawnError error ) {
        GLib.error( error.message );
      }

      if( compile_schemas_result != 0 ) {
        GLib.error( "Could not compile schemas '%s'.", target_schema_path );
      }

    }

    public virtual void global_setup () {

      Environment.set_variable( "LANGUAGE", "C", true );

      try {
        this.tmp_dir = GLib.File.new_for_path( GLib.DirUtils.make_tmp( "minder-test-XXXXXX" ) );
      } catch( GLib.Error error ) {
        GLib.error( "Error creating temporary directory for test files: %s".printf( error.message ) );
      }

      this.setup_settings();

    }

    public virtual void global_teardown () {

      if( this.tmp_dir != null ) {
        var tmp_dir_path = this.tmp_dir.get_path();
        var delete_tmp_result = 0;

        try {
          GLib.Process.spawn_command_line_sync(
            "rm -rf %s".printf (tmp_dir_path),
            null,
            null,
            out delete_tmp_result
          );
        } catch( GLib.SpawnError error ) {
          GLib.warning( error.message );
        }

        if( delete_tmp_result != 0 ) {
          GLib.warning( "Could not delete temporary directory '%s'", tmp_dir_path );
        }
      }

    }

    public int run() {

      global_setup();

      var exit_status = GLib.Test.run();

      global_teardown();

      return( exit_status );

    }

    private static string get_top_builddir () {

      var builddir = Environment.get_variable( "top_builddir" );

      if( builddir == null ) {
        var dir = GLib.File.new_for_path( Environment.get_current_dir() );
        while( dir != null ) {
          var schema_path = GLib.Path.build_filename( dir.get_path(), "data", SCHEMA_FILE_NAME );

          if( GLib.FileUtils.test( schema_path, GLib.FileTest.IS_REGULAR ) ) {
            builddir = dir.get_path();
            break;
          }

          dir = dir.get_parent();
        }
      }

      if (builddir == null) {
        builddir = "..";  /* fallback to parent dir, test should be ran
                             from 'tests' dir */
      }

      return builddir;
    }

  }

}


public static int main (string[] args) {

  var exit_status = 0;

  Test.init( ref args );

  var tests = new MinderTest.TestRunner ();
  tests.add( new MinderTest.ExampleTest() );

  exit_status = tests.run();

  return( exit_status );

}