# Copyright 2021 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Construct the drivers test binaries"""


default_config = {
    "dts_overlays": [
        here / "overlay.dts",
    ],
    "kconfig_files": [
        here / "prj.conf",
    ],
    "test_args": ["-flash={test_temp_dir}/flash.bin"],
}


def merge_dictionary(dict_1, dict_2):
    """Merge dict_1 and dict_2 and return the result"""
    dict_3 = {**dict_1, **dict_2}
    for key, value in dict_3.items():
        if key in dict_1 and key in dict_2:
            if isinstance(value, list) or isinstance(dict_1[key], list):
                dict_3[key] = value + dict_1[key]
            else:
                dict_3[key] = [value, dict_1[key]]
    return dict_3


register_host_test(
    "drivers",
    **default_config,
)

register_host_test(
    "drivers-isl923x",
    **default_config,
)

register_host_test(
    "drivers-led_driver",
    **merge_dictionary(
        default_config,
        {
            "dts_overlays": [
                here / "led_driver" / "led_pins.dts",
                here / "led_driver" / "led_policy.dts",
            ],
            "kconfig_files": [here / "led_driver" / "prj.conf"],
        },
    ),
)

@@ -208,31 +208,66 @@
 %0A%0A%0Ad
-efault_config = %7B
+rivers = register_host_test(%0A    test_name=%22drivers%22,
 %0A    
-%22
 dts_
@@ -266,35 +266,33 @@
     dts_overlays
-%22: 
+=
 %5B%0A        here /
@@ -314,25 +314,24 @@
 %0A    %5D,%0A    
-%22
 kconfig_file
@@ -331,19 +331,17 @@
 ig_files
-%22: 
+=
 %5B%0A      
@@ -372,759 +372,175 @@
  %5D,%0A
-    %22test_args%22: %5B%22-flash=%7Btest_temp_dir%7D/flash.bin%22%5D,%0A%7D%0A%0A%0Adef merge_dictionary(dict_1, dict_2):%0A    %22%22%22Merge dict_1 and dict_2 and return the result%22%22%22%0A    dict_3 = %7B**dict_1, **dict_2%7D%0A    for key, value in dict_3.items():%0A        if key in dict_1 and key in dict_2:%0A            if isinstance(value, list) or isinstance(dict_1%5Bkey%5D, list):%0A                dict_3%5Bkey%5D = value + dict_1%5Bkey%5D%0A            else:%0A                dict_3%5Bkey%5D = %5Bvalue, dict_1%5Bkey%5D%5D%0A    return dict_3%0A%0A%0Aregister_host_test(%0A    %22drivers%22,%0A    **default_config,%0A)%0A%0Aregister_host_test(%0A    %22drivers-isl923x%22,%0A    **default_config,%0A)%0A%0Aregister_host_test(%0A    %22drivers-led_driver%22,%0A    **merge_dictionary(%0A        default_config,%0A        %7B%0A            %22dts_overlays%22: %5B%0A        
+)%0A%0Aisl923x = drivers.variant(%0A    project_name=%22test-drivers-isl923x%22,%0A)%0A%0Aled_driver = drivers.variant(%0A    project_name=%22test-drivers-led_driver%22,%0A    dts_overlays=%5B%0A
     
@@ -589,24 +589,16 @@
         
-  
-      
 here / %22
@@ -637,32 +637,15 @@
     
+%5D,%0A
     
-    %5D,%0A            %22
 kcon
@@ -657,11 +657,9 @@
 iles
-%22: 
+=
 %5Bher
@@ -694,24 +694,6 @@
 %22%5D,%0A
-        %7D,%0A    ),%0A
 )%0A

Reconfigure drivers BUILD.py to use .variant