~tristan957/harvest-almanac

8ceaa01737bf3db339a8f8b9ded7c07e643b5e97 — Tristan Partin 1 year, 9 months ago 394cb4c
harvest-glib: added testing support
M CONTRIBUTING.md => CONTRIBUTING.md +16 -0
@@ 71,3 71,19 @@ flatpak-builder --verbose --sandbox --force-clean --repo=.flatpak-repo --ccache 
```text
flatpak run io.partin.tristan.HarvestAlmanac
```

### Testing

Make sure the following environment variables are set:

* `HARVEST_API_ACCESS_TOKEN`
* `HARVEST_API_CONTACT_EMAIL`
* `HARVEST_API_ACCOUNT_ID`
* `HARVEST_SOUP_MAX_CONNECTIONS`
* `HARVEST_SOUP_LOG_LEVEL`

Then run

```text
ninja test -C build
```

M harvest-glib/meson.build => harvest-glib/meson.build +4 -3
@@ 105,15 105,16 @@ harvest_glib_dep = declare_dependency(
    version: harvest_glib_version
)

if get_option('tests')
    subdir('tests')
endif

pkg.generate(
	harvest_glib,
	description: 'GLib-based library for interacting with the Harvest API.',
	filebase: 'harvest-glib-1.0',
	name: 'harvest-glib',
	version: harvest_glib_version,
    requires: [
        libsoup_dep,
    ],
    requires_private: [
        libgio_dep,
        json_glib_dep,

A harvest-glib/tests/init.h => harvest-glib/tests/init.h +29 -0
@@ 0,0 1,29 @@
#pragma once

#include <stdlib.h>

#include <json-glib/json-glib.h>
#include <libsoup/soup.h>

#include "harvest.h"

void
initialize_environment(void)
{
	char **env				  = g_get_environ();
	const char *access_token  = g_environ_getenv(env, "HARVEST_API_ACCESS_TOKEN");
	const char *contact_email = g_environ_getenv(env, "HARVEST_API_CONTACT_EMAIL");
	const char *account_id	  = g_environ_getenv(env, "HARVEST_API_ACCOUNT_ID");
	const int max_connections = atoi(g_environ_getenv(env, "HARVEST_SOUP_MAX_CONNECTIONS"));
	const int logger_level	  = atoi(g_environ_getenv(env, "HARVEST_SOUP_LOG_LEVEL"));

	g_autoptr(SoupLogger) logger = soup_logger_new(logger_level, -1);

	g_autofree char *user_agent = g_strdup_printf("Harvest Almanac (%s)", contact_email);

	g_autoptr(SoupSession) session = soup_session_new_with_options(SOUP_SESSION_MAX_CONNS,
		max_connections, SOUP_SESSION_USER_AGENT, user_agent, SOUP_SESSION_ADD_FEATURE_BY_TYPE,
		SOUP_TYPE_CONTENT_SNIFFER, SOUP_SESSION_ADD_FEATURE, SOUP_SESSION_FEATURE(logger), NULL);

	harvest_api_client_initialize(session, access_token, account_id);
}

A harvest-glib/tests/meson.build => harvest-glib/tests/meson.build +25 -0
@@ 0,0 1,25 @@
harvest_tests = {
	'user': {}
}

dependencies = [
	harvest_glib_dep,
	json_glib_dep,
	libgio_dep,
	libsoup_dep,
]

test_env = environment()
test_env.set('G_TEST_SRCDIR', meson.current_source_dir())
test_env.set('G_TEST_BUILDDIR', meson.current_build_dir())

foreach test_name, value : harvest_tests
	exe = executable(
		test_name,
		test_name + '.c',
		dependencies: dependencies,
		include_directories: include_directories('..')
	)

	test(test_name, exe, env: test_env)
endforeach

A harvest-glib/tests/user.c => harvest-glib/tests/user.c +25 -0
@@ 0,0 1,25 @@
#include <glib.h>
#include <libsoup/soup.h>

#include "harvest.h"
#include "init.h"

static void
test_harvest_get_me(void)
{
	HarvestResponse *res = harvest_user_get_me();

	g_assert_null(res->err);
}

int
main(int argc, char **argv)
{
	g_test_init(&argc, &argv, NULL);

	initialize_environment();

	g_test_add_func("/user/me", test_harvest_get_me);

	return g_test_run();
}

M meson_options.txt => meson_options.txt +2 -0
@@ 2,3 2,5 @@ option('lib-only', type: 'boolean', value: false,
	description: 'Only build harvest-glib, omitting the frontend')
option('use-system-harvest-glib', type: 'boolean', value: false,
	description: 'Use the system provided harvest-glib instead of the local copy')
option('tests', type: 'boolean', value: true,
	description: 'Build the test suite.')