github.com/opendevstack/tailor@v1.3.5-0.20220119161809-cab064e60a67/pkg/cli/options_test.go (about)

     1  package cli
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  	"github.com/opendevstack/tailor/internal/test/helper"
     8  	"github.com/opendevstack/tailor/pkg/utils"
     9  )
    10  
    11  func TestResolvedFile(t *testing.T) {
    12  	tests := map[string]struct {
    13  		fileFlag      string
    14  		namespaceFlag string
    15  		fs            utils.FileStater
    16  		expected      string
    17  	}{
    18  		"no file flag and no namespace flag given": {
    19  			fileFlag:      "Tailorfile", // default
    20  			namespaceFlag: "",
    21  			fs:            &helper.SomeFilesExistFS{Existing: []string{"Tailorfile"}},
    22  			expected:      "Tailorfile",
    23  		},
    24  		"no file flag given but namespace flag given and namespaced file exists": {
    25  			fileFlag:      "Tailorfile", // default
    26  			namespaceFlag: "foo",
    27  			fs:            &helper.SomeFilesExistFS{Existing: []string{"Tailorfile.foo"}},
    28  			expected:      "Tailorfile.foo",
    29  		},
    30  		"no file flag given but namespace flag given and namespaced file does not exist": {
    31  			fileFlag:      "Tailorfile", // default
    32  			namespaceFlag: "foo",
    33  			fs:            &helper.SomeFilesExistFS{},
    34  			expected:      "Tailorfile",
    35  		},
    36  		"file flag given and no namespace flag given": {
    37  			fileFlag:      "mytailorfile",
    38  			namespaceFlag: "",
    39  			fs:            &helper.SomeFilesExistFS{Existing: []string{"mytailorfile"}},
    40  			expected:      "mytailorfile",
    41  		},
    42  		"file flag and namespace flag given": {
    43  			fileFlag:      "mytailorfile",
    44  			namespaceFlag: "foo",
    45  			fs:            &helper.SomeFilesExistFS{Existing: []string{"mytailorfile"}},
    46  			expected:      "mytailorfile",
    47  		},
    48  	}
    49  	for name, tc := range tests {
    50  		t.Run(name, func(t *testing.T) {
    51  			o := InitGlobalOptions(tc.fs)
    52  			o.File = tc.fileFlag
    53  			actual := o.resolvedFile(tc.namespaceFlag)
    54  			if actual != tc.expected {
    55  				t.Fatalf("Expected file: '%s', got: '%s'", tc.expected, actual)
    56  			}
    57  		})
    58  	}
    59  }
    60  
    61  func TestNewCompareOptionsExcludes(t *testing.T) {
    62  	tests := map[string]struct {
    63  		excludeFlag  []string
    64  		wantExcludes []string
    65  	}{
    66  		"none": {
    67  			excludeFlag:  []string{},
    68  			wantExcludes: []string{},
    69  		},
    70  		"passed once": {
    71  			excludeFlag:  []string{"bc"},
    72  			wantExcludes: []string{"bc"},
    73  		},
    74  		"passed once comma-separated": {
    75  			excludeFlag:  []string{"bc,is"},
    76  			wantExcludes: []string{"bc", "is"},
    77  		},
    78  		"passed multiple times": {
    79  			excludeFlag:  []string{"bc", "is"},
    80  			wantExcludes: []string{"bc", "is"},
    81  		},
    82  		"passed multiple times and comma-separated": {
    83  			excludeFlag:  []string{"bc,is", "route"},
    84  			wantExcludes: []string{"bc", "is", "route"},
    85  		},
    86  	}
    87  	for name, tc := range tests {
    88  		t.Run(name, func(t *testing.T) {
    89  			o, err := NewGlobalOptions(false, "Tailorfile", false, false, false, "oc", false)
    90  			if err != nil {
    91  				t.Fatal(err)
    92  			}
    93  			got, err := NewCompareOptions(
    94  				o,
    95  				"",
    96  				"",
    97  				tc.excludeFlag,
    98  				".",
    99  				".",
   100  				"",
   101  				"",
   102  				"",
   103  				"",
   104  				[]string{},
   105  				[]string{},
   106  				[]string{},
   107  				false,
   108  				false,
   109  				false,
   110  				false,
   111  				false,
   112  				false,
   113  				"")
   114  			if err != nil {
   115  				t.Fatal(err)
   116  			}
   117  			if diff := cmp.Diff(tc.wantExcludes, got.Excludes); diff != "" {
   118  				t.Errorf("Compare options mismatch (-want +got):\n%s", diff)
   119  			}
   120  		})
   121  	}
   122  }
   123  
   124  func TestNewExportOptionsExcludes(t *testing.T) {
   125  	tests := map[string]struct {
   126  		excludeFlag  []string
   127  		wantExcludes []string
   128  	}{
   129  		"none": {
   130  			excludeFlag:  []string{},
   131  			wantExcludes: []string{},
   132  		},
   133  		"passed once": {
   134  			excludeFlag:  []string{"bc"},
   135  			wantExcludes: []string{"bc"},
   136  		},
   137  		"passed once comma-separated": {
   138  			excludeFlag:  []string{"bc,is"},
   139  			wantExcludes: []string{"bc", "is"},
   140  		},
   141  		"passed multiple times": {
   142  			excludeFlag:  []string{"bc", "is"},
   143  			wantExcludes: []string{"bc", "is"},
   144  		},
   145  		"passed multiple times and comma-separated": {
   146  			excludeFlag:  []string{"bc,is", "route"},
   147  			wantExcludes: []string{"bc", "is", "route"},
   148  		},
   149  	}
   150  	for name, tc := range tests {
   151  		t.Run(name, func(t *testing.T) {
   152  			o, err := NewGlobalOptions(false, "Tailorfile", false, false, false, "oc", false)
   153  			if err != nil {
   154  				t.Fatal(err)
   155  			}
   156  			got, err := NewExportOptions(
   157  				o,
   158  				"",
   159  				"",
   160  				tc.excludeFlag,
   161  				".",
   162  				".",
   163  				false,
   164  				false,
   165  				[]string{},
   166  				"")
   167  			if err != nil {
   168  				t.Fatal(err)
   169  			}
   170  			if diff := cmp.Diff(tc.wantExcludes, got.Excludes); diff != "" {
   171  				t.Errorf("Export options mismatch (-want +got):\n%s", diff)
   172  			}
   173  		})
   174  	}
   175  }