diff --git a/count/count_classes.go b/count/count_classes.go index 125a672..a070692 100644 --- a/count/count_classes.go +++ b/count/count_classes.go @@ -54,7 +54,7 @@ func JarClasses(path string) (int, error) { } } - if !strings.HasSuffix(path, ".jar") { + if !strings.HasSuffix(path, ".jar") || info.IsDir() { return nil } @@ -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() diff --git a/count/count_classes_test.go b/count/count_classes_test.go index 961135a..bdf5afd 100644 --- a/count/count_classes_test.go +++ b/count/count_classes_test.go @@ -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)) }) }