Skip to content

Commit d3c9c8c

Browse files
committed
Use different approach to fix tests
Signed-off-by: egibs <20933572+egibs@users.noreply.github.com>
1 parent 53dc757 commit d3c9c8c

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

pkg/action/scan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ func processArchive(ctx context.Context, c malcontent.Config, rfs []fs.FS, archi
498498
if err != nil {
499499
return nil, fmt.Errorf("failed to determine file type: %w", err)
500500
}
501-
if ft != nil && ft.MIME == "application/octet-stream" {
501+
if ft != nil && ft.MIME == "application/x-python-joblib" {
502502
logger.Debugf("skipping unsupported archive: %s", archivePath)
503503
return nil, nil
504504
}

pkg/archive/archive.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func extractNestedArchive(
3232
if err != nil {
3333
return fmt.Errorf("failed to determine file type: %w", err)
3434
}
35-
if ft != nil && ft.MIME == "application/octet-stream" {
35+
if ft != nil && ft.MIME == "application/x-python-joblib" {
3636
return nil
3737
}
3838
if ft != nil && ft.MIME == "application/zlib" {
@@ -55,7 +55,7 @@ func extractNestedArchive(
5555
if err != nil {
5656
return fmt.Errorf("failed to determine file type: %w", err)
5757
}
58-
if ft != nil && ft.MIME == "application/octet-stream" {
58+
if ft != nil && ft.MIME == "application/x-python-joblib" {
5959
return nil
6060
}
6161
if ft != nil && ft.MIME == "application/zlib" {
@@ -109,7 +109,7 @@ func ExtractArchiveToTempDir(ctx context.Context, path string) (string, error) {
109109
if err != nil {
110110
return "", fmt.Errorf("failed to determine file type: %w", err)
111111
}
112-
if ft != nil && ft.MIME == "application/octet-stream" {
112+
if ft != nil && ft.MIME == "application/x-python-joblib" {
113113
return "", nil
114114
}
115115
if ft != nil && ft.MIME == "application/zlib" {

pkg/programkind/programkind.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ var supportedKind = map[string]string{
6666
"hh": "text/x-h",
6767
"html": "",
6868
"java": "text/x-java",
69+
"joblib": "application/x-python-joblib",
6970
"js": "application/javascript",
7071
"lnk": "application/x-ms-shortcut",
7172
"lua": "text/x-lua",
@@ -140,20 +141,6 @@ func makeFileType(path string, ext string, mime string) *FileType {
140141
return &FileType{MIME: "application/json", Ext: ext}
141142
}
142143

143-
// Capture joblib files that cannot be decompressed externally
144-
// https://joblib.readthedocs.io/en/stable/generated/joblib.dump.html
145-
joblibExts := []string{
146-
"z",
147-
"gz",
148-
"bz2",
149-
"xz",
150-
"lzma",
151-
}
152-
153-
if slices.Contains(joblibExts, ext) && mime == "application/octet-stream" {
154-
return &FileType{MIME: "application/octet-stream", Ext: ext}
155-
}
156-
157144
if supportedKind[ext] == "" {
158145
return nil
159146
}
@@ -193,8 +180,9 @@ func File(path string) (*FileType, error) {
193180
// first strategy: mimetype
194181
mtype, err := mimetype.DetectFile(path)
195182
if err == nil {
196-
ft := makeFileType(path, GetExt(path), mtype.String())
197-
return ft, nil
183+
if ft := makeFileType(path, mtype.Extension(), mtype.String()); ft != nil {
184+
return ft, nil
185+
}
198186
}
199187

200188
// second strategy: path (extension, mostly)
@@ -249,6 +237,21 @@ func File(path string) (*FileType, error) {
249237
return Path(".gzip"), nil
250238
case hdr[0] == '\x78' && hdr[1] == '\x5E':
251239
return Path(".Z"), nil
240+
// Capture joblib files that cannot be decompressed externally
241+
// https://joblib.readthedocs.io/en/stable/generated/joblib.dump.html
242+
// Check the header, file extension, and MIME type to be as specific as possible
243+
case hdr[0] == '\x5A' && hdr[1] == '\x46' && hdr[2] == '\x30' && hdr[3] == '\x78':
244+
joblibExts := []string{
245+
".z",
246+
".gz",
247+
".bz2",
248+
".xz",
249+
".lzma",
250+
}
251+
252+
if slices.Contains(joblibExts, GetExt(path)) && mtype.String() == "application/octet-stream" {
253+
return Path(".joblib"), nil
254+
}
252255
}
253256
return nil, nil
254257
}

0 commit comments

Comments
 (0)