Skip to content

Commit cce03c7

Browse files
committed
improve example created by package init --type executable
motivation: example is out of date with best practices changes: * use @main istead of main.swift * test executable directly instead of shelling out to a process rdar://82119401
1 parent b4c5874 commit cce03c7

File tree

1 file changed

+11
-40
lines changed

1 file changed

+11
-40
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 11 additions & 40 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 = (packageType == .executable) ? "main.swift" : "\(typeName).swift"
289+
let sourceFileName = "\(typeName).swift"
290290
let sourceFile = moduleDir.appending(RelativePath(sourceFileName))
291291

292292
let content: String
@@ -303,7 +303,14 @@ public final class InitPackage {
303303
"""
304304
case .executable:
305305
content = """
306-
print("Hello, world!")
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+
}
307314
308315
"""
309316
case .systemModule, .empty, .manifest, .`extension`:
@@ -377,50 +384,14 @@ public final class InitPackage {
377384
try writePackageFile(path) { stream in
378385
stream <<< """
379386
import XCTest
380-
import class Foundation.Bundle
387+
@testable import \(moduleName)
381388
382389
final class \(moduleName)Tests: XCTestCase {
383390
func testExample() throws {
384391
// This is an example of a functional test case.
385392
// Use XCTAssert and related functions to verify your tests produce the correct
386393
// results.
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
394+
XCTAssertEqual(\(typeName)().text, "Hello, World!")
424395
}
425396
}
426397

0 commit comments

Comments
 (0)