github.com/nya3jp/tast@v0.0.0-20230601000426-85c8e4d83a9b/src/go.chromium.org/tast/core/internal/testcontext/entity.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 testcontext provides logic to extract information from context.
     6  package testcontext
     7  
     8  import (
     9  	"context"
    10  )
    11  
    12  // currentEntityKey is the type of the key used for attaching a CurrentEntity
    13  // to a context.Context.
    14  type currentEntityKey struct{}
    15  
    16  // CurrentEntity contains information about the currently running entity.
    17  //
    18  // Information in this struct is accessible from anywhere via context.Context
    19  // and testing.Context* functions. Each member should have strong reason to be
    20  // accessible without testing.*State.
    21  type CurrentEntity struct {
    22  	// OutDir is a directory where the current entity can save output files.
    23  	OutDir string
    24  	// HasSoftwareDeps indicates if software dependencies are available for the
    25  	// current entity. It is true only for tests.
    26  	HasSoftwareDeps bool
    27  	// SoftwareDeps is a list of software dependencies declared in the current entity.
    28  	// TODO(b/229939530): Will add support for multi-DUT dependency in future.
    29  	SoftwareDeps []string
    30  	// ServiceDeps is a list of service dependencies declared in the current entity.
    31  	ServiceDeps []string
    32  	// PrivateAttr is a list of private attributes declared in the current entity.
    33  	PrivateAttr []string
    34  }
    35  
    36  // WithCurrentEntity attaches CurrentEntity to context.Context. This function can't
    37  // be called from user code.
    38  func WithCurrentEntity(ctx context.Context, ec *CurrentEntity) context.Context {
    39  	return context.WithValue(ctx, currentEntityKey{}, ec)
    40  }
    41  
    42  // OutDir is similar to testing.State.OutDir but takes context instead. It is intended to be
    43  // used by packages providing support for tests that need to write files.
    44  func OutDir(ctx context.Context) (dir string, ok bool) {
    45  	ec, ok := ctx.Value(currentEntityKey{}).(*CurrentEntity)
    46  	if !ok || ec.OutDir == "" {
    47  		return "", false
    48  	}
    49  	return ec.OutDir, true
    50  }
    51  
    52  // SoftwareDeps is similar to testing.State.SoftwareDeps but takes context instead.
    53  // It is intended to be used by packages providing support for tests that want to
    54  // make sure tests declare proper dependencies.
    55  func SoftwareDeps(ctx context.Context) ([]string, bool) {
    56  	ec, ok := ctx.Value(currentEntityKey{}).(*CurrentEntity)
    57  	if !ok {
    58  		return nil, false
    59  	}
    60  	if !ec.HasSoftwareDeps {
    61  		return nil, false
    62  	}
    63  	return append([]string(nil), ec.SoftwareDeps...), true
    64  }
    65  
    66  // ServiceDeps is similar to testing.State.ServiceDeps but takes context instead.
    67  // It is intended to be used by packages providing support for tests that want to
    68  // make sure tests declare proper dependencies.
    69  func ServiceDeps(ctx context.Context) ([]string, bool) {
    70  	ec, ok := ctx.Value(currentEntityKey{}).(*CurrentEntity)
    71  	if !ok {
    72  		return nil, false
    73  	}
    74  	return append([]string(nil), ec.ServiceDeps...), true
    75  }
    76  
    77  // EnsurePrivateAttr ensures the current entity declares a privateAttr in its metadata.
    78  // Otherwise it will panic.
    79  func EnsurePrivateAttr(ctx context.Context, name string) {
    80  	ec, ok := ctx.Value(currentEntityKey{}).(*CurrentEntity)
    81  	if !ok {
    82  		panic("Context is not associated with an entity")
    83  	}
    84  	for _, s := range ec.PrivateAttr {
    85  		if s == name {
    86  			return
    87  		}
    88  	}
    89  	panic("Expected privateAttr " + name + " not found in the entity")
    90  }
    91  
    92  // PrivateAttr returns the private attributes of current entity.
    93  func PrivateAttr(ctx context.Context) (privateAttr []string, ok bool) {
    94  	ec, ok := ctx.Value(currentEntityKey{}).(*CurrentEntity)
    95  	if !ok {
    96  		return nil, ok
    97  	}
    98  	return ec.PrivateAttr, ok
    99  }