github.com/clusterize-io/tusk@v0.6.3-0.20211001020217-cfe8a8cd0d4a/appcli/help_test.go (about) 1 package appcli 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/clusterize-io/tusk/runner" 8 ) 9 10 var flagPrefixerTests = []struct { 11 flags string 12 placeholder string 13 expected string 14 }{ 15 {"a", "", "-a"}, 16 {"a", "foo", "-a <foo>"}, 17 {"aa", "", " --aa"}, 18 {"aa", "foo", " --aa <foo>"}, 19 {"a, aa", "", "-a, --aa"}, 20 {"aa, a", "", "-a, --aa"}, 21 {"a, aa", "foo", "-a, --aa <foo>"}, 22 } 23 24 func TestFlagPrefixer(t *testing.T) { 25 for _, tt := range flagPrefixerTests { 26 actual := flagPrefixer(tt.flags, tt.placeholder) 27 if tt.expected != actual { 28 t.Errorf( 29 "flagPrefixer(%q, %q): expected %q, got %q", 30 tt.flags, tt.placeholder, tt.expected, actual, 31 ) 32 } 33 } 34 } 35 36 var argsSectionTests = []struct { 37 desc string 38 taskCfg string 39 expected string 40 }{ 41 { 42 "no args", 43 "", 44 "", 45 }, 46 { 47 "one args", 48 "foo: {usage: 'some usage'}", 49 ` 50 51 Arguments: 52 foo some usage`, 53 }, 54 { 55 "args without usage", 56 "foo: {}, bar: {}", 57 ` 58 59 Arguments: 60 foo 61 bar`, 62 }, 63 { 64 "args with usage", 65 "foo: {usage: 'some usage'}, bar: {usage: 'other usage'}", 66 ` 67 68 Arguments: 69 foo some usage 70 bar other usage`, 71 }, 72 { 73 "variable length arguments", 74 "a: {usage: 'some usage'}, aaaaa: {usage: 'other usage'}", 75 ` 76 77 Arguments: 78 a some usage 79 aaaaa other usage`, 80 }, 81 } 82 83 func TestCreateArgsSection(t *testing.T) { 84 for _, tt := range argsSectionTests { 85 taskName := "someTaskName" 86 t.Run(tt.desc, func(t *testing.T) { 87 cfgText := fmt.Sprintf("tasks: { %s: { args: {%s} } }", taskName, tt.taskCfg) 88 cfg, err := runner.Parse([]byte(cfgText)) 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 actual := createArgsSection(cfg.Tasks[taskName]) 94 if tt.expected != actual { 95 t.Errorf( 96 "want %q, got %q", tt.expected, actual, 97 ) 98 } 99 }) 100 } 101 }