# Path: catkin_tools/jobs/utils.py
# def merge_envs(job_env, overlay_envs):
#     """
#     In the merged/linked case of single env, this function amounts to a straight
#     assignment, but a more complex merge is required with isolated result spaces,
#     since a package's build environment may require extending that of multiple
#     other result spaces.
#     """
#     merge_path_values = {}
# 
#     for overlay_env in overlay_envs:
#         for key, values_str in overlay_env.items():
#             if key.endswith('PATH'):
#                 if key not in merge_path_values:
#                     # Seed the list with any values already in the environment. We reverse the list
#                     # here so that we can cheaply append to it, representing a prepend in the final
#                     # PATH var, and because we need to maintain the order of underlay paths.
#                     if key in job_env:
#                         values = job_env[key].split(os.pathsep)
#                         values.reverse()
#                         merge_path_values[key] = values
#                     else:
#                         merge_path_values[key] = []
#                 merge_path_values[key].extend(values_str.split(os.pathsep))
#             else:
#                 # For non-PATH keys, simply assign the value. This may not always
#                 # be correct behaviour, but we don't have the information here to
#                 # know how to do anything else.
#                 job_env[key] = values_str
# 
#     # For the path values, do a deduplicating merge.
#     for key, values_list in merge_path_values.items():
#         seen_values = set()
#         new_values_list = []
#         for value in values_list:
#             if value not in seen_values:
#                 seen_values.add(value)
#                 new_values_list.append(value)
#         job_env[key] = os.pathsep.join(reversed(new_values_list))


from catkin_tools.jobs.utils import merge_envs


def test_merge_envs_basic():
    job_env = {'PATH': '/usr/local/bin:/usr/bin', 'FOO': 'foo'}

    merge_envs(job_env, [
        {'PATH': '/usr/local/bin:/bar/baz/bin'},
        {'BAR': 'bar'}])

    # Validate that the known path was not moved from the existing order, and the unfamiliar
    # path was correctly prepended.
    assert job_env['PATH'] == '/bar/baz/bin:/usr/local/bin:/usr/bin'

    # Confirm that a key only in the original env persists.
    assert job_env['FOO'] == 'foo'

    # Confirm that a new key is added.
    assert job_env['BAR'] == 'bar'


def test_merge_envs_complex():
    """Confirm that merged paths are deduplicated and that order is maintained."""
    job_env = {'PATH': 'C:B:A'}

# Path: catkin_tools/jobs/utils.py
# def merge_envs(job_env, overlay_envs):
#     """
#     In the merged/linked case of single env, this function amounts to a straight
#     assignment, but a more complex merge is required with isolated result spaces,
#     since a package's build environment may require extending that of multiple
#     other result spaces.
#     """
#     merge_path_values = {}
# 
#     for overlay_env in overlay_envs:
#         for key, values_str in overlay_env.items():
#             if key.endswith('PATH'):
#                 if key not in merge_path_values:
#                     # Seed the list with any values already in the environment. We reverse the list
#                     # here so that we can cheaply append to it, representing a prepend in the final
#                     # PATH var, and because we need to maintain the order of underlay paths.
#                     if key in job_env:
#                         values = job_env[key].split(os.pathsep)
#                         values.reverse()
#                         merge_path_values[key] = values
#                     else:
#                         merge_path_values[key] = []
#                 merge_path_values[key].extend(values_str.split(os.pathsep))
#             else:
#                 # For non-PATH keys, simply assign the value. This may not always
#                 # be correct behaviour, but we don't have the information here to
#                 # know how to do anything else.
#                 job_env[key] = values_str
# 
#     # For the path values, do a deduplicating merge.
#     for key, values_list in merge_path_values.items():
#         seen_values = set()
#         new_values_list = []
#         for value in values_list:
#             if value not in seen_values:
#                 seen_values.add(value)
#                 new_values_list.append(value)
#         job_env[key] = os.pathsep.join(reversed(new_values_list))


# Path: tests/unit/test_jobs.py
from catkin_tools.jobs.utils import merge_envs



def test_merge_envs_basic():
    job_env = {'PATH': '/usr/local/bin:/usr/bin', 'FOO': 'foo'}

    merge_envs(job_env, [
        {'PATH': '/usr/local/bin:/bar/baz/bin'},
        {'BAR': 'bar'}])

    # Validate that the known path was not moved from the existing order, and the unfamiliar
    # path was correctly prepended.
    assert job_env['PATH'] == '/bar/baz/bin:/usr/local/bin:/usr/bin'

    # Confirm that a key only in the original env persists.
    assert job_env['FOO'] == 'foo'

    # Confirm that a new key is added.
    assert job_env['BAR'] == 'bar'


def test_merge_envs_complex():
    """Confirm that merged paths are deduplicated and that order is maintained."""
    job_env = {'PATH': 'C:B:A'}

    merge_envs(job_env, [{'PATH': 'D:C'}, {'PATH': 'E:A:C'}])