<commit_before># This file contains common test fixtures for registryd's tests.
# Using pytest's terminology for a test's steps:
#
# 1. Arrange: create a test fixture; this usually involves a running registry and an accessible app stub.
#
# 2. Act: manipulate the app or the registry.
#
# 3. Assert: ensure that the thing being manipulated is in the expected state.
#
# 4. Cleanup: Terminate the registry and the app.  It is important that the registry exits cleanly,
#    so it can write out its code coverage information.
#
# This module exports the following fixtures:
#
# * main_loop - a GLib.MainLoop integrated with the DBusGMainLoop.
#
# * session_manager - A mock gnome-session to control the lifetime of daemons.  In
#   reality, the fixture assumes that there is a gnome-session mock running (see
#   ci/run-registryd-tests.sh) and just tells that mock to Logout at fixture teardown
#   time, so that all daemons that monitor the session's lifetime will exit at teardown.
#
# * registry - A dbus.proxies.ProxyObject for the registry's root object.  This automatically
#   depends on a session_manager fixture to control its lifetime.

import pytest
import dbus

@pytest.fixture
def main_loop():
    from dbus.mainloop.glib import DBusGMainLoop
    from gi.repository import GLib

    DBusGMainLoop(set_as_default=True)
    loop = GLib.MainLoop()
    return loop

def get_accesssibility_bus_address():
    bus = dbus.SessionBus()
    bus_launcher = bus.get_object('org.a11y.Bus', '/org/a11y/bus')
    return str(bus_launcher.GetAddress(dbus_interface='org.a11y.Bus'))

def get_registry_root(a11y_bus):
    return a11y_bus.get_object('org.a11y.atspi.Registry', '/org/a11y/atspi/accessible/root')

@pytest.fixture
def session_manager():
    # This assumes that pytest is running in this environment:
    #
    # * A session dbus daemon is running
    #
    # * There is a gnome-session mock running
    #
    # See the ci/run-registryd-tests.sh script to see how that environment is set up.

    import time

    bus = dbus.SessionBus()
    mock_session = bus.get_object('org.gnome.SessionManager', '/org/gnome/SessionManager')

    # return a dummy object as a fixture
    yield object()

    # Tell all session clients to terminate
    mock_session.Logout(0, dbus_interface='org.gnome.SessionManager')

    # Reset mock session back to its starting state
    mock_session.Reset(dbus_interface='org.freedesktop.DBus.Mock')

    # Wait a bit for the a11y bus launcher to really die
    proxy = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
    while True:
        names = proxy.ListNames(dbus_interface='org.freedesktop.DBus')
        if 'org.a11y.Bus' in names:
            time.sleep(1)
        else:
            break

@pytest.fixture
def registry(main_loop, session_manager):
    a11y_address = get_accesssibility_bus_address()
    a11y_bus = dbus.bus.BusConnection(a11y_address)

    return get_registry_root(a11y_bus)<commit_msg>conftest.py: Inline get_registry_root() into its only caller<commit_after># This file contains common test fixtures for registryd's tests.
# Using pytest's terminology for a test's steps:
#
# 1. Arrange: create a test fixture; this usually involves a running registry and an accessible app stub.
#
# 2. Act: manipulate the app or the registry.
#
# 3. Assert: ensure that the thing being manipulated is in the expected state.
#
# 4. Cleanup: Terminate the registry and the app.  It is important that the registry exits cleanly,
#    so it can write out its code coverage information.
#
# This module exports the following fixtures:
#
# * main_loop - a GLib.MainLoop integrated with the DBusGMainLoop.
#
# * session_manager - A mock gnome-session to control the lifetime of daemons.  In
#   reality, the fixture assumes that there is a gnome-session mock running (see
#   ci/run-registryd-tests.sh) and just tells that mock to Logout at fixture teardown
#   time, so that all daemons that monitor the session's lifetime will exit at teardown.
#
# * registry - A dbus.proxies.ProxyObject for the registry's root object.  This automatically
#   depends on a session_manager fixture to control its lifetime.

import pytest
import dbus

@pytest.fixture
def main_loop():
    from dbus.mainloop.glib import DBusGMainLoop
    from gi.repository import GLib

    DBusGMainLoop(set_as_default=True)
    loop = GLib.MainLoop()
    return loop

def get_accesssibility_bus_address():
    bus = dbus.SessionBus()
    bus_launcher = bus.get_object('org.a11y.Bus', '/org/a11y/bus')
    return str(bus_launcher.GetAddress(dbus_interface='org.a11y.Bus'))

@pytest.fixture
def session_manager():
    # This assumes that pytest is running in this environment:
    #
    # * A session dbus daemon is running
    #
    # * There is a gnome-session mock running
    #
    # See the ci/run-registryd-tests.sh script to see how that environment is set up.

    import time

    bus = dbus.SessionBus()
    mock_session = bus.get_object('org.gnome.SessionManager', '/org/gnome/SessionManager')

    # return a dummy object as a fixture
    yield object()

    # Tell all session clients to terminate
    mock_session.Logout(0, dbus_interface='org.gnome.SessionManager')

    # Reset mock session back to its starting state
    mock_session.Reset(dbus_interface='org.freedesktop.DBus.Mock')

    # Wait a bit for the a11y bus launcher to really die
    proxy = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
    while True:
        names = proxy.ListNames(dbus_interface='org.freedesktop.DBus')
        if 'org.a11y.Bus' in names:
            time.sleep(1)
        else:
            break

@pytest.fixture
def registry(main_loop, session_manager):
    a11y_address = get_accesssibility_bus_address()
    a11y_bus = dbus.bus.BusConnection(a11y_address)

    return a11y_bus.get_object('org.a11y.atspi.Registry', '/org/a11y/atspi/accessible/root')
{'fraction_non_alphanumeric': 0.08466236851034951, 'fraction_numerical': 0.01017984390906006, 'mean_word_length': 3.6527229676400945, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3280664', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1816, 'n_words': 597}