...

Source file src/github.com/redhat-developer/odo/pkg/vars/vars_test.go

Documentation: github.com/redhat-developer/odo/pkg/vars

     1  package vars
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	"github.com/redhat-developer/odo/pkg/testingutil/filesystem"
     9  )
    10  
    11  func Test_parseKeyValueFile(t *testing.T) {
    12  	type args struct {
    13  		fileContent string
    14  		lookupEnv   func(string) (string, bool)
    15  	}
    16  	tests := []struct {
    17  		name    string
    18  		args    args
    19  		want    map[string]string
    20  		wantErr bool
    21  	}{
    22  		{
    23  			name: "no error",
    24  			args: args{
    25  				lookupEnv: func(s string) (string, bool) {
    26  					res, ok := map[string]string{
    27  						"F": "a value for F from env",
    28  					}[s]
    29  					return res, ok
    30  				},
    31  				fileContent: `A=aze
    32  # a comment
    33  
    34  B=zerty
    35  # a line beginning with spaces
    36    C=cvb
    37  # a value beginning and ending with spaces
    38    D=  dfg  
    39  
    40  # an empty value
    41  E=
    42  
    43  # a key with no value
    44  F
    45  
    46  # not defined in environment
    47  G
    48  `,
    49  			},
    50  			want: map[string]string{
    51  				"A": "aze",
    52  				"B": "zerty",
    53  				"C": "cvb",
    54  				"D": "  dfg  ",
    55  				"E": "",
    56  				"F": "a value for F from env",
    57  			},
    58  		},
    59  		{
    60  			name: "works with Windows EOL",
    61  			args: args{
    62  				lookupEnv: func(s string) (string, bool) {
    63  					res, ok := map[string]string{
    64  						"F": "a value for F from env",
    65  					}[s]
    66  					return res, ok
    67  				},
    68  				fileContent: "A=aze\r\nB=qsd",
    69  			},
    70  			want: map[string]string{
    71  				"A": "aze",
    72  				"B": "qsd",
    73  			},
    74  		},
    75  		{
    76  			name: "line without key",
    77  			args: args{
    78  				fileContent: `# a comment
    79  =aze`,
    80  			},
    81  			want:    nil,
    82  			wantErr: true,
    83  		},
    84  	}
    85  	for _, tt := range tests {
    86  		t.Run(tt.name, func(t *testing.T) {
    87  			filename := "vars.txt"
    88  			fs := filesystem.NewFakeFs()
    89  			_ = fs.WriteFile(filename, []byte(tt.args.fileContent), 0444)
    90  
    91  			got, err := parseKeyValueFile(fs, filename, tt.args.lookupEnv)
    92  			if (err != nil) != tt.wantErr {
    93  				t.Errorf("parseKeyValueFile() error = %v, wantErr %v", err, tt.wantErr)
    94  				return
    95  			}
    96  			if diff := cmp.Diff(tt.want, got); diff != "" {
    97  				t.Errorf("parseKeyValueFile() mismatch (-want +got):\n%s", diff)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func Test_parseKeyValueStrings(t *testing.T) {
   104  	type args struct {
   105  		strs      []string
   106  		lookupEnv func(string) (string, bool)
   107  	}
   108  	tests := []struct {
   109  		name    string
   110  		args    args
   111  		want    map[string]string
   112  		wantErr bool
   113  	}{
   114  		{
   115  			name: "no error",
   116  			args: args{
   117  				strs: []string{
   118  					"A=aze",
   119  					"# a comment",
   120  					"",
   121  					"B=zerty",
   122  					"# a line beginning with spaces",
   123  					"  C=cvb",
   124  					"# a value beginning and ending with spaces",
   125  					"  D=  dfg  ",
   126  					"# an empty value",
   127  					"E=",
   128  					"# a key with no value",
   129  					"F",
   130  					"# not defined in environment",
   131  					"G",
   132  				},
   133  				lookupEnv: func(s string) (string, bool) {
   134  					res, ok := map[string]string{
   135  						"F": "a value for F from env",
   136  					}[s]
   137  					return res, ok
   138  				},
   139  			},
   140  			want: map[string]string{
   141  				"A": "aze",
   142  				"B": "zerty",
   143  				"C": "cvb",
   144  				"D": "  dfg  ",
   145  				"E": "",
   146  				"F": "a value for F from env",
   147  			},
   148  		},
   149  		{
   150  			name: "string without key",
   151  			args: args{
   152  				strs: []string{
   153  					"# a comment",
   154  					"=aze",
   155  				},
   156  			},
   157  			want:    nil,
   158  			wantErr: true,
   159  		},
   160  	}
   161  	for _, tt := range tests {
   162  		t.Run(tt.name, func(t *testing.T) {
   163  			got, err := parseKeyValueStrings(tt.args.strs, tt.args.lookupEnv)
   164  			if (err != nil) != tt.wantErr {
   165  				t.Errorf("parseKeyValueStrings() error = %v, wantErr %v", err, tt.wantErr)
   166  				return
   167  			}
   168  			if diff := cmp.Diff(tt.want, got); diff != "" {
   169  				t.Errorf("parseKeyValueStrings() mismatch (-want +got):\n%s", diff)
   170  			}
   171  		})
   172  	}
   173  }
   174  
   175  func TestGetVariables(t *testing.T) {
   176  	type args struct {
   177  		fileContent string
   178  		override    []string
   179  		lookupEnv   func(string) (string, bool)
   180  	}
   181  	tests := []struct {
   182  		name    string
   183  		args    args
   184  		want    map[string]string
   185  		wantErr bool
   186  	}{
   187  		{
   188  			name: "overrides file",
   189  			args: args{
   190  				fileContent: `A=aze`,
   191  
   192  				override: []string{
   193  					"A=qsd",
   194  				},
   195  
   196  				lookupEnv: func(s string) (string, bool) {
   197  					res, ok := map[string]string{
   198  						"F": "a value for F from env",
   199  					}[s]
   200  					return res, ok
   201  				},
   202  			},
   203  			want: map[string]string{
   204  				"A": "qsd",
   205  			},
   206  		},
   207  		{
   208  			name: "no override between file and override",
   209  			args: args{
   210  				fileContent: `A=aze`,
   211  
   212  				override: []string{
   213  					"B=qsd",
   214  				},
   215  
   216  				lookupEnv: func(s string) (string, bool) {
   217  					res, ok := map[string]string{
   218  						"F": "a value for F from env",
   219  					}[s]
   220  					return res, ok
   221  				},
   222  			},
   223  			want: map[string]string{
   224  				"A": "aze",
   225  				"B": "qsd",
   226  			},
   227  		},
   228  		{
   229  			name: "override file with env var",
   230  			args: args{
   231  				fileContent: `A=aze`,
   232  
   233  				override: []string{
   234  					"A",
   235  				},
   236  
   237  				lookupEnv: func(s string) (string, bool) {
   238  					res, ok := map[string]string{
   239  						"A": "a value for A from env",
   240  					}[s]
   241  					return res, ok
   242  				},
   243  			},
   244  			want: map[string]string{
   245  				"A": "a value for A from env",
   246  			},
   247  		},
   248  		{
   249  			name: "no override file with not defined env var",
   250  			args: args{
   251  				fileContent: `A=aze`,
   252  
   253  				override: []string{
   254  					"A",
   255  				},
   256  
   257  				lookupEnv: func(s string) (string, bool) {
   258  					res, ok := map[string]string{}[s]
   259  					return res, ok
   260  				},
   261  			},
   262  			want: map[string]string{
   263  				"A": "aze",
   264  			},
   265  		},
   266  		{
   267  			name: "override file with empty defined env var",
   268  			args: args{
   269  				fileContent: `A=aze`,
   270  
   271  				override: []string{
   272  					"A",
   273  				},
   274  
   275  				lookupEnv: func(s string) (string, bool) {
   276  					res, ok := map[string]string{
   277  						"A": "",
   278  					}[s]
   279  					return res, ok
   280  				},
   281  			},
   282  			want: map[string]string{
   283  				"A": "",
   284  			},
   285  		},
   286  		{
   287  			name: "error parsing file",
   288  			args: args{
   289  				fileContent: `=aze`,
   290  			},
   291  			wantErr: true,
   292  		},
   293  		{
   294  			name: "error parsing override strings",
   295  			args: args{
   296  				fileContent: `A=aze`,
   297  				override: []string{
   298  					"=aze",
   299  				},
   300  			},
   301  			wantErr: true,
   302  		},
   303  	}
   304  	for _, tt := range tests {
   305  		t.Run(tt.name, func(t *testing.T) {
   306  			filename := "vars.txt"
   307  			fs := filesystem.NewFakeFs()
   308  			_ = fs.WriteFile(filename, []byte(tt.args.fileContent), 0444)
   309  
   310  			got, err := GetVariables(fs, filename, tt.args.override, tt.args.lookupEnv)
   311  			if (err != nil) != tt.wantErr {
   312  				t.Errorf("GetVariables() error = %v, wantErr %v", err, tt.wantErr)
   313  				return
   314  			}
   315  			if diff := cmp.Diff(tt.want, got); diff != "" {
   316  				t.Errorf("GetVariables() mismatch (-want +got):\n%s", diff)
   317  			}
   318  		})
   319  	}
   320  }
   321  
   322  func TestGetVariablesEmptyFilename(t *testing.T) {
   323  	type args struct {
   324  		override  []string
   325  		lookupEnv func(string) (string, bool)
   326  	}
   327  	tests := []struct {
   328  		name    string
   329  		args    args
   330  		want    map[string]string
   331  		wantErr bool
   332  	}{
   333  		{
   334  			name: "get override value",
   335  			args: args{
   336  				override: []string{
   337  					"A=qsd",
   338  				},
   339  
   340  				lookupEnv: func(s string) (string, bool) {
   341  					res, ok := map[string]string{
   342  						"F": "a value for F from env",
   343  					}[s]
   344  					return res, ok
   345  				},
   346  			},
   347  			want: map[string]string{
   348  				"A": "qsd",
   349  			},
   350  		},
   351  	}
   352  	for _, tt := range tests {
   353  		t.Run(tt.name, func(t *testing.T) {
   354  			got, err := GetVariables(filesystem.NewFakeFs(), "", tt.args.override, tt.args.lookupEnv)
   355  			if (err != nil) != tt.wantErr {
   356  				t.Errorf("GetVariables() error = %v, wantErr %v", err, tt.wantErr)
   357  				return
   358  			}
   359  			if diff := cmp.Diff(tt.want, got); diff != "" {
   360  				t.Errorf("GetVariables() mismatch (-want +got):\n%s", diff)
   361  			}
   362  		})
   363  	}
   364  }
   365  

View as plain text