[build]: add tests for dockerfile arg parsing (#1342)

- Closes #1295
- Test cases added for validation based on changes from
  apple/container-builder-shim#70.
This commit is contained in:
Saehej Kang
2026-03-30 16:24:01 -07:00
committed by GitHub
parent e104171f5e
commit c501c8780b
@@ -1215,5 +1215,141 @@ extension TestCLIBuildBase {
)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
@Test func testBuildQuotedImageDockerfileArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
ARG IMAGE="ghcr.io/linuxcontainers/alpine:3.20"
FROM $IMAGE
RUN test -f /etc/alpine-release
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/quoted-image-dockerfile-arg:\(UUID().uuidString)"
try self.build(tag: imageName, tempDir: tempDir)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
@Test func testBuildQuotedStringDockerfileArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
FROM ghcr.io/linuxcontainers/alpine:3.20
ARG MYSTRING='"Hello, world!"'
RUN test "$MYSTRING" = '"Hello, world!"'
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/quoted-string-dockerfile-arg:\(UUID().uuidString)"
try self.build(tag: imageName, tempDir: tempDir)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
@Test func testBuildForwardReferencedDockerfileArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
ARG ALPINE="ghcr.io/linuxcontainers/alpine"
ARG IMAGE="${ALPINE}:3.20"
FROM $IMAGE
RUN test -f /etc/alpine-release
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/forward-referenced-dockerfile-arg:\(UUID().uuidString)"
try self.build(tag: imageName, tempDir: tempDir)
#expect(
try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)"
)
}
@Test func testBuildQuotedImageBuildArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
ARG IMAGE
FROM $IMAGE
RUN test -f /etc/alpine-release
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/quoted-image-build-arg:\(UUID().uuidString)"
try self.build(
tag: imageName,
tempDir: tempDir,
buildArgs: [
"IMAGE=ghcr.io/linuxcontainers/alpine:3.20"
]
)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
@Test func testBuildQuotedStringBuildArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
FROM ghcr.io/linuxcontainers/alpine:3.20
ARG MYSTRING
RUN test "$MYSTRING" = '"Hello, world!"'
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/quoted-string-build-arg:\(UUID().uuidString)"
try self.build(
tag: imageName,
tempDir: tempDir,
buildArgs: [
"MYSTRING=\"Hello, world!\""
]
)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
@Test func testBuildForwardReferencedBuildArg() throws {
let tempDir: URL = try createTempDir()
defer {
try! FileManager.default.removeItem(at: tempDir)
}
let dockerfile: String =
"""
ARG ALPINE
ARG IMAGE="$ALPINE:3.20"
FROM $IMAGE
RUN test -f /etc/alpine-release
"""
try createContext(tempDir: tempDir, dockerfile: dockerfile)
let imageName = "registry.local/forward-referenced-build-arg:\(UUID().uuidString)"
try self.build(
tag: imageName,
tempDir: tempDir,
buildArgs: [
"ALPINE=ghcr.io/linuxcontainers/alpine"
]
)
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
}
}
}