-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathjava_opts_test.go
More file actions
137 lines (122 loc) · 5.78 KB
/
java_opts_test.go
File metadata and controls
137 lines (122 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package frameworks
import (
"os"
"strings"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("JavaOpts", func() {
Describe("shellSplit", func() {
DescribeTable("shell parsing",
func(input string, expected []string, shouldError bool) {
result, err := shellSplit(input)
if shouldError {
Expect(err).To(HaveOccurred())
} else {
Expect(err).NotTo(HaveOccurred())
Expect(result).To(Equal(expected))
}
},
Entry("simple space-separated", "-Xmx512M -Xms256M", []string{"-Xmx512M", "-Xms256M"}, false),
Entry("single quoted with spaces", "-DtestJBPConfig1='test test'", []string{"-DtestJBPConfig1=test test"}, false),
Entry("double quoted with spaces", `-DtestJBPConfig2="test test"`, []string{"-DtestJBPConfig2=test test"}, false),
Entry("double quoted with env var", `-DtestJBPConfig2="$PATH"`, []string{"-DtestJBPConfig2=$PATH"}, false),
Entry("mixed quotes and plain", `-DtestJBPConfig1='test test' -DtestJBPConfig2="$PATH" -Xmx512M`,
[]string{"-DtestJBPConfig1=test test", "-DtestJBPConfig2=$PATH", "-Xmx512M"}, false),
Entry("empty string", "", nil, false),
Entry("only spaces", " ", nil, false),
Entry("escaped quotes", `test\ with\ spaces`, []string{"test with spaces"}, false),
Entry("unclosed single quote", "-Dtest='unclosed", nil, true),
Entry("unclosed double quote", `-Dtest="unclosed`, nil, true),
Entry("multiple spaces between args", "-Xmx512M -Xms256M", []string{"-Xmx512M", "-Xms256M"}, false),
Entry("Ruby buildpack example", "-DtestJBPConfig1='test test' -DtestJBPConfig2=\"$PATH\"",
[]string{"-DtestJBPConfig1=test test", "-DtestJBPConfig2=$PATH"}, false),
Entry("empty single quotes", "-Dtest=''", []string{"-Dtest="}, false),
Entry("empty double quotes", `-Dtest=""`, []string{"-Dtest="}, false),
Entry("mixed quote types", "arg1='value with spaces' arg2=plain",
[]string{"arg1=value with spaces", "arg2=plain"}, false),
)
It("should preserve environment variables literally", func() {
input := `-DtestJBPConfig1='test test' -DtestJBPConfig2="$PATH"`
result, err := shellSplit(input)
Expect(err).NotTo(HaveOccurred())
expected := []string{"-DtestJBPConfig1=test test", "-DtestJBPConfig2=$PATH"}
Expect(result).To(Equal(expected))
Expect(result[1]).To(Equal("-DtestJBPConfig2=$PATH"))
})
})
Describe("rubyStyleEscape", func() {
DescribeTable("Ruby-style escaping",
func(input, expected string) {
result := rubyStyleEscape(input)
Expect(result).To(Equal(expected))
},
Entry("simple value no special chars", "-Xmx512M", "-Xmx512M"),
Entry("value with spaces", "-DtestJBPConfig1=test test", "-DtestJBPConfig1=test\\ test"),
Entry("value with equals sign", "-Dkey=value", "-Dkey=value"),
Entry("value with equals sign in value part", "-Dkey=value=something", "-Dkey=value\\=something"),
Entry("no equals sign", "-Xmx512M", "-Xmx512M"),
Entry("value with dollar sign", "-Dpath=$PATH", "-Dpath=$PATH"),
Entry("complex value with multiple spaces", "-Dprop=hello world test", "-Dprop=hello\\ world\\ test"),
Entry("path with slashes", "/usr/local/bin:/usr/bin", "/usr/local/bin:/usr/bin"),
Entry("parentheses in value", "-Dtest=(value)", "-Dtest=\\(value\\)"),
Entry("percent sign in value", "-XX:OnOutOfMemoryError=kill -9 %p", "-XX:OnOutOfMemoryError=kill\\ -9\\ \\%p"),
)
})
Describe("shellSplit and join round-trip", func() {
DescribeTable("round-trip parsing",
func(input, expected string) {
tokens, err := shellSplit(input)
Expect(err).NotTo(HaveOccurred())
result := strings.Join(tokens, " ")
Expect(result).To(Equal(expected))
},
Entry("simple values", "-Xmx512M -Xms256M", "-Xmx512M -Xms256M"),
Entry("values with spaces in quotes",
`-DtestJBPConfig1='test test' -DtestJBPConfig2="value with spaces"`,
"-DtestJBPConfig1=test test -DtestJBPConfig2=value with spaces"),
Entry("user's example from issue",
`-DtestJBPConfig1='test test' -DtestJBPConfig2="$PATH"`,
"-DtestJBPConfig1=test test -DtestJBPConfig2=$PATH"),
)
})
Describe("loadConfig", func() {
framework := &JavaOptsFramework{context: nil}
AfterEach(func() {
os.Unsetenv("JBP_CONFIG_JAVA_OPTS")
})
It("returns default config when env var is not set", func() {
config, err := framework.loadConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.FromEnvironment).To(BeTrue())
Expect(config.JavaOpts).To(BeEmpty())
})
It("parses a plain YAML map (map[string]interface{} from yaml.v3)", func() {
os.Setenv("JBP_CONFIG_JAVA_OPTS", "{from_environment: false, java_opts: [\"-Xmx512m\", \"-Xms256m\"]}")
config, err := framework.loadConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.FromEnvironment).To(BeFalse())
Expect(config.JavaOpts).To(Equal([]string{"-Xmx512m", "-Xms256m"}))
})
It("parses from_environment: true with java_opts array", func() {
os.Setenv("JBP_CONFIG_JAVA_OPTS", "{from_environment: true, java_opts: [\"-Xmx1g\"]}")
config, err := framework.loadConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.FromEnvironment).To(BeTrue())
Expect(config.JavaOpts).To(Equal([]string{"-Xmx1g"}))
})
It("parses legacy space-separated string java_opts", func() {
os.Setenv("JBP_CONFIG_JAVA_OPTS", "{from_environment: false, java_opts: \"-Xmx512m -Xms256m\"}")
config, err := framework.loadConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.JavaOpts).To(Equal([]string{"-Xmx512m", "-Xms256m"}))
})
It("parses legacy array-of-maps format ([]interface{} from yaml.v3)", func() {
os.Setenv("JBP_CONFIG_JAVA_OPTS", "[{from_environment: false}, {java_opts: [\"-Xmx256m\"]}]")
config, err := framework.loadConfig()
Expect(err).NotTo(HaveOccurred())
Expect(config.FromEnvironment).To(BeFalse())
Expect(config.JavaOpts).To(Equal([]string{"-Xmx256m"}))
})
})
})