github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/cmd/unleash_test.go (about)

     1  /*
     2   * Copyright 2022 The Gremlins Authors
     3   *
     4   *    Licensed under the Apache License, Version 2.0 (the "License");
     5   *    you may not use this file except in compliance with the License.
     6   *    You may obtain a copy of the License at
     7   *
     8   *        http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   *    Unless required by applicable law or agreed to in writing, software
    11   *    distributed under the License is distributed on an "AS IS" BASIS,
    12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *    See the License for the specific language governing permissions and
    14   *    limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/go-maxhub/gremlins/core/configuration"
    26  	"github.com/go-maxhub/gremlins/core/mutator"
    27  )
    28  
    29  func TestUnleash(t *testing.T) {
    30  	c, err := newUnleashCmd(context.Background())
    31  	if err != nil {
    32  		t.Fatal("newUnleashCmd should no fail")
    33  	}
    34  	cmd := c.cmd
    35  
    36  	if cmd.Name() != "unleash" {
    37  		t.Errorf("expected 'unleash', got %q", cmd.Name())
    38  	}
    39  
    40  	flags := cmd.Flags()
    41  
    42  	testCases := []struct {
    43  		name      string
    44  		shorthand string
    45  		flagType  string
    46  		defValue  string
    47  	}{
    48  		{
    49  			name:     "arithmetic-base",
    50  			flagType: "bool",
    51  			defValue: "true",
    52  		},
    53  		{
    54  			name:     "conditionals-boundary",
    55  			flagType: "bool",
    56  			defValue: "true",
    57  		},
    58  		{
    59  			name:     "conditionals_negation",
    60  			flagType: "bool",
    61  			defValue: "true",
    62  		},
    63  		{
    64  			name:     "coverpkg",
    65  			flagType: "string",
    66  			defValue: "",
    67  		},
    68  		{
    69  			name:      "diff",
    70  			shorthand: "D",
    71  			flagType:  "string",
    72  			defValue:  "",
    73  		},
    74  		{
    75  			name:      "dry-run",
    76  			shorthand: "d",
    77  			flagType:  "bool",
    78  			defValue:  "false",
    79  		},
    80  		{
    81  			name:     "increment-decrement",
    82  			flagType: "bool",
    83  			defValue: "true",
    84  		},
    85  		{
    86  			name:      "integration",
    87  			shorthand: "i",
    88  			flagType:  "bool",
    89  			defValue:  "false",
    90  		},
    91  		{
    92  			name:     "invert-assignments",
    93  			flagType: "bool",
    94  			defValue: "false",
    95  		},
    96  		{
    97  			name:     "invert-bitwise",
    98  			flagType: "bool",
    99  			defValue: "false",
   100  		},
   101  		{
   102  			name:     "invert-bwassign",
   103  			flagType: "bool",
   104  			defValue: "false",
   105  		},
   106  
   107  		{
   108  			name:     "invert-logical",
   109  			flagType: "bool",
   110  			defValue: "false",
   111  		},
   112  		{
   113  			name:     "invert-loopctrl",
   114  			flagType: "bool",
   115  			defValue: "false",
   116  		},
   117  		{
   118  			name:     "invert-negatives",
   119  			flagType: "bool",
   120  			defValue: "true",
   121  		},
   122  		{
   123  			name:      "output",
   124  			shorthand: "o",
   125  			flagType:  "string",
   126  			defValue:  "",
   127  		},
   128  		{
   129  			name:     "remove-self-assignments",
   130  			flagType: "bool",
   131  			defValue: "false",
   132  		},
   133  		{
   134  			name:      "tags",
   135  			shorthand: "t",
   136  			flagType:  "string",
   137  			defValue:  "",
   138  		},
   139  		{
   140  			name:     "test-cpu",
   141  			flagType: "int",
   142  			defValue: "0",
   143  		},
   144  		{
   145  			name:     "threshold-efficacy",
   146  			flagType: "float64",
   147  			defValue: "0",
   148  		},
   149  		{
   150  			name:     "threshold-mcover",
   151  			flagType: "float64",
   152  			defValue: "0",
   153  		},
   154  		{
   155  			name:     "timeout-coefficient",
   156  			flagType: "int",
   157  			defValue: "0",
   158  		},
   159  		{
   160  			name:     "workers",
   161  			flagType: "int",
   162  			defValue: "0",
   163  		},
   164  	}
   165  
   166  	for _, tc := range testCases {
   167  		tc := tc
   168  		t.Run(tc.name, func(t *testing.T) {
   169  			f := flags.Lookup(tc.name)
   170  			if f == nil {
   171  				t.Fatalf("expected flag %q to be registered", tc.name)
   172  			}
   173  			if tc.shorthand != "" && f.Shorthand != tc.shorthand {
   174  				t.Errorf("expected %q to have a shorthand %q, got %q", tc.name, tc.shorthand, f.Shorthand)
   175  			}
   176  			if f.Value.Type() != tc.flagType {
   177  				t.Errorf("expected %q to be type %q, got %q", tc.name, f.Value.Type(), f.Value.Type())
   178  			}
   179  			if f.DefValue != tc.defValue {
   180  				t.Errorf("expected %q to have default value %q, got %q", tc.name, tc.defValue, f.DefValue)
   181  			}
   182  		})
   183  	}
   184  
   185  	// test for MutantTypes flags
   186  	for _, mt := range mutator.Types {
   187  		s := strings.ToLower(mt.String())
   188  		mtf := flags.Lookup(s)
   189  		if mtf == nil {
   190  			t.Errorf("expected to have flag for mutant type: %s", mt)
   191  
   192  			continue
   193  		}
   194  
   195  		if mtf.Value.Type() != "bool" {
   196  			t.Errorf("expected %q to be a %q, got %q", s, "bool", mtf.Value.Type())
   197  		}
   198  		wantDef := fmt.Sprintf("%v", configuration.IsDefaultEnabled(mt))
   199  		if mtf.DefValue != wantDef {
   200  			t.Errorf("expected %q have default %q, got %q", s, wantDef, mtf.DefValue)
   201  		}
   202  	}
   203  }