Skip to content

Commit

Permalink
feat: test for unsupported pkg manager
Browse files Browse the repository at this point in the history
  • Loading branch information
musdotdigital committed Nov 30, 2023
1 parent 13cdecb commit cff1066
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion __tests__/data/InvalidDockerfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
RUN
FROM centos:latest
2 changes: 1 addition & 1 deletion __tests__/dockerfile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {load} from '../src/dockerfile'
test('load invalid dockerfile', async () => {
let dockerfilePath = path.join(__dirname, 'data', 'InvalidDockerfile')
await expect(load(dockerfilePath)).rejects.toThrow(
'Unable to extract image from Dockerfile'
'Unable to find supported package manager'
)
})

Expand Down
12 changes: 7 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ function load(dockerfile) {
return __awaiter(this, void 0, void 0, function* () {
const content = fs_1.default.readFileSync(dockerfile).toString('utf-8');
const extractedImage = extract_docker_image(content);
yield extractedImage.init_package_manager();
try {
yield extractedImage.init_package_manager();
}
catch (error) {
return Promise.reject(error);
}
return extractedImage;
});
}
Expand All @@ -86,11 +91,8 @@ function extract_docker_image(dockerfile_content) {
if (line.includes('FROM')) {
imageName = line.split(' ')[1].trim();
}
if (line.includes('apk add') || line.includes('apt-get install')) {
return new image.Image(imageName);
}
}
throw Error('Unable to extract image from Dockerfile');
return new image.Image(imageName);
}


Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions src/dockerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import fs from 'fs'
export async function load(dockerfile: string): Promise<image.Image> {
const content = fs.readFileSync(dockerfile).toString('utf-8')
const extractedImage = extract_docker_image(content)
await extractedImage.init_package_manager()
try {
await extractedImage.init_package_manager()
} catch (error) {
return Promise.reject(error)
}
return extractedImage
}

Expand All @@ -15,9 +19,6 @@ function extract_docker_image(dockerfile_content: string): image.Image {
if (line.includes('FROM')) {
imageName = line.split(' ')[1].trim()
}
if (line.includes('apk add') || line.includes('apt-get install')) {
return new image.Image(imageName)
}
}
throw Error('Unable to extract image from Dockerfile')
return new image.Image(imageName)
}

0 comments on commit cff1066

Please sign in to comment.