Skip to content

Commit e58e1c2

Browse files
authored
Revert "improve example created by package init --type executable (#4122)" (#4133)
This reverts commit 85a84d9.
1 parent 85a84d9 commit e58e1c2

File tree

8 files changed

+188
-213
lines changed

8 files changed

+188
-213
lines changed

IntegrationTests/Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// swift-tools-version:5.4
1+
// swift-tools-version:5.1
22

33
import PackageDescription
44

55
let package = Package(
66
name: "IntegrationTests",
77
targets: [
88
.testTarget(name: "IntegrationTests", dependencies: [
9-
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
10-
.product(name: "TSCTestSupport", package: "swift-tools-support-core")
9+
"SwiftToolsSupport-auto",
10+
"TSCTestSupport"
1111
]),
1212
]
1313
)
@@ -20,6 +20,6 @@ if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
2020
]
2121
} else {
2222
package.dependencies += [
23-
.package(name: "swift-tools-support-core", path: "../TSC"),
23+
.package(path: "../TSC"),
2424
]
2525
}

IntegrationTests/Tests/IntegrationTests/BasicTests.swift

Lines changed: 96 additions & 156 deletions
Large diffs are not rendered by default.

IntegrationTests/Tests/IntegrationTests/Helpers.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,9 @@ func initGitRepo(
286286
}
287287

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

293293
// Generating StaticLibrary.xcframework.
294294
try withTemporaryDirectory { tmpDir in
@@ -343,3 +343,11 @@ extension ProcessResult {
343343
"""
344344
}
345345
}
346+
347+
func swiftcSupportsRenamingMainSymbol() throws -> Bool {
348+
try withTemporaryDirectory { tmpDir in
349+
FileManager.default.createFile(atPath: "\(tmpDir)/foo.swift", contents: Data())
350+
let result = try Process.popen(args: swiftc.pathString, "-c", "-Xfrontend", "-entry-point-function-name", "-Xfrontend", "foo", "\(tmpDir)/foo.swift", "-o", "\(tmpDir)/foo.o")
351+
return try !result.utf8stderrOutput().contains("unknown argument: '-entry-point-function-name'")
352+
}
353+
}

IntegrationTests/Tests/IntegrationTests/SwiftPMTests.swift

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
This source file is part of the Swift.org open source project
2+
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5-
Licensed under Apache License v2.0 with Runtime Library Exception
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
66

7-
See http://swift.org/LICENSE.txt for license information
8-
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
1010

1111
import XCTest
1212
import TSCBasic
@@ -16,13 +16,13 @@ final class SwiftPMTests: XCTestCase {
1616
func testBinaryTargets() throws {
1717
try XCTSkip("FIXME: ld: warning: dylib (/../BinaryTargets.6YVYK4/TestBinary/.build/x86_64-apple-macosx/debug/SwiftFramework.framework/SwiftFramework) was built for newer macOS version (10.15) than being linked (10.10)")
1818

19-
#if !os(macOS)
20-
try XCTSkip("Test requires macOS")
21-
#endif
19+
#if !os(macOS)
20+
try XCTSkip("Test requires macOS")
21+
#endif
2222

23-
try binaryTargetsFixture { fixturePath in
23+
try binaryTargetsFixture { prefix in
2424
do {
25-
let (stdout, stderr) = try sh(swiftRun, "--package-path", fixturePath, "exe")
25+
let (stdout, stderr) = try sh(swiftRun, "--package-path", prefix, "exe")
2626
XCTAssertNoMatch(stderr, .contains("warning: "))
2727
XCTAssertEqual(stdout, """
2828
SwiftFramework()
@@ -32,54 +32,52 @@ final class SwiftPMTests: XCTestCase {
3232
}
3333

3434
do {
35-
let (stdout, stderr) = try sh(swiftRun, "--package-path", fixturePath, "cexe")
35+
let (stdout, stderr) = try sh(swiftRun, "--package-path", prefix, "cexe")
3636
XCTAssertNoMatch(stderr, .contains("warning: "))
3737
XCTAssertMatch(stdout, .contains("<CLibrary: "))
3838
}
3939

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

45-
let validPath = fixturePath.appending(component: "SwiftFramework.zip")
46-
let (stdout, _) = try sh(swiftPackage, "--package-path", fixturePath, "compute-checksum", validPath)
45+
let validPath = prefix.appending(component: "SwiftFramework.zip")
46+
let (stdout, _) = try sh(swiftPackage, "--package-path", prefix, "compute-checksum", validPath)
4747
XCTAssertEqual(stdout.spm_chomp(), "d1f202b1bfe04dea30b2bc4038f8059dcd75a5a176f1d81fcaedb6d3597d1158")
4848
}
4949
}
5050
}
5151

5252
func testArchCustomization() throws {
5353
#if !os(macOS)
54-
try XCTSkip("Test requires macOS")
54+
try XCTSkip("Test requires macOS")
5555
#endif
5656

5757
try withTemporaryDirectory { tmpDir in
58-
let packagePath = tmpDir.appending(component: "foo")
59-
try localFileSystem.createDirectory(packagePath)
60-
try sh(swiftPackage, "--package-path", packagePath, "init", "--type", "executable")
61-
// delete any files generated
62-
for entry in try localFileSystem.getDirectoryContents(packagePath.appending(components: "Sources", "foo")) {
63-
try localFileSystem.removeFileTree(packagePath.appending(components: "Sources", "foo", entry))
64-
}
65-
try localFileSystem.writeFileContents(packagePath.appending(RelativePath("Sources/foo/main.m"))) {
58+
let foo = tmpDir.appending(component: "foo")
59+
try localFileSystem.createDirectory(foo)
60+
try sh(swiftPackage, "--package-path", foo, "init", "--type", "executable")
61+
62+
try localFileSystem.removeFileTree(foo.appending(RelativePath("Sources/foo/main.swift")))
63+
try localFileSystem.writeFileContents(foo.appending(RelativePath("Sources/foo/main.m"))) {
6664
$0 <<< "int main() {}"
6765
}
6866
let archs = ["x86_64", "arm64"]
6967

7068
for arch in archs {
71-
try sh(swiftBuild, "--package-path", packagePath, "--arch", arch)
72-
let fooPath = packagePath.appending(RelativePath(".build/\(arch)-apple-macosx/debug/foo"))
69+
try sh(swiftBuild, "--package-path", foo, "--arch", arch)
70+
let fooPath = foo.appending(RelativePath(".build/\(arch)-apple-macosx/debug/foo"))
7371
XCTAssertFileExists(fooPath)
7472
}
7573

76-
let args = [swiftBuild.pathString, "--package-path", packagePath.pathString] + archs.flatMap{ ["--arch", $0] }
74+
let args = [swiftBuild.pathString, "--package-path", foo.pathString] + archs.flatMap{ ["--arch", $0] }
7775
try _sh(args)
7876

79-
let fooPath = packagePath.appending(RelativePath(".build/apple/Products/Debug/foo"))
77+
let fooPath = foo.appending(RelativePath(".build/apple/Products/Debug/foo"))
8078
XCTAssertFileExists(fooPath)
8179

82-
let objectsDir = packagePath.appending(RelativePath(".build/apple/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal"))
80+
let objectsDir = foo.appending(RelativePath(".build/apple/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal"))
8381
for arch in archs {
8482
XCTAssertDirectoryExists(objectsDir.appending(component: arch))
8583
}

IntegrationTests/Tests/IntegrationTests/XCBuildTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9-
*/
9+
*/
1010

1111
import XCTest
1212
import TSCBasic
@@ -15,7 +15,7 @@ import TSCTestSupport
1515
final class XCBuildTests: XCTestCase {
1616
func testExecutableProducts() throws {
1717
#if !os(macOS)
18-
try XCTSkip("Test requires macOS")
18+
try XCTSkip("Test requires macOS")
1919
#endif
2020

2121
fixture(name: "XCBuild/ExecutableProducts") { path in
@@ -118,7 +118,7 @@ final class XCBuildTests: XCTestCase {
118118
try XCTSkip("FIXME: /.../XCBuild_TestProducts.551ajO/Foo/.build/apple/Intermediates.noindex/GeneratedModuleMaps/macosx/FooLib.modulemap:2:12: error: header 'FooLib-Swift.h' not found")
119119

120120
#if !os(macOS)
121-
try XCTSkip("Test requires macOS")
121+
try XCTSkip("Test requires macOS")
122122
#endif
123123

124124
fixture(name: "XCBuild/TestProducts") { path in
@@ -200,7 +200,7 @@ final class XCBuildTests: XCTestCase {
200200

201201
func testLibraryProductsAndTargets() throws {
202202
#if !os(macOS)
203-
try XCTSkip("Test requires macOS")
203+
try XCTSkip("Test requires macOS")
204204
#endif
205205

206206
fixture(name: "XCBuild/Libraries") { path in
@@ -276,7 +276,7 @@ final class XCBuildTests: XCTestCase {
276276
try XCTSkip("FIXME: ld: warning: ignoring file /../XCBuild_SystemTargets.b38QoO/Inputs/libsys.a, building for macOS-arm64 but attempting to link with file built for unknown-x86_64\n\nUndefined symbols for architecture arm64:\n \"_GetSystemLibName\", referenced from:\n _main in main.o\n\nld: symbol(s) not found for architecture arm64\n\nclang: error: linker command failed with exit code 1 (use -v to see invocation)\n\nBuild cancelled\n")
277277

278278
#if !os(macOS)
279-
try XCTSkip("Test requires macOS")
279+
try XCTSkip("Test requires macOS")
280280
#endif
281281

282282
fixture(name: "XCBuild/SystemTargets") { path in
@@ -312,7 +312,7 @@ final class XCBuildTests: XCTestCase {
312312
try XCTSkip("FIXME: swift-test invocations are timing out in Xcode and self-hosted CI")
313313

314314
#if !os(macOS) || Xcode
315-
try XCTSkip("Test requires macOS")
315+
try XCTSkip("Test requires macOS")
316316
#endif
317317

318318
fixture(name: "XCBuild/TestProducts") { path in

Sources/Workspace/InitPackage.swift

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public final class InitPackage {
286286
let moduleDir = sources.appending(component: "\(pkgname)")
287287
try makeDirectories(moduleDir)
288288

289-
let sourceFileName = "\(typeName).swift"
289+
let sourceFileName = (packageType == .executable) ? "main.swift" : "\(typeName).swift"
290290
let sourceFile = moduleDir.appending(RelativePath(sourceFileName))
291291

292292
let content: String
@@ -303,14 +303,7 @@ public final class InitPackage {
303303
"""
304304
case .executable:
305305
content = """
306-
@main
307-
public struct \(typeName) {
308-
public private(set) var text = "Hello, World!"
309-
310-
public static func main() {
311-
print(\(typeName)().text)
312-
}
313-
}
306+
print("Hello, world!")
314307
315308
"""
316309
case .systemModule, .empty, .manifest, .`extension`:
@@ -384,14 +377,50 @@ public final class InitPackage {
384377
try writePackageFile(path) { stream in
385378
stream <<< """
386379
import XCTest
387-
@testable import \(moduleName)
380+
import class Foundation.Bundle
388381
389382
final class \(moduleName)Tests: XCTestCase {
390383
func testExample() throws {
391384
// This is an example of a functional test case.
392385
// Use XCTAssert and related functions to verify your tests produce the correct
393386
// results.
394-
XCTAssertEqual(\(typeName)().text, "Hello, World!")
387+
388+
// Some of the APIs that we use below are available in macOS 10.13 and above.
389+
guard #available(macOS 10.13, *) else {
390+
return
391+
}
392+
393+
// Mac Catalyst won't have `Process`, but it is supported for executables.
394+
#if !targetEnvironment(macCatalyst)
395+
396+
let fooBinary = productsDirectory.appendingPathComponent("\(pkgname)")
397+
398+
let process = Process()
399+
process.executableURL = fooBinary
400+
401+
let pipe = Pipe()
402+
process.standardOutput = pipe
403+
404+
try process.run()
405+
process.waitUntilExit()
406+
407+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
408+
let output = String(data: data, encoding: .utf8)
409+
410+
XCTAssertEqual(output, "Hello, world!\\n")
411+
#endif
412+
}
413+
414+
/// Returns path to the built products directory.
415+
var productsDirectory: URL {
416+
#if os(macOS)
417+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
418+
return bundle.bundleURL.deletingLastPathComponent()
419+
}
420+
fatalError("couldn't find the products directory")
421+
#else
422+
return Bundle.main.bundleURL
423+
#endif
395424
}
396425
}
397426

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ final class PackageToolTests: CommandsTestCase {
673673
XCTAssertMatch(contents, .prefix("// swift-tools-version:\(version < .v5_4 ? "" : " ")\(versionSpecifier)\n"))
674674

675675
XCTAssertFileExists(manifest)
676-
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "Foo")), ["Foo.swift"])
676+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "Foo")), ["main.swift"])
677677
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")).sorted(), ["FooTests"])
678678
}
679679
}
@@ -705,7 +705,7 @@ final class PackageToolTests: CommandsTestCase {
705705
XCTAssertMatch(contents, .prefix("// swift-tools-version:\(version < .v5_4 ? "" : " ")\(versionSpecifier)\n"))
706706

707707
XCTAssertFileExists(manifest)
708-
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "CustomName")), ["CustomName.swift"])
708+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "CustomName")), ["main.swift"])
709709
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")).sorted(), ["CustomNameTests"])
710710
}
711711
}

Tests/WorkspaceTests/InitTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class InitTests: XCTestCase {
8181
let readmeContents: String = try localFileSystem.readFileContents(readme)
8282
XCTAssertMatch(readmeContents, .prefix("# Foo\n"))
8383

84-
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "Foo")), ["Foo.swift"])
84+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources").appending(component: "Foo")), ["main.swift"])
8585
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")).sorted(), ["FooTests"])
8686

8787
// If we have a compiler that supports `-entry-point-function-name`, we try building it (we need that flag now).

0 commit comments

Comments
 (0)