Skip to content

Commit

Permalink
Merge pull request #167 from paketo-buildpacks/mem-calc-filetype
Browse files Browse the repository at this point in the history
Mem calc filetype
  • Loading branch information
Daniel Mikusa authored May 5, 2022
2 parents 2055730 + b4e2390 commit 47d31d0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
8 changes: 6 additions & 2 deletions count/count_classes.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func JarClasses(path string) (int, error) {
}
}

if !strings.HasSuffix(path, ".jar") {
if !strings.HasSuffix(path, ".jar") || info.IsDir() {
return nil
}

Expand All @@ -67,7 +67,11 @@ func JarClasses(path string) (int, error) {

z, err := zip.OpenReader(path)
if err != nil {
return fmt.Errorf("unable to open ZIP %s\n%w", path, err)
if !(errors.Is(err, zip.ErrFormat)) {
return fmt.Errorf("unable to open ZIP %s\n%w", path, err)
} else {
return nil
}
}
defer z.Close()

Expand Down
13 changes: 9 additions & 4 deletions count/count_classes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ func testCountClasses(t *testing.T, context spec.G, it spec.S) {
Expect(count.Classes(path)).To(Equal(0))
})

it("fails for empty zip/jar files without none in the name", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "test.jar"), []byte{}, 0644)).To(Succeed())
it("skips directories with .jar suffix", func() {
Expect(os.MkdirAll(filepath.Join(path, "bad-dir.jar"), 0755)).To(Succeed())

_, err := count.Classes(path)
Expect(err).To(MatchError(ContainSubstring("zip: not a valid zip file")))
Expect(count.Classes(path)).To(Equal(0))
})

it("skips bad jar files", func() {
Expect(ioutil.WriteFile(filepath.Join(path, "bad-jar.jar"), []byte{}, 0755)).To(Succeed())

Expect(count.Classes(path)).To(Equal(0))
})
}

0 comments on commit 47d31d0

Please sign in to comment.