| Philip Zeyliger | 1dc2137 | 2025-05-05 19:54:44 +0000 | [diff] [blame] | 1 | package dockerimg |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestParseDockerArgs(t *testing.T) { |
| 9 | tests := []struct { |
| 10 | name string |
| 11 | input string |
| 12 | expected []string |
| 13 | }{ |
| 14 | { |
| 15 | name: "empty string", |
| 16 | input: "", |
| 17 | expected: []string{}, |
| 18 | }, |
| 19 | { |
| 20 | name: "single argument", |
| 21 | input: "--memory=2g", |
| 22 | expected: []string{"--memory=2g"}, |
| 23 | }, |
| 24 | { |
| 25 | name: "multiple arguments", |
| 26 | input: "--memory=2g --cpus=2", |
| 27 | expected: []string{"--memory=2g", "--cpus=2"}, |
| 28 | }, |
| 29 | { |
| 30 | name: "arguments with double quotes", |
| 31 | input: "--label=\"my label\" --env=FOO=bar", |
| 32 | expected: []string{"--label=my label", "--env=FOO=bar"}, |
| 33 | }, |
| 34 | { |
| 35 | name: "arguments with single quotes", |
| 36 | input: "--label='my label' --env=FOO=bar", |
| 37 | expected: []string{"--label=my label", "--env=FOO=bar"}, |
| 38 | }, |
| 39 | { |
| 40 | name: "nested quotes", |
| 41 | input: "--env=\"KEY=\\\"quoted value\\\"\"", |
| 42 | expected: []string{"--env=KEY=\"quoted value\""}, |
| 43 | }, |
| 44 | { |
| 45 | name: "mixed quotes", |
| 46 | input: "--env=\"mixed 'quotes'\" --label='single \"quotes\"'", |
| 47 | expected: []string{"--env=mixed 'quotes'", "--label=single \"quotes\""}, |
| 48 | }, |
| 49 | { |
| 50 | name: "escaped spaces", |
| 51 | input: "--label=my\\ label --env=FOO=bar", |
| 52 | expected: []string{"--label=my label", "--env=FOO=bar"}, |
| 53 | }, |
| 54 | { |
| 55 | name: "multiple spaces", |
| 56 | input: " --memory=2g --cpus=2 ", |
| 57 | expected: []string{"--memory=2g", "--cpus=2"}, |
| 58 | }, |
| 59 | } |
| 60 | |
| 61 | for _, tc := range tests { |
| 62 | t.Run(tc.name, func(t *testing.T) { |
| 63 | result := parseDockerArgs(tc.input) |
| 64 | if !reflect.DeepEqual(result, tc.expected) { |
| 65 | t.Errorf("Expected %v, got %v", tc.expected, result) |
| 66 | } |
| 67 | }) |
| 68 | } |
| 69 | } |