File: PackageModelTests.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (197 lines) | stat: -rw-r--r-- 7,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2014-2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Basics

@_spi(SwiftPMInternal)
@testable import PackageModel

import func TSCBasic.withTemporaryFile
import XCTest

import struct TSCBasic.ByteString

final class PackageModelTests: XCTestCase {
    func testProductTypeCodable() throws {
        struct Foo: Codable, Equatable {
            var type: ProductType
        }

        func checkCodable(_ type: ProductType) {
            do {
                let foo = Foo(type: type)
                let data = try JSONEncoder.makeWithDefaults().encode(foo)
                let decodedFoo = try JSONDecoder.makeWithDefaults().decode(Foo.self, from: data)
                XCTAssertEqual(foo, decodedFoo)
            } catch {
                XCTFail("\(error)")
            }
        }

        checkCodable(.library(.automatic))
        checkCodable(.library(.static))
        checkCodable(.library(.dynamic))
        checkCodable(.executable)
        checkCodable(.test)
    }

    func testProductFilterCodable() throws {
        // Test ProductFilter.everything
        try {
            let data = try JSONEncoder().encode(ProductFilter.everything)
            let decoded = try JSONDecoder().decode(ProductFilter.self, from: data)
            XCTAssertEqual(decoded, ProductFilter.everything)
        }()
        // Test ProductFilter.specific(), including that the order is normalized
        try {
            let data = try JSONEncoder().encode(ProductFilter.specific(["Bar", "Foo"]))
            let decoded = try JSONDecoder().decode(ProductFilter.self, from: data)
            XCTAssertEqual(decoded, ProductFilter.specific(["Foo", "Bar"]))
        }()
    }

    func testAndroidCompilerFlags() throws {
        let triple = try Triple("x86_64-unknown-linux-android")
        let fileSystem = InMemoryFileSystem()
        let sdkDir = AbsolutePath("/some/path/to/an/SDK.sdk")
        try fileSystem.createDirectory(sdkDir, recursive: true)
        let toolchainPath = AbsolutePath("/some/path/to/a/toolchain.xctoolchain")
        try fileSystem.createDirectory(toolchainPath, recursive: true)

        let swiftSDK = SwiftSDK(
            targetTriple: triple,
            toolset: .init(toolchainBinDir: toolchainPath.appending(components: "usr", "bin"), buildFlags: .init()),
            pathsConfiguration: .init(sdkRootPath: sdkDir)
        )

        XCTAssertEqual(
            try UserToolchain.deriveSwiftCFlags(
                triple: triple,
                swiftSDK: swiftSDK,
                environment: .current,
                fileSystem: fileSystem
            ),
            [
                // Needed when cross‐compiling for Android. 2020‐03‐01
                "-sdk",
                sdkDir.pathString,
            ]
        )
    }

    // tiny PE binary from: https://archive.is/w01DO
    static let tinyPEBytes: [UInt8] = [
        0x4D, 0x5A, 0x00, 0x00, 0x50, 0x45, 0x00, 0x00, 0x4C, 0x01, 0x01, 0x00,
        0x6A, 0x2A, 0x58, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x04, 0x00, 0x03, 0x01, 0x0B, 0x01, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00,
        0x04, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
        0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x68, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x02,
    ]

    func testWindowsLibrarianSelection() throws {

        #if os(Windows)
        let suffix = ".exe"
        #else
        let suffix = ""
        #endif

        let triple = try Triple("x86_64-unknown-windows-msvc")
        let fs = localFileSystem

        try withTemporaryFile { _ in
            try withTemporaryDirectory(removeTreeOnDeinit: true) { tmp in
                let contents = Self.tinyPEBytes
                let bin = tmp.appending("bin")
                try fs.createDirectory(bin)

                let lld = bin.appending("lld-link\(suffix)")
                try fs.writeFileContents(lld, bytes: ByteString(contents))

                let not = bin.appending("not-link\(suffix)")
                try fs.writeFileContents(not, bytes: ByteString(contents))

                #if !os(Windows)
                try fs.chmod(.executable, path: lld, options: [])
                try fs.chmod(.executable, path: not, options: [])
                #endif

                try XCTAssertEqual(
                    UserToolchain.determineLibrarian(
                        triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
                        extraSwiftFlags: ["-Xswiftc", "-use-ld=lld"],
                        fileSystem: fs
                    ),
                    lld
                )

                try XCTAssertEqual(
                    UserToolchain.determineLibrarian(
                        triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
                        extraSwiftFlags: ["-Xswiftc", "-use-ld=not-link"],
                        fileSystem: fs
                    ),
                    not
                )

                try XCTAssertThrowsError(
                    UserToolchain.determineLibrarian(
                        triple: triple, binDirectories: [bin], useXcrun: false, environment: [:], searchPaths: [],
                        extraSwiftFlags: [],
                        fileSystem: fs
                    )
                )
            }
        }
    }

    func testDetermineSwiftCompilers() throws {
        let fs = localFileSystem
        try withTemporaryFile { _ in
            try withTemporaryDirectory(removeTreeOnDeinit: true) { tmp in
                // When swiftc is not in the toolchain bin directory, UserToolchain
                // should find it in the system PATH search paths in the order they
                // are specified.
                let toolchainPath = tmp.appending("swift.xctoolchain")
                let toolchainBinDir = toolchainPath.appending(components: "usr", "bin")
                // Create the toolchain bin directory, but don't put swiftc in it.
                try fs.createDirectory(toolchainBinDir, recursive: true)

                // Create a directory with two swiftc binaries in it.
                let binDirs = ["bin1", "bin2"].map { tmp.appending($0) }
                for binDir in binDirs {
                    try fs.createDirectory(binDir)
                    let binFile = binDir.appending("swiftc")
                    try fs.writeFileContents(binFile, bytes: ByteString(Self.tinyPEBytes))
                    #if !os(Windows)
                    try fs.chmod(.executable, path: binFile, options: [])
                    #endif
                }

                let compilers = try UserToolchain.determineSwiftCompilers(
                    binDirectories: [toolchainBinDir],
                    useXcrun: false,
                    environment: [:],
                    searchPaths: binDirs,
                    fileSystem: fs
                )

                // The first swiftc in the search paths should be chosen.
                XCTAssertEqual(compilers.compile, binDirs.first?.appending("swiftc"))
            }
        }
    }
}