1  package context
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	"github.com/spf13/pflag"
    11  )
    12  
    13  func TestGetContextProperties(t *testing.T) {
    14  	ckey, value := "preferenceKey", "consenttelemetry"
    15  	ctx := NewContext(context.Background())
    16  	setContextProperty(ctx, ckey, value)
    17  
    18  	got := GetContextProperties(ctx)
    19  	want := map[string]interface{}{ckey: value}
    20  
    21  	if diff := cmp.Diff(want, got); diff != "" {
    22  		t.Errorf("GetContextProperties() mismatch (-want +got):\n%s", diff)
    23  	}
    24  }
    25  
    26  func TestSetComponentType(t *testing.T) {
    27  	want := "java"
    28  	for _, value := range []string{"java", "java:8", "myproject/java:8"} {
    29  		ctx := NewContext(context.Background())
    30  		SetComponentType(ctx, value)
    31  
    32  		if got, contains := GetContextProperties(ctx)[ComponentType]; !contains || got != want {
    33  			t.Errorf("component type was not set. Got: %q, Want: %q", got, want)
    34  		}
    35  	}
    36  }
    37  
    38  
    39  
    40  
    41  
    42  
    43  
    44  
    45  
    46  
    47  
    48  
    49  
    50  
    51  
    52  
    53  
    54  
    55  
    56  
    57  
    58  
    59  
    60  
    61  
    62  
    63  
    64  
    65  
    66  
    67  
    68  
    69  
    70  
    71  
    72  
    73  
    74  
    75  
    76  
    77  
    78  
    79  func TestGetTelemetryStatus(t *testing.T) {
    80  	want := true
    81  	ctx := NewContext(context.Background())
    82  	setContextProperty(ctx, TelemetryStatus, want)
    83  	got := GetTelemetryStatus(ctx)
    84  	if got != want {
    85  		t.Errorf("got: %v, want: %v", got, want)
    86  	}
    87  }
    88  
    89  func TestSetTelemetryStatus(t *testing.T) {
    90  	want := false
    91  	ctx := NewContext(context.Background())
    92  	SetTelemetryStatus(ctx, want)
    93  	got := GetContextProperties(ctx)[TelemetryStatus]
    94  	if got != want {
    95  		t.Errorf("got: %v, want: %v", got, want)
    96  	}
    97  }
    98  
    99  
   100  
   101  
   102  
   103  
   104  
   105  
   106  
   107  
   108  
   109  
   110  
   111  
   112  
   113  
   114  
   115  
   116  
   117  
   118  
   119  
   120  
   121  
   122  
   123  
   124  
   125  
   126  
   127  
   128  
   129  
   130  
   131  func TestSetFlags(t *testing.T) {
   132  	type args struct {
   133  		ctx   context.Context
   134  		flags *pflag.FlagSet
   135  	}
   136  	tests := []struct {
   137  		name string
   138  		args args
   139  		want string
   140  	}{
   141  		{
   142  			name: "no flags",
   143  			args: args{
   144  				ctx:   NewContext(context.Background()),
   145  				flags: &pflag.FlagSet{},
   146  			},
   147  			want: "",
   148  		},
   149  		{
   150  			name: "one changed flag",
   151  			args: args{
   152  				ctx: NewContext(context.Background()),
   153  				flags: func() *pflag.FlagSet {
   154  					f := &pflag.FlagSet{}
   155  					f.String("flag1", "", "")
   156  					
   157  					f.Set("flag1", "value1")
   158  					return f
   159  				}(),
   160  			},
   161  			want: "flag1",
   162  		},
   163  		{
   164  			name: "one changed flag, one unchanged flag",
   165  			args: args{
   166  				ctx: NewContext(context.Background()),
   167  				flags: func() *pflag.FlagSet {
   168  					f := &pflag.FlagSet{}
   169  					f.String("flag1", "", "")
   170  					f.String("flag2", "", "")
   171  					
   172  					f.Set("flag2", "value1")
   173  					return f
   174  				}(),
   175  			},
   176  			want: "flag2",
   177  		},
   178  		{
   179  			name: "two changed flags",
   180  			args: args{
   181  				ctx: NewContext(context.Background()),
   182  				flags: func() *pflag.FlagSet {
   183  					f := &pflag.FlagSet{}
   184  					f.String("flag1", "", "")
   185  					f.String("flag2", "", "")
   186  					
   187  					f.Set("flag1", "value1")
   188  					
   189  					f.Set("flag2", "value1")
   190  					return f
   191  				}(),
   192  			},
   193  			want: "flag1 flag2",
   194  		},
   195  	}
   196  	for _, tt := range tests {
   197  		t.Run(tt.name, func(t *testing.T) {
   198  			SetFlags(tt.args.ctx, tt.args.flags)
   199  			got := GetContextProperties(tt.args.ctx)[Flags]
   200  			if got != tt.want {
   201  				t.Errorf("SetFlags() = %v, want %v", got, tt.want)
   202  			}
   203  		})
   204  	}
   205  }
   206  
   207  func TestSetCaller(t *testing.T) {
   208  	type testScope struct {
   209  		name       string
   210  		callerType string
   211  		wantErr    bool
   212  		want       interface{}
   213  	}
   214  
   215  	tests := []testScope{
   216  		{
   217  			name:       "empty caller",
   218  			callerType: "",
   219  			want:       "",
   220  		},
   221  		{
   222  			name:       "unknown caller",
   223  			callerType: "an-unknown-caller",
   224  			wantErr:    true,
   225  			want:       "an-unknown-caller",
   226  		},
   227  		{
   228  			name:       "case-insensitive caller",
   229  			callerType: strings.ToUpper(IntelliJ),
   230  			want:       IntelliJ,
   231  		},
   232  		{
   233  			name:       "trimming space from caller",
   234  			callerType: fmt.Sprintf("   %s\t", VSCode),
   235  			want:       VSCode,
   236  		},
   237  	}
   238  	for _, c := range []string{VSCode, IntelliJ, JBoss} {
   239  		tests = append(tests, testScope{
   240  			name:       fmt.Sprintf("valid caller: %s", c),
   241  			callerType: c,
   242  			want:       c,
   243  		})
   244  	}
   245  
   246  	for _, tt := range tests {
   247  		t.Run(tt.name, func(t *testing.T) {
   248  			ctx := NewContext(context.Background())
   249  
   250  			err := SetCaller(ctx, tt.callerType)
   251  
   252  			if !tt.wantErr == (err != nil) {
   253  				t.Errorf("unexpected error %v, wantErr %v", err, tt.wantErr)
   254  			}
   255  
   256  			got := GetContextProperties(ctx)[Caller]
   257  			if got != tt.want {
   258  				t.Errorf("SetCaller() = %v, want %v", got, tt.want)
   259  			}
   260  		})
   261  	}
   262  }
   263  
View as plain text