Skip to content

Commit 7b1c1fb

Browse files
committed
update temp file name + add test cases
1 parent 57c8add commit 7b1c1fb

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Sources/ContainerCommands/BuildCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ extension Application {
208208
}
209209

210210
let buildFileData: Data
211-
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("TempDockerfile")
211+
let tempFile = FileManager.default.temporaryDirectory.appendingPathComponent("Dockerfile-\(UUID().uuidString)")
212212
defer {
213213
try? FileManager.default.removeItem(at: tempFile)
214214
}

Tests/CLITests/Subcommands/Build/CLIBuildBase.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,42 @@ class TestCLIBuildBase: CLITest {
144144
return response.output
145145
}
146146

147+
@discardableResult
148+
func buildWithStdin(
149+
tags: [String],
150+
tempContext: URL,
151+
dockerfileContents: String,
152+
buildArgs: [String] = [],
153+
otherArgs: [String] = []
154+
) throws -> String {
155+
let contextDir: URL = tempContext.appendingPathComponent("context")
156+
let contextDirPath = contextDir.absoluteURL.path
157+
var args = [
158+
"build",
159+
"-f",
160+
"-",
161+
]
162+
for tag in tags {
163+
args.append("-t")
164+
args.append(tag)
165+
}
166+
for arg in buildArgs {
167+
args.append("--build-arg")
168+
args.append(arg)
169+
}
170+
args.append(contextDirPath)
171+
172+
args.append(contentsOf: otherArgs)
173+
174+
let stdinData = Data(dockerfileContents.utf8)
175+
let response = try run(arguments: args, stdin: stdinData)
176+
if response.status != 0 {
177+
throw CLIError.executionFailed("build failed: stdout=\(response.output) stderr=\(response.error)")
178+
}
179+
180+
return response.output
181+
}
182+
147183
enum FileSystemEntry {
148184
case file(
149185
_ path: String,

Tests/CLITests/Subcommands/Build/CLIBuilderTest.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,5 +456,20 @@ extension TestCLIBuildBase {
456456
#expect(try self.inspectImage(tag2) == tag2, "expected to have successfully built \(tag2)")
457457
#expect(try self.inspectImage(tag3) == tag3, "expected to have successfully built \(tag3)")
458458
}
459+
460+
@Test func testBuildWithDockerfileFromStdin() throws {
461+
let tempDir: URL = try createTempDir()
462+
let dockerfile =
463+
"""
464+
FROM scratch
465+
466+
ADD emptyFile /
467+
"""
468+
let context: [FileSystemEntry] = [.file("emptyFile", content: .zeroFilled(size: 1))]
469+
try createContext(tempDir: tempDir, dockerfile: "", context: context)
470+
let imageName = "registry.local/stdin-file:\(UUID().uuidString)"
471+
try buildWithStdin(tags: [imageName], tempContext: tempDir, dockerfileContents: dockerfile)
472+
#expect(try self.inspectImage(imageName) == imageName, "expected to have successfully built \(imageName)")
473+
}
459474
}
460475
}

0 commit comments

Comments
 (0)