github.com/clusterize-io/tusk@v0.6.3-0.20211001020217-cfe8a8cd0d4a/runner/dependencies_test.go (about)

     1  package runner
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  var findalloptionstests = []struct {
     8  	desc                string
     9  	taskOptions         []Option
    10  	cfgOptions          []Option
    11  	expectedTaskIndices []int
    12  	expectedCfgIndices  []int
    13  }{
    14  	{
    15  		"no dependencies",
    16  		[]Option{},
    17  		[]Option{
    18  			createOption(
    19  				withOptionName("foo"),
    20  			),
    21  		},
    22  		[]int{},
    23  		[]int{},
    24  	},
    25  	{
    26  		"fake dependencies",
    27  		[]Option{
    28  			createOption(
    29  				withOptionName("foo"),
    30  				withOptionDependency("fake"),
    31  			),
    32  		},
    33  		[]Option{},
    34  		[]int{0},
    35  		[]int{},
    36  	},
    37  	{
    38  		"multiple dependencies per option",
    39  		[]Option{
    40  			createOption(
    41  				withOptionName("foo"),
    42  				withOptionDependency("one"),
    43  				withOptionDependency("two"),
    44  				withOptionDependency("three"),
    45  			),
    46  		},
    47  		[]Option{
    48  			createOption(
    49  				withOptionName("one"),
    50  			),
    51  			createOption(
    52  				withOptionName("two"),
    53  			),
    54  			createOption(
    55  				withOptionName("three"),
    56  			),
    57  			createOption(
    58  				withOptionName("wrong"),
    59  			),
    60  		},
    61  		[]int{0},
    62  		[]int{0, 1, 2},
    63  	},
    64  	{
    65  		"only task dependencies",
    66  		[]Option{
    67  			createOption(
    68  				withOptionName("foo"),
    69  			),
    70  			createOption(
    71  				withOptionName("bar"),
    72  				withOptionDependency("foo"),
    73  			),
    74  		},
    75  		[]Option{},
    76  		[]int{0, 1},
    77  		[]int{},
    78  	},
    79  	{
    80  		"overridden global dependencies",
    81  		[]Option{
    82  			createOption(
    83  				withOptionName("foo"),
    84  			),
    85  			createOption(
    86  				withOptionName("bar"),
    87  				withOptionDependency("foo"),
    88  			),
    89  		},
    90  		[]Option{
    91  			createOption(
    92  				withOptionName("foo"),
    93  			),
    94  		},
    95  		[]int{0, 1},
    96  		[]int{},
    97  	},
    98  	{
    99  		"when dependencies",
   100  		[]Option{
   101  			createOption(
   102  				withOptionName("foo"),
   103  			),
   104  			createOption(
   105  				withOptionName("bar"),
   106  				withOptionWhenDependency("foo"),
   107  			),
   108  		},
   109  		[]Option{},
   110  		[]int{0, 1},
   111  		[]int{},
   112  	},
   113  	{
   114  		"task requires global",
   115  		[]Option{
   116  			createOption(
   117  				withOptionName("bar"),
   118  				withOptionDependency("foo"),
   119  			),
   120  		},
   121  		[]Option{
   122  			createOption(
   123  				withOptionName("foo"),
   124  			),
   125  		},
   126  		[]int{0},
   127  		[]int{0},
   128  	},
   129  	{
   130  		"global requires task (false positive)",
   131  		[]Option{
   132  			createOption(
   133  				withOptionName("foo"),
   134  			),
   135  		},
   136  		[]Option{
   137  			createOption(
   138  				withOptionName("bar"),
   139  				withOptionDependency("foo"),
   140  			),
   141  		},
   142  		[]int{0},
   143  		[]int{},
   144  	},
   145  	{
   146  		"nested depdendencies",
   147  		[]Option{
   148  			createOption(
   149  				withOptionName("foo"),
   150  				withOptionDependency("bar"),
   151  			),
   152  			createOption(
   153  				withOptionName("bar"),
   154  				withOptionDependency("baz"),
   155  			),
   156  		},
   157  		[]Option{
   158  			createOption(
   159  				withOptionName("baz"),
   160  				withOptionDependency("qux"),
   161  			),
   162  			createOption(
   163  				withOptionName("qux"),
   164  			),
   165  		},
   166  		[]int{0, 1},
   167  		[]int{0, 1},
   168  	},
   169  	{
   170  		"nested depdendencies with ignored globals",
   171  		[]Option{
   172  			createOption(
   173  				withOptionName("foo"),
   174  				withOptionDependency("bar"),
   175  			),
   176  			createOption(
   177  				withOptionName("bar"),
   178  				withOptionDependency("baz"),
   179  			),
   180  		},
   181  		[]Option{
   182  			createOption(
   183  				withOptionName("qux"),
   184  			),
   185  			createOption(
   186  				withOptionName("skiptwo"),
   187  			),
   188  			createOption(
   189  				withOptionName("baz"),
   190  				withOptionDependency("qux"),
   191  			),
   192  			createOption(
   193  				withOptionName("skipone"),
   194  				withOptionDependency("skiptwo"),
   195  			),
   196  		},
   197  		[]int{0, 1},
   198  		[]int{0, 2},
   199  	},
   200  }
   201  
   202  func TestFindAllOptions(t *testing.T) {
   203  	for _, tt := range findalloptionstests {
   204  		tsk := Task{}
   205  		for i := range tt.taskOptions {
   206  			tsk.Options = append(tsk.Options, &tt.taskOptions[i])
   207  		}
   208  
   209  		cfg := Config{}
   210  		for i := range tt.cfgOptions {
   211  			cfg.Options = append(cfg.Options, &tt.cfgOptions[i])
   212  		}
   213  
   214  		actual, err := FindAllOptions(&tsk, &cfg)
   215  		if err != nil {
   216  			t.Errorf(
   217  				"FindAllOptions() for %s: unexpected error: %s",
   218  				tt.desc, err,
   219  			)
   220  			continue
   221  		}
   222  
   223  		var expected []*Option
   224  		for _, i := range tt.expectedTaskIndices {
   225  			expected = append(expected, &tt.taskOptions[i])
   226  		}
   227  		for _, i := range tt.expectedCfgIndices {
   228  			expected = append(expected, &tt.cfgOptions[i])
   229  		}
   230  
   231  		assertOptionsEqualUnordered(t, tt.desc, expected, actual)
   232  	}
   233  }
   234  
   235  func assertOptionsEqualUnordered(t *testing.T, desc string, a, b []*Option) {
   236  	t.Helper()
   237  
   238  	if len(a) != len(b) {
   239  		t.Errorf(
   240  			"options for %s: expected %d options, actual %d",
   241  			desc, len(a), len(b),
   242  		)
   243  		return
   244  	}
   245  
   246  	bMap := make(map[*Option]interface{})
   247  	for _, val := range b {
   248  		bMap[val] = struct{}{}
   249  	}
   250  
   251  	for _, item := range a {
   252  		if _, ok := bMap[item]; !ok {
   253  			t.Errorf(
   254  				"options for %s: expected item %s not in actual list",
   255  				desc, item.Name,
   256  			)
   257  			continue
   258  		}
   259  	}
   260  }