github.com/sercand/please@v13.4.0+incompatible/src/build/incrementality_test.go (about)

     1  // Test to make sure that every field in BuildTarget has been thought of
     2  // in the rule hash calculation.
     3  // Not every field necessarily needs to be hashed there (and indeed not
     4  // all should be), this is just a guard against adding new fields and
     5  // forgetting to update that function.
     6  
     7  package build
     8  
     9  import (
    10  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/thought-machine/please/src/core"
    14  )
    15  
    16  var KnownFields = map[string]bool{
    17  	// These fields are explicitly hashed.
    18  	"Label":                       true,
    19  	"dependencies":                true,
    20  	"Hashes":                      true,
    21  	"Sources":                     true,
    22  	"NamedSources":                true,
    23  	"IsBinary":                    true,
    24  	"IsTest":                      true,
    25  	"IsFilegroup":                 true,
    26  	"IsHashFilegroup":             true,
    27  	"IsRemoteFile":                true,
    28  	"Command":                     true,
    29  	"Commands":                    true,
    30  	"TestCommand":                 true,
    31  	"TestCommands":                true,
    32  	"NeedsTransitiveDependencies": true,
    33  	"OptionalOutputs":             true,
    34  	"OutputIsComplete":            true,
    35  	"Requires":                    true,
    36  	"Provides":                    true,
    37  	"PreBuildFunction":            true,
    38  	"PostBuildFunction":           true,
    39  	"PreBuildHash":                true,
    40  	"PostBuildHash":               true,
    41  	"outputs":                     true,
    42  	"namedOutputs":                true,
    43  	"Licences":                    true,
    44  	"Sandbox":                     true,
    45  	"Tools":                       true,
    46  	"namedTools":                  true,
    47  	"Secrets":                     true,
    48  	"TestOutputs":                 true,
    49  	"Stamp":                       true,
    50  
    51  	// These only contribute to the runtime hash, not at build time.
    52  	"Data":              true,
    53  	"Containerise":      true,
    54  	"TestSandbox":       true,
    55  	"ContainerSettings": true,
    56  
    57  	// These would ideally not contribute to the hash, but we need that at present
    58  	// because we don't have a good way to force a recheck of its reverse dependencies.
    59  	"Visibility": true,
    60  	"TestOnly":   true,
    61  	"Labels":     true,
    62  
    63  	// These fields we have thought about and decided that they shouldn't contribute to the
    64  	// hash because they don't affect the actual output of the target.
    65  	"Subrepo":             true,
    66  	"AddedPostBuild":      true,
    67  	"Flakiness":           true,
    68  	"NoTestOutput":        true,
    69  	"BuildTimeout":        true,
    70  	"TestTimeout":         true,
    71  	"state":               true,
    72  	"Results":             true, // Recall that unsuccessful test results aren't cached...
    73  	"BuildingDescription": true,
    74  	"ShowProgress":        true,
    75  	"Progress":            true,
    76  
    77  	// Used to save the rule hash rather than actually being hashed itself.
    78  	"RuleHash": true,
    79  }
    80  
    81  func TestAllFieldsArePresentAndAccountedFor(t *testing.T) {
    82  	target := core.BuildTarget{}
    83  	val := reflect.ValueOf(target)
    84  	for i := 0; i < val.Type().NumField(); i++ {
    85  		field := val.Type().Field(i)
    86  		if !KnownFields[field.Name] {
    87  			t.Errorf("Unaccounted field in RuleHash: %s", field.Name)
    88  		}
    89  	}
    90  }