github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/cli/args/args_test.go (about) 1 package args_test 2 3 import ( 4 "reflect" 5 "testing" 6 7 tauCLI "github.com/taubyte/tau-cli/cli" 8 "github.com/taubyte/tau-cli/cli/args" 9 "github.com/urfave/cli/v2" 10 ) 11 12 type testRunner struct { 13 name string 14 testArgs []string 15 expectedArgs []string 16 app *cli.App 17 } 18 19 func TestArgs(t *testing.T) { 20 realApp, err := tauCLI.New() 21 if err != nil { 22 t.Error(err) 23 return 24 } 25 26 testCases := []testRunner{ 27 { 28 name: "global", 29 testArgs: []string{"program", "new", "function", "someFunc", "-g"}, 30 expectedArgs: []string{"program", "-g", "new", "function", "someFunc"}, 31 app: &cli.App{ 32 Flags: []cli.Flag{ 33 &cli.BoolFlag{ 34 Name: "global", 35 Aliases: []string{"g"}, 36 }, 37 }, 38 }, 39 }, 40 { 41 name: "cmd", 42 testArgs: []string{"program", "function", "someFunc", "-g", "-c", "someCommand"}, 43 expectedArgs: []string{"program", "-g", "function", "-c", "someCommand", "someFunc"}, 44 app: &cli.App{ 45 Flags: []cli.Flag{ 46 &cli.BoolFlag{ 47 Name: "global", 48 Aliases: []string{"g"}, 49 }, 50 }, 51 Commands: []*cli.Command{ 52 { 53 Name: "function", 54 Flags: []cli.Flag{ 55 &cli.StringFlag{ 56 Name: "command", 57 Aliases: []string{"c"}, 58 }, 59 }, 60 }, 61 }, 62 }, 63 }, 64 { 65 name: "sub", 66 testArgs: []string{"program", "new", "function", "someFunc", "-g", "-c", "someCommand"}, 67 expectedArgs: []string{"program", "-g", "new", "function", "-c", "someCommand", "someFunc"}, 68 app: &cli.App{ 69 Flags: []cli.Flag{ 70 &cli.BoolFlag{ 71 Name: "global", 72 Aliases: []string{"g"}, 73 }, 74 }, 75 Commands: []*cli.Command{ 76 { 77 Name: "new", 78 Subcommands: []*cli.Command{ 79 { 80 Name: "function", 81 Flags: []cli.Flag{ 82 &cli.StringFlag{ 83 Name: "command", 84 Aliases: []string{"c"}, 85 }, 86 }, 87 }, 88 }, 89 }, 90 }, 91 }, 92 }, 93 { 94 name: "sub", 95 testArgs: []string{"tau", "login", "someProfile2", "-p", "github", "-t", "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", "--new", "--color", "never"}, 96 expectedArgs: []string{"tau", "--color", "never", "login", "-p", "github", "-t", "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", "--new", "someProfile2"}, 97 app: realApp, 98 }, 99 { 100 name: "sub2", 101 testArgs: []string{"tau", "login", "--set-default", "profileName", "-t", "sometoken", "--env", "--color", "never", "--new"}, 102 expectedArgs: []string{"tau", "--env", "--color", "never", "login", "--set-default", "-t", "sometoken", "--new", "profileName"}, 103 app: realApp, 104 }, 105 { 106 name: "sub3", 107 testArgs: []string{"tau", "new", "-y", "application", "-n", "someApp", "-d", "some app desc", "-t", "some, other, tags"}, 108 expectedArgs: []string{"tau", "new", "application", "-y", "-n", "someApp", "-d", "some app desc", "-t", "some, other, tags"}, 109 app: realApp, 110 }, 111 { 112 name: "sub4", 113 testArgs: []string{"tau", "new", "application", "someApp", "-d", "some app desc"}, 114 expectedArgs: []string{"tau", "new", "application", "-d", "some app desc", "someApp"}, 115 app: realApp, 116 }, 117 { 118 name: "sub with alias", 119 testArgs: []string{"tau", "new", "-y", "app", "someApp", "-d", "some app desc"}, 120 expectedArgs: []string{"tau", "new", "app", "-y", "-d", "some app desc", "someApp"}, 121 app: realApp, 122 }, 123 { 124 name: "crazy command (example usage)", 125 testArgs: []string{"tau", "-y", "-d", "some app desc", "new", "someApp", "app"}, 126 expectedArgs: []string{"tau", "new", "app", "-y", "-d", "some app desc", "someApp"}, 127 app: realApp, 128 }, 129 { 130 name: "true attached on a bool flag parse", 131 testArgs: []string{"tau", "-env", "true", "-y", "true", "new", "someApp", "app"}, 132 expectedArgs: []string{"tau", "-env", "new", "app", "-y", "someApp"}, 133 app: realApp, 134 }, 135 { 136 name: "true attached on a bool flag parse", 137 testArgs: []string{"tau", "-y", "true", "new", "someApp", "app", "-env", "false"}, 138 139 // Note: this will not work in practice for env, as it's not a required variable 140 // It will work for variables that initialize --var and --no-var 141 expectedArgs: []string{"tau", "-no-env", "new", "app", "-y", "someApp"}, 142 app: realApp, 143 }, 144 { 145 name: "Using inverse bool flags", 146 testArgs: []string{ 147 "tau", "new", "-y", "website", 148 "-name", "someWebsite", 149 "-description", "desc", 150 "-tags", "tag1", 151 "--no-generate-repository", 152 "--paths", "/", 153 "--repository-name", "tb_website_reactdemo", 154 "--no-clone", 155 "-b", "master", 156 "--provider", "github", 157 "--domains", "hal.computers.com", 158 }, 159 expectedArgs: []string{ 160 "tau", "new", "website", "-y", 161 "-name", "someWebsite", 162 "-description", "desc", 163 "-tags", "tag1", 164 "--no-generate-repository", 165 "--paths", "/", 166 "--repository-name", "tb_website_reactdemo", 167 "--no-clone", 168 "-b", "master", 169 "--provider", "github", 170 "--domains", "hal.computers.com", 171 }, 172 app: realApp, 173 }, 174 } 175 for _, testCase := range testCases { 176 t.Run(testCase.name, func(t *testing.T) { 177 gotArgs := args.ParseArguments(testCase.app.Flags, testCase.app.Commands, testCase.testArgs...) 178 179 if len(gotArgs) != len(testCase.expectedArgs) { 180 t.Errorf("Expected %d args, got %d", len(testCase.expectedArgs), len(gotArgs)) 181 } 182 183 if !reflect.DeepEqual(gotArgs, testCase.expectedArgs) { 184 t.Errorf("\nExpected: %v\ngot : %v", testCase.expectedArgs, gotArgs) 185 } 186 }) 187 } 188 189 }