@@ -10,98 +10,108 @@ import (
1010 "github.com/cloudfoundry/java-buildpack/src/java/containers"
1111)
1212
13- var _ = Describe ("HasMainMethod" , func () {
14- DescribeTable ("detecting main method in Groovy files" ,
15- func (content string , expected bool ) {
16- tmpFile , err := os .CreateTemp ("" , "test-*.groovy" )
17- Expect (err ).NotTo (HaveOccurred ())
18- defer os .Remove (tmpFile .Name ())
13+ // groovyFile writes content to a temp file and returns its path.
14+ // The file is removed when the current spec ends.
15+ func groovyFile (content string ) string {
16+ tmpFile , err := os .CreateTemp ("" , "test-*.groovy" )
17+ Expect (err ).NotTo (HaveOccurred ())
18+ DeferCleanup (os .Remove , tmpFile .Name ())
19+ _ , err = tmpFile .WriteString (content )
20+ Expect (err ).NotTo (HaveOccurred ())
21+ Expect (tmpFile .Close ()).To (Succeed ())
22+ return tmpFile .Name ()
23+ }
24+
25+ var _ = Describe ("GroovyUtils" , func () {
26+ var g * containers.GroovyUtils
1927
20- _ , err = tmpFile . WriteString ( content )
21- Expect ( err ). NotTo ( HaveOccurred ())
22- tmpFile . Close ( )
28+ BeforeEach ( func () {
29+ g = & containers. GroovyUtils {}
30+ } )
2331
24- result , err := containers .HasMainMethod (tmpFile .Name ())
25- Expect (err ).NotTo (HaveOccurred ())
26- Expect (result ).To (Equal (expected ))
27- },
28- Entry ("has static void main" , `class MyApp {
32+ Describe ("HasMainMethod" , func () {
33+ DescribeTable ("detecting main method in Groovy files" ,
34+ func (content string , expected bool ) {
35+ Expect (g .HasMainMethod (groovyFile (content ))).To (Equal (expected ))
36+ },
37+ Entry ("has static void main" , `class MyApp {
2938 static void main(String[] args) {
3039 println "Hello"
3140 }
3241}` , true ),
33- Entry ("has static void main with whitespace variations " , `class MyApp {
42+ Entry ("has static void main with extra whitespace " , `class MyApp {
3443 static void main ( String[] args ) {
3544 println "Hello"
3645 }
3746}` , true ),
38- Entry ("no main method" , `class Alpha {
47+ Entry ("no main method" , `class Alpha {
3948}` , false ),
40- Entry ("simple script no main" , `println 'Hello World'` , false ),
41- Entry ("instance method not static main" , `class Test {
49+ Entry ("simple script no main" , `println 'Hello World'` , false ),
50+ Entry ("instance method not static main" , `class Test {
4251 void main() {
4352 println "Not static"
4453 }
4554}` , false ),
46- )
47- })
48-
49- var _ = Describe ("IsPOGO" , func () {
50- DescribeTable ("detecting Plain Old Groovy Objects" ,
51- func (content string , expected bool ) {
52- tmpFile , err := os .CreateTemp ("" , "test-*.groovy" )
53- Expect (err ).NotTo (HaveOccurred ())
54- defer os .Remove (tmpFile .Name ())
55+ )
5556
56- _ , err = tmpFile .WriteString (content )
57- Expect (err ).NotTo (HaveOccurred ())
58- tmpFile .Close ()
57+ It ("returns false for an unreadable file" , func () {
58+ Expect (g .HasMainMethod ("/nonexistent/file.groovy" )).To (BeFalse ())
59+ })
60+ })
5961
60- result , err := containers .IsPOGO (tmpFile .Name ())
61- Expect (err ).NotTo (HaveOccurred ())
62- Expect (result ).To (Equal (expected ))
63- },
64- Entry ("simple class definition" , `class Alpha {
62+ Describe ("IsPOGO" , func () {
63+ DescribeTable ("detecting Plain Old Groovy Objects" ,
64+ func (content string , expected bool ) {
65+ Expect (g .IsPOGO (groovyFile (content ))).To (Equal (expected ))
66+ },
67+ Entry ("simple class definition" , `class Alpha {
6568}` , true ),
66- Entry ("class with inheritance" , `class MyApp extends BaseApp {
69+ Entry ("class with inheritance" , `class MyApp extends BaseApp {
6770 void run() {}
6871}` , true ),
69- Entry ("simple script no class" , `println 'Hello World'` , false ),
70- Entry ("script with variables no class" , `def name = "World"
72+ Entry ("simple script no class" , `println 'Hello World'` , false ),
73+ Entry ("script with variables no class" , `def name = "World"
7174println "Hello $name"` , false ),
72- Entry ("class keyword in comment" , `// This is not a class
75+ Entry ("class keyword in comment" , `// This is not a class
7376println 'Hello'` , false ),
74- Entry ("class keyword in string" , `println "This mentions class but isn't one"` , false ),
75- )
76- })
77-
78- var _ = Describe ("HasShebang" , func () {
79- DescribeTable ("detecting shebang in Groovy files" ,
80- func (content string , expected bool ) {
81- tmpFile , err := os .CreateTemp ("" , "test-*.groovy" )
82- Expect (err ).NotTo (HaveOccurred ())
83- defer os .Remove (tmpFile .Name ())
77+ Entry ("class keyword in string" , `println "This mentions class but isn't one"` , false ),
78+ )
8479
85- _ , err = tmpFile .WriteString (content )
86- Expect (err ).NotTo (HaveOccurred ())
87- tmpFile .Close ()
80+ It ("returns false for an unreadable file" , func () {
81+ Expect (g .IsPOGO ("/nonexistent/file.groovy" )).To (BeFalse ())
82+ })
83+ })
8884
89- result , err := containers .HasShebang (tmpFile .Name ())
90- Expect (err ).NotTo (HaveOccurred ())
91- Expect (result ).To (Equal (expected ))
92- },
93- Entry ("has shebang" , `#!/usr/bin/env groovy
94- println 'Hello World'` , true ),
95- Entry ("has groovy shebang" , `#!/usr/bin/groovy
96- println 'Hello'` , true ),
97- Entry ("no shebang" , `class Alpha {
85+ Describe ("HasShebang" , func () {
86+ DescribeTable ("detecting shebang in Groovy files" ,
87+ func (content string , expected bool ) {
88+ Expect (g .HasShebang (groovyFile (content ))).To (Equal (expected ))
89+ },
90+ Entry ("has shebang" , "#!/usr/bin/env groovy\n println 'Hello World'" , true ),
91+ Entry ("has groovy shebang" , "#!/usr/bin/groovy\n println 'Hello'" , true ),
92+ Entry ("no shebang" , `class Alpha {
9893}` , false ),
99- Entry ("shebang not at start" , `
100- #!/usr/bin/env groovy
94+ Entry ("shebang not at start" , " \n #!/usr/bin/env groovy \n println 'Hello'" , false ),
95+ Entry ( "comment mentioning shebang" , `// Use #!/usr/bin/env groovy at the top
10196println 'Hello'` , false ),
102- Entry ("comment mentioning shebang" , `// Use #!/usr/bin/env groovy at the top
103- println 'Hello'` , false ),
104- )
97+ )
98+
99+ It ("returns false for an unreadable file" , func () {
100+ Expect (g .HasShebang ("/nonexistent/file.groovy" )).To (BeFalse ())
101+ })
102+ })
103+
104+ Describe ("IsBeans" , func () {
105+ DescribeTable ("detecting beans-style configuration" ,
106+ func (content string , expected bool ) {
107+ Expect (g .IsBeans (groovyFile (content ))).To (Equal (expected ))
108+ },
109+ Entry ("has beans block" , `beans {
110+ bean(MyBean)
111+ }` , true ),
112+ Entry ("no beans block" , `class Alpha {}` , false ),
113+ )
114+ })
105115})
106116
107117var _ = Describe ("FindMainGroovyScript" , func () {
@@ -117,32 +127,24 @@ var _ = Describe("FindMainGroovyScript", func() {
117127 os .RemoveAll (tmpDir )
118128 })
119129
130+ writeFile := func (name , content string ) string {
131+ path := filepath .Join (tmpDir , name )
132+ Expect (os .WriteFile (path , []byte (content ), 0644 )).To (Succeed ())
133+ return path
134+ }
135+
120136 Context ("with various Groovy script types" , func () {
121137 var pogoFile , nonPogoFile , mainMethodFile , shebangFile string
122138
123139 BeforeEach (func () {
124- var err error
125-
126- pogoFile = filepath .Join (tmpDir , "Alpha.groovy" )
127- err = os .WriteFile (pogoFile , []byte ("class Alpha {}" ), 0644 )
128- Expect (err ).NotTo (HaveOccurred ())
129-
130- nonPogoFile = filepath .Join (tmpDir , "Application.groovy" )
131- err = os .WriteFile (nonPogoFile , []byte ("println 'Hello World'" ), 0644 )
132- Expect (err ).NotTo (HaveOccurred ())
133-
134- mainMethodFile = filepath .Join (tmpDir , "Main.groovy" )
135- mainContent := `class Main {
140+ pogoFile = writeFile ("Alpha.groovy" , "class Alpha {}" )
141+ nonPogoFile = writeFile ("Application.groovy" , "println 'Hello World'" )
142+ mainMethodFile = writeFile ("Main.groovy" , `class Main {
136143 static void main(String[] args) {
137144 println "Main"
138145 }
139- }`
140- err = os .WriteFile (mainMethodFile , []byte (mainContent ), 0644 )
141- Expect (err ).NotTo (HaveOccurred ())
142-
143- shebangFile = filepath .Join (tmpDir , "Script.groovy" )
144- err = os .WriteFile (shebangFile , []byte ("#!/usr/bin/env groovy\n println 'Script'" ), 0644 )
145- Expect (err ).NotTo (HaveOccurred ())
146+ }` )
147+ shebangFile = writeFile ("Script.groovy" , "#!/usr/bin/env groovy\n println 'Script'" )
146148 })
147149
148150 DescribeTable ("finding the main Groovy script" ,
@@ -176,17 +178,11 @@ var _ = Describe("FindMainGroovyScript", func() {
176178 })
177179
178180 Context ("with invalid files" , func () {
179- It ("should skip invalid files and select valid ones" , func () {
180- invalidFile := filepath .Join (tmpDir , "invalid.groovy" )
181- err := os .WriteFile (invalidFile , []byte {0xff , 0xfe }, 0644 )
182- Expect (err ).NotTo (HaveOccurred ())
183-
184- validFile := filepath .Join (tmpDir , "valid.groovy" )
185- err = os .WriteFile (validFile , []byte ("println 'Hello'" ), 0644 )
186- Expect (err ).NotTo (HaveOccurred ())
181+ It ("skips invalid files and selects the valid one" , func () {
182+ invalidFile := writeFile ("invalid.groovy" , string ([]byte {0xff , 0xfe }))
183+ validFile := writeFile ("valid.groovy" , "println 'Hello'" )
187184
188- scripts := []string {invalidFile , validFile }
189- result , err := containers .FindMainGroovyScript (scripts )
185+ result , err := containers .FindMainGroovyScript ([]string {invalidFile , validFile })
190186 Expect (err ).NotTo (HaveOccurred ())
191187 Expect (result ).To (Equal (validFile ))
192188 })
0 commit comments