|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2026 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 class Basics.InMemoryFileSystem |
| 14 | +import struct Basics.AbsolutePath |
| 15 | +import protocol Basics.FileSystem |
| 16 | +import class Basics.ObservabilitySystem |
| 17 | +import func Basics.resolveSymlinks |
| 18 | +import func Basics.withTemporaryDirectory |
| 19 | +import var Basics.localFileSystem |
| 20 | +import struct TSCBasic.StringError |
| 21 | +import struct TSCBasic.AbsolutePath |
| 22 | +import struct TSCBasic.ByteString |
| 23 | +import enum TSCBasic.FileMode |
| 24 | + |
| 25 | +import func SwiftBuildSupport.createBuildSymbolicLinks |
| 26 | + |
| 27 | +import _InternalTestSupport |
| 28 | +import Testing |
| 29 | + |
| 30 | +typealias AbsolutePath = Basics.AbsolutePath |
| 31 | + |
| 32 | +@Suite( |
| 33 | + .tags( |
| 34 | + .TestSize.small, |
| 35 | + ), |
| 36 | +) |
| 37 | +struct CreateBuildSymbolicLinkFunction { |
| 38 | + @Test( |
| 39 | + ) |
| 40 | + func createBuildSymbolicLinkCreation() async throws { |
| 41 | + let fs = localFileSystem |
| 42 | + try withTemporaryDirectory(removeTreeOnDeinit: false) { tmpDir in |
| 43 | + // Arrange |
| 44 | + let observability = ObservabilitySystem.makeForTesting() |
| 45 | + let source = tmpDir.appending("source") |
| 46 | + let target = tmpDir.appending(components: "my","target","directory") |
| 47 | + try localFileSystem.createDirectory(target, recursive: true) |
| 48 | + |
| 49 | + // Act |
| 50 | + createBuildSymbolicLinks( |
| 51 | + source, |
| 52 | + pointingAt: target, |
| 53 | + fileSystem: fs, |
| 54 | + observabilityScope: observability.topScope, |
| 55 | + ) |
| 56 | + |
| 57 | + // Assert |
| 58 | + try expectSymlink( |
| 59 | + source, |
| 60 | + pointsTo: target, |
| 61 | + fileSystem: fs, |
| 62 | + ) |
| 63 | + expectNoDiagnostics(observability.diagnostics) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + func failingToRemoveSourceSymlinkGeneratedAWarning() async throws { |
| 69 | + // Arrange |
| 70 | + struct FileSystemDouble: FileSystem { |
| 71 | + func createSymbolicLink(_ path: TSCBasic.AbsolutePath, pointingAt destination: TSCBasic.AbsolutePath, relative: Bool) throws { |
| 72 | + throw StringError("Purposely failing in \(#function)") |
| 73 | + } |
| 74 | + |
| 75 | + func removeFileTree(_ path: TSCBasic.AbsolutePath) throws { |
| 76 | + throw StringError("Purposely failing in \(#function)") |
| 77 | + } |
| 78 | + |
| 79 | + func removeFileTree(_ path: AbsolutePath) throws { |
| 80 | + throw StringError("Purposely failing in \(#function)") |
| 81 | + } |
| 82 | + func createSymbolicLink( source: AbsolutePath, pointingAt target: AbsolutePath ) throws { |
| 83 | + throw StringError("Purposely failing in \(#function)") |
| 84 | + } |
| 85 | + |
| 86 | + func move(from sourcePath: TSCBasic.AbsolutePath, to destinationPath: TSCBasic.AbsolutePath) throws {} |
| 87 | + func copy(from sourcePath: TSCBasic.AbsolutePath, to destinationPath: TSCBasic.AbsolutePath) throws {} |
| 88 | + func chmod(_ mode: TSCBasic.FileMode, path: TSCBasic.AbsolutePath, options: Set<TSCBasic.FileMode.Option>) throws {} |
| 89 | + func writeFileContents(_ path: TSCBasic.AbsolutePath, bytes: TSCBasic.ByteString) throws {} |
| 90 | + func readFileContents(_ path: TSCBasic.AbsolutePath) throws -> TSCBasic.ByteString { |
| 91 | + TSCBasic.ByteString() |
| 92 | + } |
| 93 | + func createDirectory(_ path: TSCBasic.AbsolutePath, recursive: Bool) throws {} |
| 94 | + let tempDirectory: TSCBasic.AbsolutePath |
| 95 | + let cachesDirectory: TSCBasic.AbsolutePath? |
| 96 | + let homeDirectory: TSCBasic.AbsolutePath |
| 97 | + func changeCurrentWorkingDirectory(to path: TSCBasic.AbsolutePath) throws {} |
| 98 | + let currentWorkingDirectory: TSCBasic.AbsolutePath? |
| 99 | + func getDirectoryContents(_ path: TSCBasic.AbsolutePath) throws -> [String] { |
| 100 | + return [] |
| 101 | + } |
| 102 | + func isWritable(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 103 | + func isReadable(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 104 | + func isSymlink(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 105 | + func isExecutableFile(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 106 | + func isFile(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 107 | + func isDirectory(_ path: TSCBasic.AbsolutePath) -> Bool { false } |
| 108 | + func exists(_ path: TSCBasic.AbsolutePath, followSymlink: Bool) -> Bool { false } |
| 109 | + func exists(_ path: AbsolutePath, followSymlink: Bool) -> Bool { false } |
| 110 | + } |
| 111 | + |
| 112 | + let fs = FileSystemDouble( |
| 113 | + tempDirectory: TSCBasic.AbsolutePath.root.appending(components: "tmp", "\(#function)", "tmp"), |
| 114 | + cachesDirectory: nil, |
| 115 | + homeDirectory: TSCBasic.AbsolutePath.root.appending(components: "tmp", "\(#function)", "home"), |
| 116 | + currentWorkingDirectory: nil, |
| 117 | + ) |
| 118 | + let observability = ObservabilitySystem.makeForTesting() |
| 119 | + |
| 120 | + // Act |
| 121 | + createBuildSymbolicLinks( |
| 122 | + AbsolutePath("/foo/bar"), |
| 123 | + pointingAt: AbsolutePath("/foo/ping/pong"), |
| 124 | + fileSystem: fs, |
| 125 | + observabilityScope: observability.topScope, |
| 126 | + ) |
| 127 | + |
| 128 | + testDiagnostics(observability.diagnostics) { result in |
| 129 | + result.check( |
| 130 | + diagnostic: .contains("unable to delete"), |
| 131 | + severity: .warning |
| 132 | + ) |
| 133 | + |
| 134 | + result.check( |
| 135 | + diagnostic: .contains("unable to create symbolic link"), |
| 136 | + severity: .warning |
| 137 | + ) |
| 138 | + |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments