Skip to content

Commit

Permalink
Merge pull request #545 from snyk/feat/add-parse-dockerfile-functiona…
Browse files Browse the repository at this point in the history
…lity

feat: add "parse dockerfile" functionality
  • Loading branch information
shlomiSnyk authored Oct 22, 2023
2 parents cb0a0c2 + 30c3030 commit 0b7a1e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/dockerfile/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DockerfileParser } from "dockerfile-ast";
import { Dockerfile, DockerfileParser } from "dockerfile-ast";
import * as fs from "fs";
import { normalize as normalizePath } from "path";
import {
Expand All @@ -18,6 +18,7 @@ export {
getDockerfileBaseImageName,
updateDockerfileBaseImageName,
DockerFileAnalysis,
parseDockerfile,
};

async function readDockerfileAndAnalyse(
Expand Down Expand Up @@ -53,3 +54,7 @@ async function readFile(path: string) {
});
}) as Promise<string>;
}

function parseDockerfile(content: string): Dockerfile {
return DockerfileParser.parse(content);
}
24 changes: 23 additions & 1 deletion test/lib/dockerfile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import exp from "constants";
import { DockerfileParser } from "dockerfile-ast";
import { Dockerfile } from "dockerfile-ast/lib/dockerfile";
import * as path from "path";
import { parseDockerfile } from "../../lib/dockerfile";

import * as dockerFile from "../../lib/dockerfile";
import {
Expand Down Expand Up @@ -268,3 +270,23 @@ describe("readDockerfileAndAnalyse() correctly parses...", () => {
});
});
});

describe("parseDockerfile()", () => {
afterEach(() => {
jest.resetAllMocks();
});

test("returns undefined when Dockerfile is not supplied", async () => {
const dockerfileContent: string = "some dockerfile content";

const expectedParsedDockerfile: jest.Mocked<Dockerfile> =
jest.mocked(Dockerfile);
jest
.spyOn(DockerfileParser, "parse")
.mockImplementation(() => expectedParsedDockerfile);

const result = parseDockerfile(dockerfileContent);

expect(result).toEqual(expectedParsedDockerfile);
});
});

0 comments on commit 0b7a1e7

Please sign in to comment.