@@ -22,6 +22,7 @@ import (
22
22
"os"
23
23
"os/exec"
24
24
"path/filepath"
25
+ "reflect"
25
26
"runtime"
26
27
"strings"
27
28
"testing"
@@ -2850,6 +2851,53 @@ func betaFeaturesEnabled() bool {
2850
2851
return os .Getenv ("ENABLE_BETA" ) == "1"
2851
2852
}
2852
2853
2854
+ // isEmpty gets whether the specified object is considered empty or not.
2855
+ func isEmpty (object interface {}) bool {
2856
+ // get nil case out of the way
2857
+ if object == nil {
2858
+ return true
2859
+ }
2860
+
2861
+ objValue := reflect .ValueOf (object )
2862
+
2863
+ switch objValue .Kind () {
2864
+ // collection types are empty when they have no element
2865
+ case reflect .Chan , reflect .Map , reflect .Slice :
2866
+ return objValue .Len () == 0
2867
+ // pointers are empty if nil or if the value they point to is empty
2868
+ case reflect .Ptr :
2869
+ if objValue .IsNil () {
2870
+ return true
2871
+ }
2872
+ deref := objValue .Elem ().Interface ()
2873
+ return isEmpty (deref )
2874
+ // for all other types, compare against the zero value
2875
+ // array types are empty when they match their zero-initialized state
2876
+ default :
2877
+ zero := reflect .Zero (objValue .Type ())
2878
+ return reflect .DeepEqual (object , zero .Interface ())
2879
+ }
2880
+ }
2881
+
2882
+ // requireExactlyOneNotEmpty accepts any number of values and calls t.Fatal if
2883
+ // less or more than one is empty.
2884
+ func requireExactlyOneNotEmpty (t * testing.T , v ... any ) {
2885
+ if len (v ) == 0 {
2886
+ t .Fatal ("Expected some values for requireExactlyOneNotEmpty, but received none" )
2887
+ }
2888
+
2889
+ empty := 0
2890
+ for _ , value := range v {
2891
+ if isEmpty (value ) {
2892
+ empty += 1
2893
+ }
2894
+ }
2895
+
2896
+ if empty != len (v )- 1 {
2897
+ t .Fatalf ("Expected exactly one value to not be empty, but found %d empty values" , empty )
2898
+ }
2899
+ }
2900
+
2853
2901
// Useless key but enough to pass validation in the API
2854
2902
const testGpgArmor string = `
2855
2903
-----BEGIN PGP PUBLIC KEY BLOCK-----
0 commit comments