Class: FastCov::Coverage

Inherits:
Object
  • Object
show all
Defined in:
ext/fast_cov/fast_cov.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

—- Ruby instance methods ———————————————-



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
# File 'ext/fast_cov/fast_cov.c', line 145

static VALUE fast_cov_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE opt;
  rb_scan_args(argc, argv, "01", &opt);
  if (NIL_P(opt)) opt = rb_hash_new();

  // root: defaults to Dir.pwd
  VALUE rb_root = rb_hash_lookup(opt, ID2SYM(rb_intern("root")));
  if (!RTEST(rb_root)) {
    rb_root = rb_funcall(rb_cDir, rb_intern("pwd"), 0);
  }
  Check_Type(rb_root, T_STRING);

  // ignored_paths: optional array, [] if not provided
  VALUE rb_ignored_paths =
      rb_hash_lookup(opt, ID2SYM(rb_intern("ignored_paths")));
  if (!NIL_P(rb_ignored_paths)) {
    Check_Type(rb_ignored_paths, T_ARRAY);
  }

  // threads: true (multi) or false (single), defaults to true
  VALUE rb_threads = rb_hash_lookup(opt, ID2SYM(rb_intern("threads")));
  bool threads = (rb_threads != Qfalse);

  struct fast_cov_data *data;
  TypedData_Get_Struct(self, struct fast_cov_data, &fast_cov_data_type, data);

  char *root = fast_cov_ruby_strndup(RSTRING_PTR(rb_root), RSTRING_LEN(rb_root));

  data->threads = threads;
  if (data->root) xfree(data->root);
  data->root = root;
  data->root_len = RSTRING_LEN(rb_root);

  if (data->ignored_paths) {
    long i;
    for (i = 0; i < data->ignored_paths_count; i++) {
      xfree(data->ignored_paths[i]);
    }
    xfree(data->ignored_paths);
    data->ignored_paths = NULL;
  }
  if (data->ignored_path_lens) {
    xfree(data->ignored_path_lens);
    data->ignored_path_lens = NULL;
  }
  data->ignored_paths_count = 0;

  if (!NIL_P(rb_ignored_paths) && RARRAY_LEN(rb_ignored_paths) > 0) {
    long i;
    long ignored_paths_count = RARRAY_LEN(rb_ignored_paths);

    for (i = 0; i < ignored_paths_count; i++) {
      Check_Type(rb_ary_entry(rb_ignored_paths, i), T_STRING);
    }

    data->ignored_paths_count = ignored_paths_count;
    data->ignored_paths = xcalloc(data->ignored_paths_count, sizeof(char *));
    data->ignored_path_lens = xcalloc(data->ignored_paths_count, sizeof(long));

    for (i = 0; i < data->ignored_paths_count; i++) {
      VALUE rb_ignored_path = rb_ary_entry(rb_ignored_paths, i);

      data->ignored_path_lens[i] = RSTRING_LEN(rb_ignored_path);
      data->ignored_paths[i] =
          fast_cov_ruby_strndup(RSTRING_PTR(rb_ignored_path),
                                data->ignored_path_lens[i]);
    }
  }

  return Qnil;
}

Instance Method Details

#startObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/fast_cov/fast_cov.c', line 217

static VALUE fast_cov_start(VALUE self) {
  struct fast_cov_data *data;
  TypedData_Get_Struct(self, struct fast_cov_data, &fast_cov_data_type, data);

  if (data->root_len == 0) {
    rb_raise(rb_eRuntimeError, "root is required");
  }

  if (data->started) {
    if (rb_block_given_p()) {
      rb_raise(rb_eRuntimeError, "Coverage is already started");
    }
    return self;
  }

  if (!data->threads) {
    VALUE thval = rb_thread_current();
    rb_thread_add_event_hook(thval, on_line_event, RUBY_EVENT_LINE, self);
    data->th_covered = thval;
  } else {
    rb_add_event_hook(on_line_event, RUBY_EVENT_LINE, self);
  }
  data->started = true;

  // Block form: start { ... } runs the block then returns stop result
  if (rb_block_given_p()) {
    int exception_state = 0;
    rb_protect(fast_cov_yield_block, Qnil, &exception_state);
    VALUE result = fast_cov_stop(self);
    if (exception_state != 0) {
      rb_jump_tag(exception_state);
    }
    return result;
  }

  return self;
}

#stopObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'ext/fast_cov/fast_cov.c', line 255

static VALUE fast_cov_stop(VALUE self) {
  struct fast_cov_data *data;
  TypedData_Get_Struct(self, struct fast_cov_data, &fast_cov_data_type, data);

  if (!data->started) {
    return rb_hash_new();
  }

  if (!data->threads) {
    VALUE thval = rb_thread_current();
    if (thval != data->th_covered) {
      rb_raise(rb_eRuntimeError, "Coverage was not started by this thread");
    }
    rb_thread_remove_event_hook(data->th_covered, on_line_event);
    data->th_covered = Qnil;
  } else {
    rb_remove_event_hook(on_line_event);
  }

  VALUE res = data->impacted_files;

  data->impacted_files = rb_hash_new();
  data->last_filename_ptr = 0;
  data->started = false;

  return res;
}