# Buggy code
def _available_services():
    """
    This is a helper function needed for testing. We are using the memoziation
    decorator on the `available_services` function, which causes the function
    to run once and then return the results of the first run on subsequent
    calls. This causes problems when trying to test the functionality of the
    `available_services` function.
    """
    launchd_paths = [
        "/Library/LaunchAgents",
        "/Library/LaunchDaemons",
        "/System/Library/LaunchAgents",
        "/System/Library/LaunchDaemons",
    ]
    _available_services = dict()
    for launch_dir in launchd_paths:
        for root, dirs, files in salt.utils.path.os_walk(launch_dir):
            for file_name in files:
                # Must be a plist file
                if not file_name.endswith(".plist"):
                    continue

                # Follow symbolic links of files in _launchd_paths
                file_path = os.path.join(root, file_name)
                true_path = os.path.realpath(file_path)

                # ignore broken symlinks
                if not os.path.exists(true_path):
                    continue

                try:
                    # This assumes most of the plist files
                    # will be already in XML format
                    plist = plistlib.readPlist(true_path)

                except Exception:
                    # If plistlib is unable to read the file we'll need to use
                    # the system provided plutil program to do the conversion
                    cmd = '/usr/bin/plutil -convert xml1 -o - -- "{0}"'.format(
                        true_path
                    )
                    plist_xml = salt.modules.cmdmod.run(cmd, output_loglevel="quiet")
                    if six.PY2:
                        plist = plistlib.readPlistFromString(plist_xml)
                    else:
                        plist = plistlib.loads(
                            salt.utils.stringutils.to_bytes(plist_xml)
                        )

                try:
                    _available_services[plist.Label.lower()] = {
                        "file_name": file_name,
                        "file_path": true_path,
                        "plist": plist,
                    }
                except AttributeError:
                    # Handle malformed plist files
                    _available_services[os.path.basename(file_name).lower()] = {
                        "file_name": file_name,
                        "file_path": true_path,
                        "plist": plist,
                    }

    return _available_services


# Traceback (NameError)
Traceback (most recent call last):
File "/testing/scripts/salt-call", line 11, in <module>
salt_call()
File "/testing/salt/scripts.py", line 400, in salt_call
client.run()
File "/testing/salt/cli/call.py", line 57, in run
caller.run()
File "/testing/salt/cli/caller.py", line 134, in run
ret = self.call()
File "/testing/salt/cli/caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "/testing/salt/modules/mac_service.py", line 130, in launchctl
return salt.utils.mac_utils.launchctl(sub_cmd, *args, **kwargs)
File "/testing/salt/utils/mac_utils.py", line 270, in launchctl
ret = salt.modules.cmdmod.run_all(cmd, **kwargs)
File "/testing/salt/modules/cmdmod.py", line 1834, in run_all
**kwargs)
File "/testing/salt/modules/cmdmod.py", line 357, in _run
if not _check_avail(cmd):
File "/testing/salt/modules/cmdmod.py", line 230, in _check_avail
if __salt__['config.get']('cmd_blacklist_glob'):
NameError: global name '__salt__' is not defined

# Fixed code
def _available_services():
    """
    This is a helper function needed for testing. We are using the memoziation
    decorator on the `available_services` function, which causes the function
    to run once and then return the results of the first run on subsequent
    calls. This causes problems when trying to test the functionality of the
    `available_services` function.
    """
    launchd_paths = [
        "/Library/LaunchAgents",
        "/Library/LaunchDaemons",
        "/System/Library/LaunchAgents",
        "/System/Library/LaunchDaemons",
    ]
    _available_services = dict()
    for launch_dir in launchd_paths:
        for root, dirs, files in salt.utils.path.os_walk(launch_dir):
            for file_name in files:
                # Must be a plist file
                if not file_name.endswith(".plist"):
                    continue

                # Follow symbolic links of files in _launchd_paths
                file_path = os.path.join(root, file_name)
                true_path = os.path.realpath(file_path)

                # ignore broken symlinks
                if not os.path.exists(true_path):
                    continue

                try:
                    # This assumes most of the plist files
                    # will be already in XML format
                    plist = plistlib.readPlist(true_path)

                except Exception:
                    # If plistlib is unable to read the file we'll need to use
                    # the system provided plutil program to do the conversion
                    cmd = '/usr/bin/plutil -convert xml1 -o - -- "{0}"'.format(
                        true_path
                    )
                    plist_xml = __salt__["cmd.run"](cmd)
                    if six.PY2:
                        plist = plistlib.readPlistFromString(plist_xml)
                    else:
                        plist = plistlib.loads(
                            salt.utils.stringutils.to_bytes(plist_xml)
                        )

                try:
                    _available_services[plist.Label.lower()] = {
                        "file_name": file_name,
                        "file_path": true_path,
                        "plist": plist,
                    }
                except AttributeError:
                    # Handle malformed plist files
                    _available_services[os.path.basename(file_name).lower()] = {
                        "file_name": file_name,
                        "file_path": true_path,
                        "plist": plist,
                    }

    return _available_services