|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// Copyright © 2025 Apple Inc. and the container project authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +import Foundation |
| 18 | +import Logging |
| 19 | +import Testing |
| 20 | + |
| 21 | +@testable import ContainerBuild |
| 22 | + |
| 23 | +@Suite class BuildFileResolvePathTests { |
| 24 | + private var baseTempURL: URL! |
| 25 | + private let fileManager = FileManager.default |
| 26 | + |
| 27 | + init() throws { |
| 28 | + baseTempURL = URL(fileURLWithPath: NSTemporaryDirectory()) |
| 29 | + .appendingPathComponent("BuildFileTests-\(UUID().uuidString)") |
| 30 | + try fileManager.createDirectory(at: baseTempURL, withIntermediateDirectories: true, attributes: nil) |
| 31 | + } |
| 32 | + |
| 33 | + deinit { |
| 34 | + if let baseTempURL = baseTempURL { |
| 35 | + try? fileManager.removeItem(at: baseTempURL) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private func createFile(at url: URL, content: String = "") throws { |
| 40 | + try fileManager.createDirectory( |
| 41 | + at: url.deletingLastPathComponent(), |
| 42 | + withIntermediateDirectories: true, |
| 43 | + attributes: nil |
| 44 | + ) |
| 45 | + let created = fileManager.createFile( |
| 46 | + atPath: url.path, |
| 47 | + contents: content.data(using: .utf8), |
| 48 | + attributes: nil |
| 49 | + ) |
| 50 | + try #require(created) |
| 51 | + } |
| 52 | + |
| 53 | + @Test func testResolvePathFindsDockerfile() throws { |
| 54 | + let contextDir = baseTempURL.path |
| 55 | + let dockerfilePath = baseTempURL.appendingPathComponent("Dockerfile") |
| 56 | + try createFile(at: dockerfilePath, content: "FROM alpine") |
| 57 | + |
| 58 | + let result = try BuildFile.resolvePath(contextDir: contextDir) |
| 59 | + |
| 60 | + #expect(result == dockerfilePath.path) |
| 61 | + } |
| 62 | + |
| 63 | + @Test func testResolvePathFindsContainerfile() throws { |
| 64 | + let contextDir = baseTempURL.path |
| 65 | + let containerfilePath = baseTempURL.appendingPathComponent("Containerfile") |
| 66 | + try createFile(at: containerfilePath, content: "FROM alpine") |
| 67 | + |
| 68 | + let result = try BuildFile.resolvePath(contextDir: contextDir) |
| 69 | + |
| 70 | + #expect(result == containerfilePath.path) |
| 71 | + } |
| 72 | + |
| 73 | + @Test func testResolvePathPrefersDockerfileWhenBothExist() throws { |
| 74 | + let contextDir = baseTempURL.path |
| 75 | + let dockerfilePath = baseTempURL.appendingPathComponent("Dockerfile") |
| 76 | + let containerfilePath = baseTempURL.appendingPathComponent("Containerfile") |
| 77 | + try createFile(at: dockerfilePath, content: "FROM alpine") |
| 78 | + try createFile(at: containerfilePath, content: "FROM ubuntu") |
| 79 | + |
| 80 | + let result = try BuildFile.resolvePath(contextDir: contextDir) |
| 81 | + |
| 82 | + #expect(result == dockerfilePath.path) |
| 83 | + } |
| 84 | + |
| 85 | + @Test func testResolvePathReturnsNilWhenNoFilesExist() throws { |
| 86 | + let contextDir = baseTempURL.path |
| 87 | + |
| 88 | + let result = try BuildFile.resolvePath(contextDir: contextDir) |
| 89 | + |
| 90 | + #expect(result == nil) |
| 91 | + } |
| 92 | + |
| 93 | + @Test func testResolvePathWithEmptyDirectory() throws { |
| 94 | + let emptyDir = baseTempURL.appendingPathComponent("empty") |
| 95 | + try fileManager.createDirectory(at: emptyDir, withIntermediateDirectories: true, attributes: nil) |
| 96 | + |
| 97 | + let result = try BuildFile.resolvePath(contextDir: emptyDir.path) |
| 98 | + |
| 99 | + #expect(result == nil) |
| 100 | + } |
| 101 | + |
| 102 | + @Test func testResolvePathWithNestedContextDirectory() throws { |
| 103 | + let nestedDir = baseTempURL.appendingPathComponent("project/build") |
| 104 | + try fileManager.createDirectory(at: nestedDir, withIntermediateDirectories: true, attributes: nil) |
| 105 | + let dockerfilePath = nestedDir.appendingPathComponent("Dockerfile") |
| 106 | + try createFile(at: dockerfilePath, content: "FROM node") |
| 107 | + |
| 108 | + let result = try BuildFile.resolvePath(contextDir: nestedDir.path) |
| 109 | + |
| 110 | + #expect(result == dockerfilePath.path) |
| 111 | + } |
| 112 | + |
| 113 | + @Test func testResolvePathWithRelativeContextDirectory() throws { |
| 114 | + let nestedDir = baseTempURL.appendingPathComponent("project") |
| 115 | + try fileManager.createDirectory(at: nestedDir, withIntermediateDirectories: true, attributes: nil) |
| 116 | + let dockerfilePath = nestedDir.appendingPathComponent("Dockerfile") |
| 117 | + try createFile(at: dockerfilePath, content: "FROM python") |
| 118 | + |
| 119 | + // Test with the absolute path |
| 120 | + let result = try BuildFile.resolvePath(contextDir: nestedDir.path) |
| 121 | + |
| 122 | + #expect(result == dockerfilePath.path) |
| 123 | + } |
| 124 | +} |
0 commit comments