|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import Testing |
| 15 | +import SWBCore |
| 16 | +import SWBTaskExecution |
| 17 | +import SWBTestSupport |
| 18 | +import SWBUtil |
| 19 | + |
| 20 | +@Suite |
| 21 | +fileprivate struct ObjectLibraryAssemblerTaskActionTests { |
| 22 | + @Test |
| 23 | + func duplicateFileHandling() async throws { |
| 24 | + // Create input files with duplicate basenames (all named file.o) |
| 25 | + let inputOne = Path.root.join("input1").join("file.o") |
| 26 | + let inputTwo = Path.root.join("input2").join("file.o") |
| 27 | + let inputThree = Path.root.join("input3").join("file.o") |
| 28 | + let inputUnique = Path.root.join("unique.o") |
| 29 | + let output = Path.root.join("output") |
| 30 | + |
| 31 | + let executionDelegate = MockExecutionDelegate() |
| 32 | + try executionDelegate.fs.createDirectory(inputOne.dirname, recursive: true) |
| 33 | + try executionDelegate.fs.createDirectory(inputTwo.dirname, recursive: true) |
| 34 | + try executionDelegate.fs.createDirectory(inputThree.dirname, recursive: true) |
| 35 | + try executionDelegate.fs.write(inputOne, contents: ByteString(encodingAsUTF8: "one")) |
| 36 | + try executionDelegate.fs.write(inputTwo, contents: ByteString(encodingAsUTF8: "two")) |
| 37 | + try executionDelegate.fs.write(inputThree, contents: ByteString(encodingAsUTF8: "three")) |
| 38 | + try executionDelegate.fs.write(inputUnique, contents: ByteString(encodingAsUTF8: "unique")) |
| 39 | + |
| 40 | + let outputDelegate = MockTaskOutputDelegate() |
| 41 | + |
| 42 | + let commandLine = [ |
| 43 | + "builtin-ObjectLibraryAssembler", |
| 44 | + "--linker-response-file-format", |
| 45 | + "unixShellQuotedSpaceSeparated", |
| 46 | + inputOne.str, |
| 47 | + inputTwo.str, |
| 48 | + inputThree.str, |
| 49 | + inputUnique.str, |
| 50 | + "--output", |
| 51 | + output.str |
| 52 | + ].map { ByteString(encodingAsUTF8: $0) } |
| 53 | + |
| 54 | + let inputs = [inputOne, inputTwo, inputThree, inputUnique].map { MakePlannedPathNode($0) } |
| 55 | + var builder = PlannedTaskBuilder(type: mockTaskType, ruleInfo: [], commandLine: commandLine.map { .literal($0) }, inputs: inputs, outputs: [MakePlannedPathNode(output)]) |
| 56 | + let task = Task(&builder) |
| 57 | + |
| 58 | + let result = await ObjectLibraryAssemblerTaskAction().performTaskAction( |
| 59 | + task, |
| 60 | + dynamicExecutionDelegate: MockDynamicTaskExecutionDelegate(), |
| 61 | + executionDelegate: executionDelegate, |
| 62 | + clientDelegate: MockTaskExecutionClientDelegate(), |
| 63 | + outputDelegate: outputDelegate |
| 64 | + ) |
| 65 | + |
| 66 | + #expect(result == .succeeded, "Task should succeed") |
| 67 | + #expect(outputDelegate.messages == [], "Should have no error messages") |
| 68 | + |
| 69 | + // Verify the output directory was created |
| 70 | + #expect(executionDelegate.fs.exists(output), "Output directory should exist") |
| 71 | + |
| 72 | + // Verify that files were copied with proper duplicate handling |
| 73 | + let copiedOriginal = output.join("file.o") |
| 74 | + let copiedDuplicate1 = output.join("file-1.o") |
| 75 | + let copiedDuplicate2 = output.join("file-2.o") |
| 76 | + let copiedUnique = output.join("unique.o") |
| 77 | + |
| 78 | + #expect(executionDelegate.fs.exists(copiedOriginal), "First file.o should exist") |
| 79 | + #expect(executionDelegate.fs.exists(copiedDuplicate1), "file-1.o should exist") |
| 80 | + #expect(executionDelegate.fs.exists(copiedDuplicate2), "file-2.o should exist") |
| 81 | + #expect(executionDelegate.fs.exists(copiedUnique), "unique.o should exist") |
| 82 | + |
| 83 | + // Verify content of copied files |
| 84 | + #expect(try executionDelegate.fs.read(copiedOriginal).asString == "one") |
| 85 | + #expect(try executionDelegate.fs.read(copiedDuplicate1).asString == "two") |
| 86 | + #expect(try executionDelegate.fs.read(copiedDuplicate2).asString == "three") |
| 87 | + #expect(try executionDelegate.fs.read(copiedUnique).asString == "unique") |
| 88 | + |
| 89 | + // Verify the response file was created with correct paths |
| 90 | + let responseFile = output.join("args.resp") |
| 91 | + #expect(executionDelegate.fs.exists(responseFile), "Response file should exist") |
| 92 | + |
| 93 | + let responseContent = try executionDelegate.fs.read(responseFile).asString |
| 94 | + #expect(responseContent.contains("file.o"), "Response should contain file.o") |
| 95 | + #expect(responseContent.contains("file-1.o"), "Response should contain file-1.o") |
| 96 | + #expect(responseContent.contains("file-2.o"), "Response should contain file-2.o") |
| 97 | + #expect(responseContent.contains("unique.o"), "Response should contain unique.o") |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + func noDuplicates() async throws { |
| 102 | + // Test that files with unique basenames are not renamed |
| 103 | + let inputOne = Path.root.join("a.o") |
| 104 | + let inputTwo = Path.root.join("b.o") |
| 105 | + let inputThree = Path.root.join("c.o") |
| 106 | + let output = Path.root.join("output") |
| 107 | + |
| 108 | + let executionDelegate = MockExecutionDelegate() |
| 109 | + try executionDelegate.fs.write(inputOne, contents: ByteString(encodingAsUTF8: "a")) |
| 110 | + try executionDelegate.fs.write(inputTwo, contents: ByteString(encodingAsUTF8: "b")) |
| 111 | + try executionDelegate.fs.write(inputThree, contents: ByteString(encodingAsUTF8: "c")) |
| 112 | + |
| 113 | + let outputDelegate = MockTaskOutputDelegate() |
| 114 | + |
| 115 | + let commandLine = [ |
| 116 | + "builtin-ObjectLibraryAssembler", |
| 117 | + "--linker-response-file-format", |
| 118 | + "unixShellQuotedSpaceSeparated", |
| 119 | + inputOne.str, |
| 120 | + inputTwo.str, |
| 121 | + inputThree.str, |
| 122 | + "--output", |
| 123 | + output.str |
| 124 | + ].map { ByteString(encodingAsUTF8: $0) } |
| 125 | + |
| 126 | + let inputs = [inputOne, inputTwo, inputThree].map { MakePlannedPathNode($0) } |
| 127 | + var builder = PlannedTaskBuilder(type: mockTaskType, ruleInfo: [], commandLine: commandLine.map { .literal($0) }, inputs: inputs, outputs: [MakePlannedPathNode(output)]) |
| 128 | + let task = Task(&builder) |
| 129 | + |
| 130 | + let result = await ObjectLibraryAssemblerTaskAction().performTaskAction( |
| 131 | + task, |
| 132 | + dynamicExecutionDelegate: MockDynamicTaskExecutionDelegate(), |
| 133 | + executionDelegate: executionDelegate, |
| 134 | + clientDelegate: MockTaskExecutionClientDelegate(), |
| 135 | + outputDelegate: outputDelegate |
| 136 | + ) |
| 137 | + |
| 138 | + #expect(result == .succeeded, "Task should succeed") |
| 139 | + |
| 140 | + // Verify files are copied with their original names (no -N suffix) |
| 141 | + let copiedA = output.join("a.o") |
| 142 | + let copiedB = output.join("b.o") |
| 143 | + let copiedC = output.join("c.o") |
| 144 | + |
| 145 | + #expect(executionDelegate.fs.exists(copiedA), "a.o should exist") |
| 146 | + #expect(executionDelegate.fs.exists(copiedB), "b.o should exist") |
| 147 | + #expect(executionDelegate.fs.exists(copiedC), "c.o should exist") |
| 148 | + |
| 149 | + // Verify that no renamed versions exist |
| 150 | + #expect(!executionDelegate.fs.exists(output.join("a-1.o")), "a-1.o should not exist") |
| 151 | + #expect(!executionDelegate.fs.exists(output.join("b-1.o")), "b-1.o should not exist") |
| 152 | + #expect(!executionDelegate.fs.exists(output.join("c-1.o")), "c-1.o should not exist") |
| 153 | + } |
| 154 | +} |
0 commit comments