<commit_before>
#include <locale.h>
#include <string.h>

#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>

static gboolean
cleanup_dir_recurse (GFile   *parent,
                     GFile   *root,
                     GError **error)
{
  gboolean ret = FALSE;
  GFileEnumerator *enumerator;
  GError *local_error = NULL;

  g_assert (root != NULL);

  enumerator =
    g_file_enumerate_children (parent, "*",
                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
                               &local_error);
  if (!enumerator)
    {
      if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
        {
          g_clear_error (&local_error);
          ret = TRUE;
        }
      goto out;
    }

  while (TRUE)
    {
      GFile *child;
      GFileInfo *finfo;
      char *relative_path;

      if (!g_file_enumerator_iterate (enumerator, &finfo, &child, NULL, error))
        goto out;
      if (!finfo)
        break;

      relative_path = g_file_get_relative_path (root, child);
      g_assert (relative_path != NULL);
      g_free (relative_path);

      if (g_file_info_get_file_type (finfo) == G_FILE_TYPE_DIRECTORY)
        {
          if (!cleanup_dir_recurse (child, root, error))
            goto out;
        }

      if (!g_file_delete (child, NULL, error))
        goto out;
    }

  ret = TRUE;
 out:
  g_clear_object (&enumerator);

  return ret;
}

typedef struct
{
  gchar *tmp_dir;
  gchar *config_dir;
  gchar *data_dir;
} Fixture;

static void
setup (Fixture       *fixture,
       gconstpointer  user_data)
{
  GError *local_error = NULL;

  fixture->tmp_dir = g_dir_make_tmp ("gio-test-appinfo_XXXXXX", &local_error);
  g_assert_no_error (local_error);

  fixture->config_dir = g_build_filename (fixture->tmp_dir, "config", NULL);
  g_assert_cmpint (g_mkdir (fixture->config_dir, 0755), ==, 0);

  fixture->data_dir = g_build_filename (fixture->tmp_dir, "data", NULL);
  g_assert_cmpint (g_mkdir (fixture->data_dir, 0755), ==, 0);

  g_setenv ("XDG_CONFIG_HOME", fixture->config_dir, TRUE);
  g_setenv ("XDG_DATA_HOME", fixture->data_dir, TRUE);

  g_setenv ("XDG_DATA_DIRS", "/dev/null", TRUE);
  g_setenv ("XDG_CONFIG_DIRS", "/dev/null", TRUE);
  g_setenv ("XDG_CACHE_HOME", "/dev/null", TRUE);
  g_setenv ("XDG_RUNTIME_DIR", "/dev/null", TRUE);

  g_test_message ("Using tmp directory: %s", fixture->tmp_dir);
}

static void
teardown (Fixture       *fixture,
          gconstpointer  user_data)
{
  GFile *tmp_dir = g_file_new_for_path (fixture->tmp_dir);
  GError *local_error = NULL;

  cleanup_dir_recurse (tmp_dir, tmp_dir, &local_error);
  g_assert_no_error (local_error);

  g_clear_pointer (&fixture->config_dir, g_free);
  g_clear_pointer (&fixture->data_dir, g_free);
  g_clear_pointer (&fixture->tmp_dir, g_free);
}

static void
test_launch_for_app_info (GAppInfo *appinfo)
{
  GError *error;
  GFile *file;
  GList *l;
  const gchar *path;
  gchar *uri;

  if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
    {
      g_test_skip ("No DISPLAY set");
      return;
    }

  error = NULL;
  g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
  g_assert_no_error (error);

  g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
  g_assert_no_error (error);

  path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
  file = g_file_new_for_path (path);
  l = NULL;
  l = g_list_append (l, file);

  g_assert (g_app_info_launch (appinfo, l, NULL, &error));
  g_assert_no_error (error);
  g_list_free (l);
  g_object_unref (file);

  l = NULL;
  uri = g_strconcat ("file://", g_test_get_dir (G_TEST_BUILT), "/appinfo-test.desktop", NULL);
  l = g_list_append (l, uri);
  l = g_list_append (l, "file:///etc/group#adm");

  g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
  g_assert_no_error (error);
  g_list_free (l);
  g_free (uri);
}

static void
test_launch (Fixture       *fixture,
             gconstpointer  user_data)
{
  GAppInfo *appinfo;
  const gchar *path;

  path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
  appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
  g_assert (appinfo != NULL);

  test_launch_for_app_info (appinfo);
  g_object_unref (appinfo);
}

static void
test_launch_no_app_id (Fixture       *fixture,
                       gconstpointer  user_data)
{
  const gchar desktop_file_base_contents[] =
    "[Desktop Entry]\n"
    "Type=Application\n"
    "GenericName=generic-appinfo-test\n"
    "Name=appinfo-test\n"
    "Name[de]=appinfo-test-de\n"
    "X-GNOME-FullName=example\n"
    "X-GNOME-FullName[de]=Beispiel\n"
    "Comment=GAppInfo example\n"
    "Comment[de]=GAppInfo Beispiel\n"
    "Icon=testicon.svg\n"
    "Terminal=true\n"
    "StartupNotify=true\n"
    "StartupWMClass=appinfo-class\n"
    "MimeType=image/png;image/jpeg;\n"
    "Keywords=keyword1;test keyword;\n"
    "Categories=GNOME;GTK;\n";

  gchar *exec_line_variants[2];
  gsize i;

  exec_line_variants[0] = g_strdup_printf (
      "Exec=%s/appinfo-test --option %%U %%i --name %%c --filename %%k %%m %%%%",
      g_test_get_dir (G_TEST_BUILT));
  exec_line_variants[1] = g_strdup_printf (
      "Exec=%s/appinfo-test --option %%u %%i --name %%c --filename %%k %%m %%%%",
      g_test_get_dir (G_TEST_BUILT));

  g_test_bug ("791337");

  for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
  {
    gchar *desktop_file_contents;
    GKeyFile *fake_desktop_file;
    GAppInfo *appinfo;
    gboolean loaded;

    g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);

    desktop_file_contents = g_strdup_printf ("%s\n%s",
                                             desktop_file_base_contents,
                                             exec_line_variants[i]);

    /* We load a desktop file from memory to force the app not
     * to have an app ID, which would check different codepaths.
     */
    fake_desktop_file = g_key_file_new ();
    loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
    g_assert_true (loaded);

    appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
    g_assert (appinfo != NULL);

    test_launch_for_app_info (appinfo);

    g_free (desktop_file_contents);
    g_object_unref (appinfo);
    g_key_file_unref (fake_desktop_file);
  }

  g_free (exec_line_variants[1]);
  g_free (exec_line_variants[0]);
}

static void
test_locale (const char *locale)
{
  GAppInfo *appinfo;
  gchar *orig = NULL;<commit_msg>tests: Fix indentation of a block in the appinfo test<commit_after>
#include <locale.h>
#include <string.h>

#include <glib/gstdio.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>

static gboolean
cleanup_dir_recurse (GFile   *parent,
                     GFile   *root,
                     GError **error)
{
  gboolean ret = FALSE;
  GFileEnumerator *enumerator;
  GError *local_error = NULL;

  g_assert (root != NULL);

  enumerator =
    g_file_enumerate_children (parent, "*",
                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
                               &local_error);
  if (!enumerator)
    {
      if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
        {
          g_clear_error (&local_error);
          ret = TRUE;
        }
      goto out;
    }

  while (TRUE)
    {
      GFile *child;
      GFileInfo *finfo;
      char *relative_path;

      if (!g_file_enumerator_iterate (enumerator, &finfo, &child, NULL, error))
        goto out;
      if (!finfo)
        break;

      relative_path = g_file_get_relative_path (root, child);
      g_assert (relative_path != NULL);
      g_free (relative_path);

      if (g_file_info_get_file_type (finfo) == G_FILE_TYPE_DIRECTORY)
        {
          if (!cleanup_dir_recurse (child, root, error))
            goto out;
        }

      if (!g_file_delete (child, NULL, error))
        goto out;
    }

  ret = TRUE;
 out:
  g_clear_object (&enumerator);

  return ret;
}

typedef struct
{
  gchar *tmp_dir;
  gchar *config_dir;
  gchar *data_dir;
} Fixture;

static void
setup (Fixture       *fixture,
       gconstpointer  user_data)
{
  GError *local_error = NULL;

  fixture->tmp_dir = g_dir_make_tmp ("gio-test-appinfo_XXXXXX", &local_error);
  g_assert_no_error (local_error);

  fixture->config_dir = g_build_filename (fixture->tmp_dir, "config", NULL);
  g_assert_cmpint (g_mkdir (fixture->config_dir, 0755), ==, 0);

  fixture->data_dir = g_build_filename (fixture->tmp_dir, "data", NULL);
  g_assert_cmpint (g_mkdir (fixture->data_dir, 0755), ==, 0);

  g_setenv ("XDG_CONFIG_HOME", fixture->config_dir, TRUE);
  g_setenv ("XDG_DATA_HOME", fixture->data_dir, TRUE);

  g_setenv ("XDG_DATA_DIRS", "/dev/null", TRUE);
  g_setenv ("XDG_CONFIG_DIRS", "/dev/null", TRUE);
  g_setenv ("XDG_CACHE_HOME", "/dev/null", TRUE);
  g_setenv ("XDG_RUNTIME_DIR", "/dev/null", TRUE);

  g_test_message ("Using tmp directory: %s", fixture->tmp_dir);
}

static void
teardown (Fixture       *fixture,
          gconstpointer  user_data)
{
  GFile *tmp_dir = g_file_new_for_path (fixture->tmp_dir);
  GError *local_error = NULL;

  cleanup_dir_recurse (tmp_dir, tmp_dir, &local_error);
  g_assert_no_error (local_error);

  g_clear_pointer (&fixture->config_dir, g_free);
  g_clear_pointer (&fixture->data_dir, g_free);
  g_clear_pointer (&fixture->tmp_dir, g_free);
}

static void
test_launch_for_app_info (GAppInfo *appinfo)
{
  GError *error;
  GFile *file;
  GList *l;
  const gchar *path;
  gchar *uri;

  if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
    {
      g_test_skip ("No DISPLAY set");
      return;
    }

  error = NULL;
  g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
  g_assert_no_error (error);

  g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
  g_assert_no_error (error);

  path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
  file = g_file_new_for_path (path);
  l = NULL;
  l = g_list_append (l, file);

  g_assert (g_app_info_launch (appinfo, l, NULL, &error));
  g_assert_no_error (error);
  g_list_free (l);
  g_object_unref (file);

  l = NULL;
  uri = g_strconcat ("file://", g_test_get_dir (G_TEST_BUILT), "/appinfo-test.desktop", NULL);
  l = g_list_append (l, uri);
  l = g_list_append (l, "file:///etc/group#adm");

  g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
  g_assert_no_error (error);
  g_list_free (l);
  g_free (uri);
}

static void
test_launch (Fixture       *fixture,
             gconstpointer  user_data)
{
  GAppInfo *appinfo;
  const gchar *path;

  path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
  appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
  g_assert (appinfo != NULL);

  test_launch_for_app_info (appinfo);
  g_object_unref (appinfo);
}

static void
test_launch_no_app_id (Fixture       *fixture,
                       gconstpointer  user_data)
{
  const gchar desktop_file_base_contents[] =
    "[Desktop Entry]\n"
    "Type=Application\n"
    "GenericName=generic-appinfo-test\n"
    "Name=appinfo-test\n"
    "Name[de]=appinfo-test-de\n"
    "X-GNOME-FullName=example\n"
    "X-GNOME-FullName[de]=Beispiel\n"
    "Comment=GAppInfo example\n"
    "Comment[de]=GAppInfo Beispiel\n"
    "Icon=testicon.svg\n"
    "Terminal=true\n"
    "StartupNotify=true\n"
    "StartupWMClass=appinfo-class\n"
    "MimeType=image/png;image/jpeg;\n"
    "Keywords=keyword1;test keyword;\n"
    "Categories=GNOME;GTK;\n";

  gchar *exec_line_variants[2];
  gsize i;

  exec_line_variants[0] = g_strdup_printf (
      "Exec=%s/appinfo-test --option %%U %%i --name %%c --filename %%k %%m %%%%",
      g_test_get_dir (G_TEST_BUILT));
  exec_line_variants[1] = g_strdup_printf (
      "Exec=%s/appinfo-test --option %%u %%i --name %%c --filename %%k %%m %%%%",
      g_test_get_dir (G_TEST_BUILT));

  g_test_bug ("791337");

  for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
    {
      gchar *desktop_file_contents;
      GKeyFile *fake_desktop_file;
      GAppInfo *appinfo;
      gboolean loaded;

      g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);

      desktop_file_contents = g_strdup_printf ("%s\n%s",
                                               desktop_file_base_contents,
                                               exec_line_variants[i]);

      /* We load a desktop file from memory to force the app not
       * to have an app ID, which would check different codepaths.
       */
      fake_desktop_file = g_key_file_new ();
      loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
      g_assert_true (loaded);

      appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
      g_assert (appinfo != NULL);

      test_launch_for_app_info (appinfo);

      g_free (desktop_file_contents);
      g_object_unref (appinfo);
      g_key_file_unref (fake_desktop_file);
    }

  g_free (exec_line_variants[1]);
  g_free (exec_line_variants[0]);
}

static void
test_locale (const char *locale)
{
  GAppInfo *appinfo;
  gchar *orig = NULL;
{'fraction_non_alphanumeric': 0.12754172989377846, 'fraction_numerical': 0.0039453717754172985, 'mean_word_length': 3.2574289405684755, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3175071', 'n_tokens_mistral': 5139, 'n_tokens_neox': 4939, 'n_words': 980}