|
| 1 | +// File: cmd_test.go |
| 2 | +package engine |
| 3 | + |
| 4 | +import "testing" |
| 5 | + |
| 6 | +func TestSplitByQuotes(t *testing.T) { |
| 7 | + tests := []struct { |
| 8 | + name string |
| 9 | + input string |
| 10 | + expected []string |
| 11 | + }{ |
| 12 | + { |
| 13 | + name: "NoQuotes", |
| 14 | + input: "Hello World", |
| 15 | + expected: []string{"Hello World"}, |
| 16 | + }, |
| 17 | + { |
| 18 | + name: "ValidQuote", |
| 19 | + input: `"Hello" "World"`, |
| 20 | + expected: []string{``, `"Hello"`, ` `, `"World"`}, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "ValidQuoteWithEscape", |
| 24 | + input: `"Hello\" World"`, |
| 25 | + expected: []string{``, `"Hello\" World"`}, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Nothing", |
| 29 | + input: "", |
| 30 | + expected: []string{}, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "SpaceInsideQuote", |
| 34 | + input: `"Hello World"`, |
| 35 | + expected: []string{``, `"Hello World"`}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "SingleChar", |
| 39 | + input: "H", |
| 40 | + expected: []string{"H"}, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "SingleQuote", |
| 44 | + input: `"Hello`, |
| 45 | + expected: []string{``, ``, `"Hello`}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "ThreeQuotes", |
| 49 | + input: `Test "Hello "World" End\"`, |
| 50 | + expected: []string{`Test `, `"Hello "`, `World`, ``, `" End\"`}, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tt := range tests { |
| 55 | + t.Run(tt.name, func(t *testing.T) { |
| 56 | + got := splitByQuotes(tt.input) |
| 57 | + if !equal(got, tt.expected) { |
| 58 | + t.Errorf("splitByQuotes() = %v, want %v", got, tt.expected) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// Helper function to assert equality of two string slices. |
| 65 | +func equal(a, b []string) bool { |
| 66 | + if len(a) != len(b) { |
| 67 | + return false |
| 68 | + } |
| 69 | + for i, v := range a { |
| 70 | + if v != b[i] { |
| 71 | + return false |
| 72 | + } |
| 73 | + } |
| 74 | + return true |
| 75 | +} |
| 76 | + |
| 77 | +// Testing for replaceVariablesForInterpreter |
| 78 | +func TestReplaceVariablesForInterpreter(t *testing.T) { |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + interpreter string |
| 82 | + envMap map[string]string |
| 83 | + expected []string |
| 84 | + shouldFail bool |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "No quotes", |
| 88 | + interpreter: "/bin/bash -c ${COMMAND} tail", |
| 89 | + envMap: map[string]string{"COMMAND": "echo Hello!"}, |
| 90 | + expected: []string{"/bin/bash", "-c", "echo", "Hello!", "tail"}, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "Quotes Variables", |
| 94 | + interpreter: `/bin/bash -c "${COMMAND}" tail`, |
| 95 | + envMap: map[string]string{"COMMAND": "Hello, World!"}, |
| 96 | + expected: []string{"/bin/bash", "-c", "Hello, World!", "tail"}, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Double escape", |
| 100 | + interpreter: `/bin/bash -c "${COMMAND}" ${TWO} tail`, |
| 101 | + envMap: map[string]string{ |
| 102 | + "COMMAND": "Hello, World!", |
| 103 | + "TWO": "${COMMAND}", |
| 104 | + }, |
| 105 | + expected: []string{"/bin/bash", "-c", "Hello, World!", "${COMMAND}", "tail"}, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "aws cli issue", |
| 109 | + interpreter: "aws ${ARGS}", |
| 110 | + envMap: map[string]string{ |
| 111 | + "ARGS": `ec2 describe-instances --region us-east-1 --query 'Reservations[*].Instances[*].{Instance:InstanceId,State:State.Name}'`, |
| 112 | + }, |
| 113 | + expected: []string{ |
| 114 | + `aws`, |
| 115 | + `ec2`, |
| 116 | + `describe-instances`, |
| 117 | + `--region`, `us-east-1`, |
| 118 | + `--query`, `Reservations[*].Instances[*].{Instance:InstanceId,State:State.Name}`, |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + got, err := replaceVariablesForInterpreter(tt.interpreter, tt.envMap) |
| 126 | + if (err != nil) != tt.shouldFail { |
| 127 | + t.Errorf("replaceVariablesForInterpreter() error = %v, want %v", err, tt.shouldFail) |
| 128 | + return |
| 129 | + } |
| 130 | + if !equal(got, tt.expected) { |
| 131 | + t.Errorf("replaceVariablesForInterpreter() = %v, want %v", got, tt.expected) |
| 132 | + } |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
0 commit comments