github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/dep/hardware.go (about)

     1  // Copyright 2020 The ChromiumOS Authors
     2  // Use of this source code is governed by a BSD-style license that can be
     3  // found in the LICENSE file.
     4  
     5  package dep
     6  
     7  import (
     8  	"go.chromium.org/tast/core/framework/protocol"
     9  )
    10  
    11  // HardwareDeps is exported as go.chromium.org/tast/core/testing/hwdep.Deps. Please find its document for details.
    12  type HardwareDeps struct {
    13  	// conds hold a slice of HardwareConditions. The enclosing HardwareDeps instance will be satisfied
    14  	// iff all conds return true. Note that, if conds is empty, HardwareDeps is considered as
    15  	// satisfied.
    16  	conds []HardwareCondition
    17  }
    18  
    19  // HardwareCondition is exported as go.chromium.org/tast/core/testing/hwdep.Condition. Please find its document for details.
    20  // Either Satisfied or Err should be nil exclusively.
    21  type HardwareCondition struct {
    22  	// Satisfied is a pointer to a function which checks if the given HardwareFeatures satisfies
    23  	// the condition.
    24  	Satisfied func(f *protocol.HardwareFeatures) (satisfied bool, reason string, err error)
    25  
    26  	// Err is an error to be reported on Test registration
    27  	// if instantiation of HardwareCondition fails.
    28  	Err error
    29  }
    30  
    31  // NewHardwareDeps creates a HardwareDeps from a set of HWConditions.
    32  func NewHardwareDeps(conds ...HardwareCondition) HardwareDeps {
    33  	return HardwareDeps{conds: conds}
    34  }
    35  
    36  // UnsatisfiedReasons contain the detailed reasons why Satisfied failed. An empty list means success.
    37  type UnsatisfiedReasons []string
    38  
    39  // Satisfied returns whether the condition is satisfied.
    40  // UnsatisfiedReasons is empty if the given device.Config satisfies the dependencies.
    41  // i.e., the test can run on the current device setup.
    42  // A non-nil error is returned when failed to evaluate the condition.
    43  // Otherwise, the UnsatisfiedReasons instance contains a collection of reasons why any of the condition was not satisfied.
    44  func (d *HardwareDeps) Satisfied(f *protocol.HardwareFeatures) (UnsatisfiedReasons, error) {
    45  	var reasons UnsatisfiedReasons
    46  	for _, c := range d.conds {
    47  		if c.Satisfied == nil {
    48  			reasons = append(reasons, "Satisfied was nil")
    49  			continue
    50  		}
    51  		satisfied, reason, err := c.Satisfied(f)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  		if !satisfied {
    56  			if reason == "" {
    57  				reason = "(no reason given)"
    58  			}
    59  			reasons = append(reasons, reason)
    60  		}
    61  	}
    62  	return reasons, nil
    63  }
    64  
    65  // Validate returns error if one of the conditions failed to be instantiated.
    66  func (d *HardwareDeps) Validate() error {
    67  	for _, c := range d.conds {
    68  		if c.Err != nil {
    69  			return c.Err
    70  		}
    71  	}
    72  	return nil
    73  }
    74  
    75  // MergeHardwareDeps merges two HardwareDeps instance into one HardwareDeps instance.
    76  // The returned HardwareDeps is satisfied iff all conditions in d1 and ones in d2 are
    77  // satisfied.
    78  func MergeHardwareDeps(d1, d2 HardwareDeps) HardwareDeps {
    79  	var conds []HardwareCondition
    80  	conds = append(conds, d1.conds...)
    81  	conds = append(conds, d2.conds...)
    82  	return HardwareDeps{conds: conds}
    83  }