Skip to content

Commit aef1854

Browse files
committed
fix: skip archive files when detecting packed licenses
1 parent c9ef275 commit aef1854

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

pkg/dependency/parser/java/jar/export_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ package jar
55
var (
66
EmbeddedPomGAV = embeddedPomGAV
77
DecodePomLicenses = decodePomLicenses
8+
IsJarLicenseFile = isJarLicenseFile
89
)

pkg/dependency/parser/java/jar/parse.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,9 @@ func isJarLicenseFile(name string) bool {
490490
return false
491491
}
492492
base := path.Base(name)
493+
if isArtifact(base) {
494+
return false // e.g. license.jar is a nested archive, not a license file
495+
}
493496
stem := strings.TrimSuffix(base, path.Ext(base))
494497
return licenseFileStems.Contains(stem)
495498
}

pkg/dependency/parser/java/jar/parse_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,62 @@ func TestDecodePomLicenses(t *testing.T) {
451451
})
452452
}
453453
}
454+
455+
func TestIsJarLicenseFile(t *testing.T) {
456+
tests := []struct {
457+
name string
458+
path string
459+
want bool
460+
}{
461+
{
462+
name: "LICENSE at root",
463+
path: "LICENSE",
464+
want: true,
465+
},
466+
{
467+
name: "LICENSE.txt at root",
468+
path: "LICENSE.txt",
469+
want: true,
470+
},
471+
{
472+
name: "license under META-INF",
473+
path: "META-INF/LICENSE",
474+
want: true,
475+
},
476+
{
477+
name: "copyright at root",
478+
path: "COPYRIGHT",
479+
want: true,
480+
},
481+
{
482+
name: "nested archive named license.jar",
483+
path: "license.jar",
484+
want: false,
485+
},
486+
{
487+
name: "nested archive named copyright.war under META-INF",
488+
path: "META-INF/copyright.war",
489+
want: false,
490+
},
491+
{
492+
name: "vendored license with prefix",
493+
path: "META-INF/FastDoubleParser-LICENSE",
494+
want: false,
495+
},
496+
{
497+
name: "license in subdirectory",
498+
path: "META-INF/licenses/LICENSE",
499+
want: false,
500+
},
501+
{
502+
name: "unrelated file",
503+
path: "com/example/Main.class",
504+
want: false,
505+
},
506+
}
507+
for _, tt := range tests {
508+
t.Run(tt.name, func(t *testing.T) {
509+
assert.Equal(t, tt.want, jar.IsJarLicenseFile(tt.path))
510+
})
511+
}
512+
}

0 commit comments

Comments
 (0)