|
15 | 15 | //===----------------------------------------------------------------------===// |
16 | 16 |
|
17 | 17 | import Foundation |
| 18 | +import SystemPackage |
18 | 19 | import Testing |
19 | 20 |
|
20 | 21 | @testable import ContainerPlugin |
@@ -88,6 +89,122 @@ struct PluginLoaderTest { |
88 | 89 | #expect(loader.findPlugin(name: "throw") == nil) |
89 | 90 | } |
90 | 91 |
|
| 92 | + @Test |
| 93 | + func testFindPluginForExecutable() async throws { |
| 94 | + let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 95 | + defer { try? FileManager.default.removeItem(at: tempURL) } |
| 96 | + let (factory, cliBinaryURL, serviceBinaryURL) = try setupMockWithRealBinaries(tempURL: tempURL) |
| 97 | + let loader = try PluginLoader( |
| 98 | + appRoot: tempURL, |
| 99 | + installRoot: URL(filePath: "/usr/local/"), |
| 100 | + logRoot: nil, |
| 101 | + pluginDirectories: [tempURL], |
| 102 | + pluginFactories: [factory] |
| 103 | + ) |
| 104 | + |
| 105 | + let cliMatch = loader.findPlugin(forExecutable: FilePath(cliBinaryURL.path(percentEncoded: false))) |
| 106 | + #expect(cliMatch?.name == "cli") |
| 107 | + |
| 108 | + let serviceMatch = loader.findPlugin(forExecutable: FilePath(serviceBinaryURL.path(percentEncoded: false))) |
| 109 | + #expect(serviceMatch?.name == "service") |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + func testFindPluginForExecutableViaSymlinkedInput() async throws { |
| 114 | + let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 115 | + defer { try? FileManager.default.removeItem(at: tempURL) } |
| 116 | + let (factory, cliBinaryURL, _) = try setupMockWithRealBinaries(tempURL: tempURL) |
| 117 | + let loader = try PluginLoader( |
| 118 | + appRoot: tempURL, |
| 119 | + installRoot: URL(filePath: "/usr/local/"), |
| 120 | + logRoot: nil, |
| 121 | + pluginDirectories: [tempURL], |
| 122 | + pluginFactories: [factory] |
| 123 | + ) |
| 124 | + |
| 125 | + // Simulate CommandLine.executablePath resolving to a symlink that |
| 126 | + // ultimately points at the plugin's real binary on disk. |
| 127 | + let otherTempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 128 | + defer { try? FileManager.default.removeItem(at: otherTempURL) } |
| 129 | + try FileManager.default.createDirectory(at: otherTempURL, withIntermediateDirectories: true) |
| 130 | + let symlinkURL = otherTempURL.appendingPathComponent("cli-symlink") |
| 131 | + try FileManager.default.createSymbolicLink(at: symlinkURL, withDestinationURL: cliBinaryURL) |
| 132 | + |
| 133 | + let match = loader.findPlugin(forExecutable: FilePath(symlinkURL.path(percentEncoded: false))) |
| 134 | + #expect(match?.name == "cli") |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + func testFindPluginForExecutableWithSymlinkedPluginBinary() async throws { |
| 139 | + let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 140 | + defer { try? FileManager.default.removeItem(at: tempURL) } |
| 141 | + try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true) |
| 142 | + |
| 143 | + // The plugin's registered binaryURL is itself a symlink pointing at a |
| 144 | + // binary that lives elsewhere on disk (e.g. a dev-mode install). |
| 145 | + let realBinDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 146 | + defer { try? FileManager.default.removeItem(at: realBinDir) } |
| 147 | + try FileManager.default.createDirectory(at: realBinDir, withIntermediateDirectories: true) |
| 148 | + let realBinaryURL = realBinDir.appendingPathComponent("cli-real") |
| 149 | + try Data().write(to: realBinaryURL) |
| 150 | + |
| 151 | + let symlinkBinaryURL = tempURL.appendingPathComponent("cli-bin") |
| 152 | + try FileManager.default.createSymbolicLink(at: symlinkBinaryURL, withDestinationURL: realBinaryURL) |
| 153 | + |
| 154 | + let cliConfig = PluginConfig(abstract: "cli", author: "CLI", servicesConfig: nil) |
| 155 | + let cliPlugin = Plugin(binaryURL: symlinkBinaryURL, config: cliConfig) |
| 156 | + let factory = try MockPluginFactory(tempURL: tempURL, plugins: ["cli": cliPlugin]) |
| 157 | + |
| 158 | + let loader = try PluginLoader( |
| 159 | + appRoot: tempURL, |
| 160 | + installRoot: URL(filePath: "/usr/local/"), |
| 161 | + logRoot: nil, |
| 162 | + pluginDirectories: [tempURL], |
| 163 | + pluginFactories: [factory] |
| 164 | + ) |
| 165 | + |
| 166 | + let match = loader.findPlugin(forExecutable: FilePath(realBinaryURL.path(percentEncoded: false))) |
| 167 | + #expect(match?.name == "cli") |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + func testFindPluginForExecutableNoMatch() async throws { |
| 172 | + let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 173 | + defer { try? FileManager.default.removeItem(at: tempURL) } |
| 174 | + let (factory, _, _) = try setupMockWithRealBinaries(tempURL: tempURL) |
| 175 | + let loader = try PluginLoader( |
| 176 | + appRoot: tempURL, |
| 177 | + installRoot: URL(filePath: "/usr/local/"), |
| 178 | + logRoot: nil, |
| 179 | + pluginDirectories: [tempURL], |
| 180 | + pluginFactories: [factory] |
| 181 | + ) |
| 182 | + |
| 183 | + let unrelatedURL = tempURL.appendingPathComponent("unrelated-bin") |
| 184 | + try Data().write(to: unrelatedURL) |
| 185 | + |
| 186 | + let match = loader.findPlugin(forExecutable: FilePath(unrelatedURL.path(percentEncoded: false))) |
| 187 | + #expect(match == nil) |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + func testFindPluginForExecutableNonexistentPath() async throws { |
| 192 | + let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 193 | + defer { try? FileManager.default.removeItem(at: tempURL) } |
| 194 | + let (factory, _, _) = try setupMockWithRealBinaries(tempURL: tempURL) |
| 195 | + let loader = try PluginLoader( |
| 196 | + appRoot: tempURL, |
| 197 | + installRoot: URL(filePath: "/usr/local/"), |
| 198 | + logRoot: nil, |
| 199 | + pluginDirectories: [tempURL], |
| 200 | + pluginFactories: [factory] |
| 201 | + ) |
| 202 | + |
| 203 | + let missingURL = tempURL.appendingPathComponent("does-not-exist") |
| 204 | + let match = loader.findPlugin(forExecutable: FilePath(missingURL.path(percentEncoded: false))) |
| 205 | + #expect(match == nil) |
| 206 | + } |
| 207 | + |
91 | 208 | @Test |
92 | 209 | func testFilterEnvironmentWithContainerPrefix() async throws { |
93 | 210 | let env = [ |
@@ -289,4 +406,34 @@ struct PluginLoaderTest { |
289 | 406 |
|
290 | 407 | return try MockPluginFactory(tempURL: tempURL, plugins: mockPlugins) |
291 | 408 | } |
| 409 | + |
| 410 | + // Unlike `setupMock`, the plugins here are backed by real, empty files on |
| 411 | + // disk so `findPlugin(forExecutable:)` can resolve their paths with |
| 412 | + // `realpath`. |
| 413 | + private func setupMockWithRealBinaries(tempURL: URL) throws -> (factory: MockPluginFactory, cliBinaryURL: URL, serviceBinaryURL: URL) { |
| 414 | + try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true) |
| 415 | + let cliBinaryURL = tempURL.appendingPathComponent("cli-bin") |
| 416 | + let serviceBinaryURL = tempURL.appendingPathComponent("service-bin") |
| 417 | + try Data().write(to: cliBinaryURL) |
| 418 | + try Data().write(to: serviceBinaryURL) |
| 419 | + |
| 420 | + let cliConfig = PluginConfig(abstract: "cli", author: "CLI", servicesConfig: nil) |
| 421 | + let cliPlugin: Plugin = Plugin(binaryURL: cliBinaryURL, config: cliConfig) |
| 422 | + let serviceServicesConfig = PluginConfig.ServicesConfig( |
| 423 | + loadAtBoot: false, |
| 424 | + runAtLoad: false, |
| 425 | + services: [PluginConfig.Service(type: .runtime, description: nil)], |
| 426 | + defaultArguments: [] |
| 427 | + ) |
| 428 | + let serviceConfig = PluginConfig(abstract: "service", author: "SERVICE", servicesConfig: serviceServicesConfig) |
| 429 | + let servicePlugin: Plugin = Plugin(binaryURL: serviceBinaryURL, config: serviceConfig) |
| 430 | + let mockPlugins = [ |
| 431 | + "cli": cliPlugin, |
| 432 | + MockPluginFactory.throwSuffix: nil, |
| 433 | + "service": servicePlugin, |
| 434 | + ] |
| 435 | + |
| 436 | + let factory = try MockPluginFactory(tempURL: tempURL, plugins: mockPlugins) |
| 437 | + return (factory, cliBinaryURL, serviceBinaryURL) |
| 438 | + } |
292 | 439 | } |
0 commit comments