Skip to content

Commit

Permalink
Merge pull request #2588 from hartbit/binary-targets-tests
Browse files Browse the repository at this point in the history
Reimplement binary targets tests without binaries
  • Loading branch information
aciidgh authored Feb 18, 2020
2 parents 961ed32 + 139077a commit 6aa2bb7
Show file tree
Hide file tree
Showing 21 changed files with 338 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

#import "DynamicLibrary.h"

@implementation DynamicLibrary
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

#import <Foundation/Foundation.h>

@interface DynamicLibrary : NSObject
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

#import "StaticLibrary.h"

@implementation StaticLibrary
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

#import <Foundation/Foundation.h>

@interface StaticLibrary : NSObject
@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2020 David Hart. All rights reserved.</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SwiftFramework.h
// SwiftFramework
//
// Created by David Hart on 17.02.20.
// Copyright © 2020 David Hart. All rights reserved.
//

#import <Foundation/Foundation.h>

//! Project version number for SwiftFramework.
FOUNDATION_EXPORT double SwiftFrameworkVersionNumber;

//! Project version string for SwiftFramework.
FOUNDATION_EXPORT const unsigned char SwiftFrameworkVersionString[];

// In this header, you should import all the public headers of your framework using statements like #import <SwiftFramework/PublicHeader.h>


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SwiftFramework.swift
// SwiftFramework
//
// Created by David Hart on 17.02.20.
// Copyright © 2020 David Hart. All rights reserved.
//

import Foundation

public struct SwiftFramework {
public init() {}
}
18 changes: 18 additions & 0 deletions IntegrationTests/Fixtures/BinaryTargets/TestBinary/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version:999.0

import PackageDescription

let package = Package(
name: "TestBinary",
dependencies: [
],
targets: [
.target(name: "exe", dependencies: ["Library"]),
.target(name: "Library", dependencies: ["SwiftFramework"]),
.target(name: "cexe", dependencies: ["CLibrary"]),
.target(name: "CLibrary", dependencies: ["StaticLibrary", "DynamicLibrary"]),
.binaryTarget(name: "SwiftFramework", path: "SwiftFramework.xcframework"),
.binaryTarget(name: "StaticLibrary", path: "StaticLibrary.xcframework"),
.binaryTarget(name: "DynamicLibrary", path: "DynamicLibrary.xcframework"),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#import "CLibrary.h"

@implementation CLibrary

- (instancetype)init {
self = [super init];
_staticLibrary = [StaticLibrary new];
_dynamicLibrary = [DynamicLibrary new];
return self;
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#import <StaticLibrary.h>
#import <DynamicLibrary.h>

@interface CLibrary: NSObject

@property (nonatomic, readonly) StaticLibrary* staticLibrary;
@property (nonatomic, readonly) DynamicLibrary* dynamicLibrary;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SwiftFramework

public struct Library {
public let framework = SwiftFramework()

public init() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <CLibrary.h>

int main(int argc, const char* argv[]) {
printf("%s", [CLibrary new].description.UTF8String);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import SwiftFramework
import Library

print(SwiftFramework())
print(Library())
Binary file not shown.
Binary file not shown.
13 changes: 13 additions & 0 deletions IntegrationTests/Fixtures/XCBuild/SystemTargets/Inputs/libsys.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

const char* GetSystemLibName() {
return "SystemLibrary";
}
12 changes: 11 additions & 1 deletion IntegrationTests/Fixtures/XCBuild/SystemTargets/Inputs/libsys.h
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
const char* GetSystemLibName();
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 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 Swift project authors
*/

const char* GetSystemLibName();
96 changes: 90 additions & 6 deletions IntegrationTests/Tests/IntegrationTests/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,32 @@ let toolchainPath: AbsolutePath = {
}

#if os(macOS)
let swiftcPath = try! AbsolutePath(sh("xcrun", "--find", "swift").stdout)
let swiftcPath = try! AbsolutePath(sh("xcrun", "--find", "swift").stdout.spm_chomp())
let toolchainPath = swiftcPath.parentDirectory.parentDirectory.parentDirectory
return toolchainPath
#else
fatalError("TOOLCHAIN_PATH environment variable required")
#endif
}()

let clang: AbsolutePath = {
if let environmentPath = ProcessInfo.processInfo.environment["CLANG_PATH"] {
return AbsolutePath(environmentPath)
}

let clangPath = toolchainPath.appending(components: "usr", "bin", "clang")
return clangPath
}()

let xcodebuild: AbsolutePath = {
#if os(macOS)
let xcodebuildPath = try! AbsolutePath(sh("xcrun", "--find", "xcodebuild").stdout.spm_chomp())
return xcodebuildPath
#else
fatalError("should not be used on other platforms than macOS")
#endif
}()

let swift: AbsolutePath = {
if let environmentPath = ProcessInfo.processInfo.environment["SWIFT_PATH"] {
return AbsolutePath(environmentPath)
Expand Down Expand Up @@ -72,7 +90,7 @@ let lldb: AbsolutePath = {
}

#if os(macOS)
let lldbPath = try! AbsolutePath(sh("xcrun", "--find", "lldb").stdout)
let lldbPath = try! AbsolutePath(sh("xcrun", "--find", "lldb").stdout.spm_chomp())
return lldbPath
#else
fatalError("LLDB_PATH environment variable required")
Expand Down Expand Up @@ -110,6 +128,34 @@ func sh(
file: StaticString = #file,
line: UInt = #line
) throws -> (stdout: String, stderr: String) {
let result = try _sh(arguments, env: env, file: file, line: line)
let stdout = try result.utf8Output()
let stderr = try result.utf8stderrOutput()
XCTAssertEqual(result.exitStatus, .terminated(code: 0), stderr, file: file, line: line)
return (stdout, stderr)
}

@discardableResult
func shFails(
_ arguments: CustomStringConvertible...,
env: [String: String] = [:],
file: StaticString = #file,
line: UInt = #line
) throws -> (stdout: String, stderr: String) {
let result = try _sh(arguments, env: env, file: file, line: line)
let stdout = try result.utf8Output()
let stderr = try result.utf8stderrOutput()
XCTAssertNotEqual(result.exitStatus, .terminated(code: 0), stderr, file: file, line: line)
return (stdout, stderr)
}

@discardableResult
func _sh(
_ arguments: [CustomStringConvertible],
env: [String: String] = [:],
file: StaticString = #file,
line: UInt = #line
) throws -> ProcessResult {
var environment = ProcessInfo.processInfo.environment

if let sdkRoot = sdkRoot {
Expand All @@ -119,10 +165,7 @@ func sh(
environment.merge(env, uniquingKeysWith: { $1 })

let result = try Process.popen(arguments: arguments.map { $0.description }, environment: environment)
let stdout = try result.utf8Output()
let stderr = try result.utf8stderrOutput()
XCTAssertEqual(result.exitStatus, .terminated(code: 0), stderr, file: file, line: line)
return (stdout, stderr)
return result
}

/// Test-helper function that runs a block of code on a copy of a test fixture
Expand Down Expand Up @@ -228,3 +271,44 @@ func initGitRepo(
XCTFail("\(error)", file: file, line: line)
}
}

func binaryTargetsFixture(_ closure: (AbsolutePath) throws -> Void) throws {
fixture(name: "BinaryTargets") { prefix in
let inputsPath = prefix.appending(component: "Inputs")
let packagePath = prefix.appending(component: "TestBinary")

// Generating StaticLibrary.xcframework.
try withTemporaryDirectory { tmpDir in
let subpath = inputsPath.appending(component: "StaticLibrary")
let sourcePath = subpath.appending(component: "StaticLibrary.m")
let headersPath = subpath.appending(component: "include")
let libraryPath = tmpDir.appending(component: "libStaticLibrary.a")
try sh(clang, "-c", sourcePath, "-I", headersPath, "-fobjc-arc", "-fmodules", "-o", libraryPath)
let xcframeworkPath = packagePath.appending(component: "StaticLibrary.xcframework")
try sh(xcodebuild, "-create-xcframework", "-library", libraryPath, "-headers", headersPath, "-output", xcframeworkPath)
}

// Generating DynamicLibrary.xcframework.
try withTemporaryDirectory { tmpDir in
let subpath = inputsPath.appending(component: "DynamicLibrary")
let sourcePath = subpath.appending(component: "DynamicLibrary.m")
let headersPath = subpath.appending(component: "include")
let libraryPath = tmpDir.appending(component: "libDynamicLibrary.dylib")
try sh(clang, sourcePath, "-I", headersPath, "-fobjc-arc", "-fmodules", "-dynamiclib", "-o", libraryPath)
let xcframeworkPath = packagePath.appending(component: "DynamicLibrary.xcframework")
try sh(xcodebuild, "-create-xcframework", "-library", libraryPath, "-headers", headersPath, "-output", xcframeworkPath)
}

// Generating SwiftFramework.xcframework.
try withTemporaryDirectory { tmpDir in
let subpath = inputsPath.appending(component: "SwiftFramework")
let projectPath = subpath.appending(component: "SwiftFramework.xcodeproj")
try sh(xcodebuild, "-project", projectPath, "-scheme", "SwiftFramework", "-derivedDataPath", tmpDir, "COMPILER_INDEX_STORE_ENABLE=NO")
let frameworkPath = tmpDir.appending(RelativePath("Build/Products/Debug/SwiftFramework.framework"))
let xcframeworkPath = packagePath.appending(component: "SwiftFramework.xcframework")
try sh(xcodebuild, "-create-xcframework", "-framework", frameworkPath, "-output", xcframeworkPath)
}

try closure(packagePath)
}
}
47 changes: 47 additions & 0 deletions IntegrationTests/Tests/IntegrationTests/SwiftPMTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
This source file is part of the Swift.org open source project
Copyright (c) 2014 - 2020 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 Swift project authors
*/

import XCTest
import TSCBasic
import TSCTestSupport

final class SwiftPMTests: XCTestCase {
#if os(macOS)
func testBinaryTargets() throws {
try binaryTargetsFixture { prefix in
do {
let (stdout, stderr) = try sh(swiftRun, "--package-path", prefix, "exe")
XCTAssertNoMatch(stderr, .contains("warning: "))
XCTAssertEqual(stdout, """
SwiftFramework()
Library(framework: SwiftFramework.SwiftFramework())
""")
}

do {
let (stdout, stderr) = try sh(swiftRun, "--package-path", prefix, "cexe")
XCTAssertNoMatch(stderr, .contains("warning: "))
XCTAssertMatch(stdout, .contains("<CLibrary: "))
}

do {
let invalidPath = prefix.appending(component: "SwiftFramework.xcframework")
let (_, stderr) = try shFails(swiftPackage, "--package-path", prefix, "compute-checksum", invalidPath)
XCTAssertMatch(stderr, .contains("error: unexpected file type; supported extensions are: zip"))

let validPath = prefix.appending(component: "SwiftFramework.zip")
let (stdout, _) = try sh(swiftPackage, "--package-path", prefix, "compute-checksum", validPath)
XCTAssertEqual(stdout.spm_chomp(), "d1f202b1bfe04dea30b2bc4038f8059dcd75a5a176f1d81fcaedb6d3597d1158")
}
}
}
#endif
}
Loading

0 comments on commit 6aa2bb7

Please sign in to comment.