|
| 1 | +package detect_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/cloudfoundry/java-buildpack/src/java/detect" |
| 9 | + . "github.com/onsi/ginkgo" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | +) |
| 12 | + |
| 13 | +func TestDetect(t *testing.T) { |
| 14 | + RegisterFailHandler(Fail) |
| 15 | + RunSpecs(t, "Detect Suite") |
| 16 | +} |
| 17 | + |
| 18 | +var _ = Describe("Detect", func() { |
| 19 | + var ( |
| 20 | + buildDir string |
| 21 | + detector *detect.Detector |
| 22 | + ) |
| 23 | + |
| 24 | + BeforeEach(func() { |
| 25 | + var err error |
| 26 | + buildDir, err = os.MkdirTemp("", "detect-test") |
| 27 | + Expect(err).NotTo(HaveOccurred()) |
| 28 | + |
| 29 | + detector = &detect.Detector{ |
| 30 | + BuildDir: buildDir, |
| 31 | + Version: "1.0.0", |
| 32 | + } |
| 33 | + }) |
| 34 | + |
| 35 | + AfterEach(func() { |
| 36 | + os.RemoveAll(buildDir) |
| 37 | + }) |
| 38 | + |
| 39 | + Context("when detecting servlet applications", func() { |
| 40 | + It("detects WEB-INF directory", func() { |
| 41 | + webInfDir := filepath.Join(buildDir, "WEB-INF") |
| 42 | + Expect(os.MkdirAll(webInfDir, 0755)).To(Succeed()) |
| 43 | + |
| 44 | + err := detect.Run(detector) |
| 45 | + Expect(err).NotTo(HaveOccurred()) |
| 46 | + }) |
| 47 | + |
| 48 | + It("detects WAR files", func() { |
| 49 | + warFile := filepath.Join(buildDir, "app.war") |
| 50 | + Expect(os.WriteFile(warFile, []byte("fake war"), 0644)).To(Succeed()) |
| 51 | + |
| 52 | + err := detect.Run(detector) |
| 53 | + Expect(err).NotTo(HaveOccurred()) |
| 54 | + }) |
| 55 | + }) |
| 56 | + |
| 57 | + Context("when detecting Maven applications", func() { |
| 58 | + It("detects pom.xml", func() { |
| 59 | + pomFile := filepath.Join(buildDir, "pom.xml") |
| 60 | + Expect(os.WriteFile(pomFile, []byte("<project/>"), 0644)).To(Succeed()) |
| 61 | + |
| 62 | + err := detect.Run(detector) |
| 63 | + Expect(err).NotTo(HaveOccurred()) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + Context("when detecting Gradle applications", func() { |
| 68 | + It("detects build.gradle", func() { |
| 69 | + gradleFile := filepath.Join(buildDir, "build.gradle") |
| 70 | + Expect(os.WriteFile(gradleFile, []byte("apply plugin: 'java'"), 0644)).To(Succeed()) |
| 71 | + |
| 72 | + err := detect.Run(detector) |
| 73 | + Expect(err).NotTo(HaveOccurred()) |
| 74 | + }) |
| 75 | + |
| 76 | + It("detects build.gradle.kts", func() { |
| 77 | + gradleKtsFile := filepath.Join(buildDir, "build.gradle.kts") |
| 78 | + Expect(os.WriteFile(gradleKtsFile, []byte("plugins { java }"), 0644)).To(Succeed()) |
| 79 | + |
| 80 | + err := detect.Run(detector) |
| 81 | + Expect(err).NotTo(HaveOccurred()) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + Context("when detecting JAR applications", func() { |
| 86 | + It("detects JAR files", func() { |
| 87 | + jarFile := filepath.Join(buildDir, "app.jar") |
| 88 | + Expect(os.WriteFile(jarFile, []byte("fake jar"), 0644)).To(Succeed()) |
| 89 | + |
| 90 | + err := detect.Run(detector) |
| 91 | + Expect(err).NotTo(HaveOccurred()) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + Context("when detecting Spring Boot applications", func() { |
| 96 | + It("detects BOOT-INF directory", func() { |
| 97 | + bootInfDir := filepath.Join(buildDir, "BOOT-INF") |
| 98 | + Expect(os.MkdirAll(bootInfDir, 0755)).To(Succeed()) |
| 99 | + |
| 100 | + err := detect.Run(detector) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + }) |
| 103 | + |
| 104 | + It("detects META-INF/MANIFEST.MF", func() { |
| 105 | + metaInfDir := filepath.Join(buildDir, "META-INF") |
| 106 | + Expect(os.MkdirAll(metaInfDir, 0755)).To(Succeed()) |
| 107 | + manifestFile := filepath.Join(metaInfDir, "MANIFEST.MF") |
| 108 | + Expect(os.WriteFile(manifestFile, []byte("Main-Class: com.example.Main"), 0644)).To(Succeed()) |
| 109 | + |
| 110 | + err := detect.Run(detector) |
| 111 | + Expect(err).NotTo(HaveOccurred()) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + Context("when detecting class files", func() { |
| 116 | + It("detects .class files", func() { |
| 117 | + classFile := filepath.Join(buildDir, "Main.class") |
| 118 | + Expect(os.WriteFile(classFile, []byte("fake class"), 0644)).To(Succeed()) |
| 119 | + |
| 120 | + err := detect.Run(detector) |
| 121 | + Expect(err).NotTo(HaveOccurred()) |
| 122 | + }) |
| 123 | + |
| 124 | + It("detects .class files in subdirectories", func() { |
| 125 | + subDir := filepath.Join(buildDir, "com", "example") |
| 126 | + Expect(os.MkdirAll(subDir, 0755)).To(Succeed()) |
| 127 | + classFile := filepath.Join(subDir, "Main.class") |
| 128 | + Expect(os.WriteFile(classFile, []byte("fake class"), 0644)).To(Succeed()) |
| 129 | + |
| 130 | + err := detect.Run(detector) |
| 131 | + Expect(err).NotTo(HaveOccurred()) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + Context("when detecting Groovy applications", func() { |
| 136 | + It("detects .groovy files", func() { |
| 137 | + groovyFile := filepath.Join(buildDir, "app.groovy") |
| 138 | + Expect(os.WriteFile(groovyFile, []byte("println 'hello'"), 0644)).To(Succeed()) |
| 139 | + |
| 140 | + err := detect.Run(detector) |
| 141 | + Expect(err).NotTo(HaveOccurred()) |
| 142 | + }) |
| 143 | + }) |
| 144 | + |
| 145 | + Context("when detecting Play Framework applications", func() { |
| 146 | + It("detects start script at root", func() { |
| 147 | + startScript := filepath.Join(buildDir, "start") |
| 148 | + Expect(os.WriteFile(startScript, []byte("#!/bin/bash"), 0755)).To(Succeed()) |
| 149 | + |
| 150 | + err := detect.Run(detector) |
| 151 | + Expect(err).NotTo(HaveOccurred()) |
| 152 | + }) |
| 153 | + |
| 154 | + It("detects start script in application-root", func() { |
| 155 | + appRootDir := filepath.Join(buildDir, "application-root") |
| 156 | + Expect(os.MkdirAll(appRootDir, 0755)).To(Succeed()) |
| 157 | + startScript := filepath.Join(appRootDir, "start") |
| 158 | + Expect(os.WriteFile(startScript, []byte("#!/bin/bash"), 0755)).To(Succeed()) |
| 159 | + |
| 160 | + err := detect.Run(detector) |
| 161 | + Expect(err).NotTo(HaveOccurred()) |
| 162 | + }) |
| 163 | + |
| 164 | + It("detects start script in staged-app", func() { |
| 165 | + stagedAppDir := filepath.Join(buildDir, "staged-app") |
| 166 | + Expect(os.MkdirAll(stagedAppDir, 0755)).To(Succeed()) |
| 167 | + startScript := filepath.Join(stagedAppDir, "start") |
| 168 | + Expect(os.WriteFile(startScript, []byte("#!/bin/bash"), 0755)).To(Succeed()) |
| 169 | + |
| 170 | + err := detect.Run(detector) |
| 171 | + Expect(err).NotTo(HaveOccurred()) |
| 172 | + }) |
| 173 | + }) |
| 174 | + |
| 175 | + Context("when detecting Ratpack applications", func() { |
| 176 | + It("detects ratpack-core JAR", func() { |
| 177 | + libDir := filepath.Join(buildDir, "application-root", "lib") |
| 178 | + Expect(os.MkdirAll(libDir, 0755)).To(Succeed()) |
| 179 | + ratpackJar := filepath.Join(libDir, "ratpack-core-1.5.0.jar") |
| 180 | + Expect(os.WriteFile(ratpackJar, []byte("fake jar"), 0644)).To(Succeed()) |
| 181 | + |
| 182 | + err := detect.Run(detector) |
| 183 | + Expect(err).NotTo(HaveOccurred()) |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + Context("when detecting generic Java applications", func() { |
| 188 | + It("detects application-root/lib with JARs", func() { |
| 189 | + libDir := filepath.Join(buildDir, "application-root", "lib") |
| 190 | + Expect(os.MkdirAll(libDir, 0755)).To(Succeed()) |
| 191 | + jarFile := filepath.Join(libDir, "app.jar") |
| 192 | + Expect(os.WriteFile(jarFile, []byte("fake jar"), 0644)).To(Succeed()) |
| 193 | + |
| 194 | + err := detect.Run(detector) |
| 195 | + Expect(err).NotTo(HaveOccurred()) |
| 196 | + }) |
| 197 | + }) |
| 198 | + |
| 199 | + Context("when detecting dist-zip applications", func() { |
| 200 | + It("detects bin/ and lib/ directories at root", func() { |
| 201 | + binDir := filepath.Join(buildDir, "bin") |
| 202 | + libDir := filepath.Join(buildDir, "lib") |
| 203 | + Expect(os.MkdirAll(binDir, 0755)).To(Succeed()) |
| 204 | + Expect(os.MkdirAll(libDir, 0755)).To(Succeed()) |
| 205 | + |
| 206 | + // Create a non-.bat script in bin/ |
| 207 | + startScript := filepath.Join(binDir, "start") |
| 208 | + Expect(os.WriteFile(startScript, []byte("#!/bin/bash"), 0755)).To(Succeed()) |
| 209 | + |
| 210 | + err := detect.Run(detector) |
| 211 | + Expect(err).NotTo(HaveOccurred()) |
| 212 | + }) |
| 213 | + |
| 214 | + It("detects bin/ and lib/ directories in application-root", func() { |
| 215 | + appRoot := filepath.Join(buildDir, "application-root") |
| 216 | + binDir := filepath.Join(appRoot, "bin") |
| 217 | + libDir := filepath.Join(appRoot, "lib") |
| 218 | + Expect(os.MkdirAll(binDir, 0755)).To(Succeed()) |
| 219 | + Expect(os.MkdirAll(libDir, 0755)).To(Succeed()) |
| 220 | + |
| 221 | + // Create a non-.bat script in bin/ |
| 222 | + startScript := filepath.Join(binDir, "start") |
| 223 | + Expect(os.WriteFile(startScript, []byte("#!/bin/bash"), 0755)).To(Succeed()) |
| 224 | + |
| 225 | + err := detect.Run(detector) |
| 226 | + Expect(err).NotTo(HaveOccurred()) |
| 227 | + }) |
| 228 | + |
| 229 | + It("ignores .bat files in bin/ directory", func() { |
| 230 | + binDir := filepath.Join(buildDir, "bin") |
| 231 | + libDir := filepath.Join(buildDir, "lib") |
| 232 | + Expect(os.MkdirAll(binDir, 0755)).To(Succeed()) |
| 233 | + Expect(os.MkdirAll(libDir, 0755)).To(Succeed()) |
| 234 | + |
| 235 | + // Create only .bat script in bin/ |
| 236 | + batScript := filepath.Join(binDir, "start.bat") |
| 237 | + Expect(os.WriteFile(batScript, []byte("@echo off"), 0644)).To(Succeed()) |
| 238 | + |
| 239 | + err := detect.Run(detector) |
| 240 | + Expect(err).To(HaveOccurred()) |
| 241 | + }) |
| 242 | + }) |
| 243 | + |
| 244 | + Context("when detecting Procfile", func() { |
| 245 | + It("detects Procfile with content", func() { |
| 246 | + procfile := filepath.Join(buildDir, "Procfile") |
| 247 | + Expect(os.WriteFile(procfile, []byte("web: java -jar app.jar"), 0644)).To(Succeed()) |
| 248 | + |
| 249 | + err := detect.Run(detector) |
| 250 | + Expect(err).NotTo(HaveOccurred()) |
| 251 | + }) |
| 252 | + |
| 253 | + It("fails when Procfile is empty", func() { |
| 254 | + procfile := filepath.Join(buildDir, "Procfile") |
| 255 | + Expect(os.WriteFile(procfile, []byte(""), 0644)).To(Succeed()) |
| 256 | + |
| 257 | + err := detect.Run(detector) |
| 258 | + Expect(err).To(HaveOccurred()) |
| 259 | + }) |
| 260 | + }) |
| 261 | + |
| 262 | + Context("when no Java application is detected", func() { |
| 263 | + It("returns an error", func() { |
| 264 | + err := detect.Run(detector) |
| 265 | + Expect(err).To(HaveOccurred()) |
| 266 | + Expect(err.Error()).To(ContainSubstring("no Java app detected")) |
| 267 | + }) |
| 268 | + }) |
| 269 | +}) |
0 commit comments