From c635db2fbc0436ad90685c65bd33aa0da8e8979b Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 6 Oct 2026 18:28:41 -0400 Subject: [PATCH] tests: add example project fixtures (#301) ## What does this PR do? Adds integration tests in the form of example apps that we can build and run. ## How was it tested? Ran builds on Linux, tests on macOS. Will also add this to CI later. ## AI tool usage How much of this PR was AI-assisted? (check one) - [ ] **0** - No AI was used to write code - [x] **1** - I was assisted by AI. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only --- Examples/.gitignore | 6 + Examples/DemoDynamic/Package.swift | 29 ++++ .../DemoDynamic/Sources/DemoDynamic/App.swift | 12 ++ Examples/DemoDynamic/xtool.yml | 2 + Examples/DemoExtensions/Package.swift | 33 +++++ .../Sources/DemoExtensions/App.swift | 4 + .../Sources/DemoExtensionsWidget/Widget.swift | 51 +++++++ .../Support/DemoExtensionsWidget-Info.plist | 11 ++ Examples/DemoExtensions/xtool.yml | 6 + Examples/DemoResources/Package.swift | 31 ++++ Examples/DemoResources/Resources/top.txt | 1 + .../Sources/DemoResources/App.swift | 34 +++++ .../Sources/DemoResources/bundled.txt | 1 + Examples/DemoResources/xtool.yml | 4 + Examples/DemoSimple/Package.swift | 28 ++++ .../DemoSimple/Sources/DemoSimple/App.swift | 4 + Examples/DemoSimple/xtool.yml | 2 + Examples/DemoTCA/Package.resolved | 132 ++++++++++++++++++ Examples/DemoTCA/Package.swift | 30 ++++ .../DemoTCA/Sources/DemoTCA/ContentView.swift | 24 ++++ .../Sources/DemoTCA/CounterFeature.swift | 31 ++++ .../DemoTCA/Sources/DemoTCA/DemoTCAApp.swift | 15 ++ Examples/DemoTCA/xtool.yml | 2 + Examples/ExampleSupport/Package.swift | 31 ++++ .../ExampleDynamicLibrary.swift | 9 ++ .../ExampleDynamicLibrary/response.txt | 1 + .../Sources/ExampleSupport/ExampleApp.swift | 25 ++++ .../ExampleSupport/ExampleSupport.swift | 12 ++ Examples/README.md | 14 ++ Examples/test.sh | 118 ++++++++++++++++ 30 files changed, 703 insertions(+) create mode 100644 Examples/.gitignore create mode 100644 Examples/DemoDynamic/Package.swift create mode 100644 Examples/DemoDynamic/Sources/DemoDynamic/App.swift create mode 100644 Examples/DemoDynamic/xtool.yml create mode 100644 Examples/DemoExtensions/Package.swift create mode 100644 Examples/DemoExtensions/Sources/DemoExtensions/App.swift create mode 100644 Examples/DemoExtensions/Sources/DemoExtensionsWidget/Widget.swift create mode 100644 Examples/DemoExtensions/Support/DemoExtensionsWidget-Info.plist create mode 100644 Examples/DemoExtensions/xtool.yml create mode 100644 Examples/DemoResources/Package.swift create mode 100644 Examples/DemoResources/Resources/top.txt create mode 100644 Examples/DemoResources/Sources/DemoResources/App.swift create mode 100644 Examples/DemoResources/Sources/DemoResources/bundled.txt create mode 100644 Examples/DemoResources/xtool.yml create mode 100644 Examples/DemoSimple/Package.swift create mode 100644 Examples/DemoSimple/Sources/DemoSimple/App.swift create mode 100644 Examples/DemoSimple/xtool.yml create mode 100644 Examples/DemoTCA/Package.resolved create mode 100644 Examples/DemoTCA/Package.swift create mode 100644 Examples/DemoTCA/Sources/DemoTCA/ContentView.swift create mode 100644 Examples/DemoTCA/Sources/DemoTCA/CounterFeature.swift create mode 100644 Examples/DemoTCA/Sources/DemoTCA/DemoTCAApp.swift create mode 100644 Examples/DemoTCA/xtool.yml create mode 100644 Examples/ExampleSupport/Package.swift create mode 100644 Examples/ExampleSupport/Sources/ExampleDynamicLibrary/ExampleDynamicLibrary.swift create mode 100644 Examples/ExampleSupport/Sources/ExampleDynamicLibrary/response.txt create mode 100644 Examples/ExampleSupport/Sources/ExampleSupport/ExampleApp.swift create mode 100644 Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift create mode 100644 Examples/README.md create mode 100755 Examples/test.sh diff --git a/Examples/.gitignore b/Examples/.gitignore new file mode 100644 index 00000000..8d0b8f23 --- /dev/null +++ b/Examples/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +.build +.swiftpm +*/xtool +*/.bsp +/out diff --git a/Examples/DemoDynamic/Package.swift b/Examples/DemoDynamic/Package.swift new file mode 100644 index 00000000..68469000 --- /dev/null +++ b/Examples/DemoDynamic/Package.swift @@ -0,0 +1,29 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoDynamic", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoDynamic", + targets: ["DemoDynamic"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + ], + targets: [ + .target( + name: "DemoDynamic", + dependencies: [ + "ExampleSupport", + .product(name: "ExampleDynamicLibrary", package: "ExampleSupport"), + ] + ), + ] +) diff --git a/Examples/DemoDynamic/Sources/DemoDynamic/App.swift b/Examples/DemoDynamic/Sources/DemoDynamic/App.swift new file mode 100644 index 00000000..4843d4c1 --- /dev/null +++ b/Examples/DemoDynamic/Sources/DemoDynamic/App.swift @@ -0,0 +1,12 @@ +import ExampleDynamicLibrary +import ExampleSupport +import SwiftUI + +@main struct DemoApp: ExampleApp { + init() { + let message = exampleDynamicLibraryFunction() + guard message == "Hello from the dynamic library\n" else { + fatalError("Unexpected message from dynamic library: \(message)") + } + } +} diff --git a/Examples/DemoDynamic/xtool.yml b/Examples/DemoDynamic/xtool.yml new file mode 100644 index 00000000..30b819b4 --- /dev/null +++ b/Examples/DemoDynamic/xtool.yml @@ -0,0 +1,2 @@ +version: 1 +bundleID: sh.xtool.examples.DemoDynamic diff --git a/Examples/DemoExtensions/Package.swift b/Examples/DemoExtensions/Package.swift new file mode 100644 index 00000000..3319b5e1 --- /dev/null +++ b/Examples/DemoExtensions/Package.swift @@ -0,0 +1,33 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoExtensions", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoExtensions", + targets: ["DemoExtensions"] + ), + .library( + name: "DemoExtensionsWidget", + targets: ["DemoExtensionsWidget"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + ], + targets: [ + .target( + name: "DemoExtensions", + dependencies: [ + "ExampleSupport", + ] + ), + .target(name: "DemoExtensionsWidget"), + ] +) diff --git a/Examples/DemoExtensions/Sources/DemoExtensions/App.swift b/Examples/DemoExtensions/Sources/DemoExtensions/App.swift new file mode 100644 index 00000000..f9222aea --- /dev/null +++ b/Examples/DemoExtensions/Sources/DemoExtensions/App.swift @@ -0,0 +1,4 @@ +import SwiftUI +import ExampleSupport + +@main struct DemoApp: ExampleApp {} diff --git a/Examples/DemoExtensions/Sources/DemoExtensionsWidget/Widget.swift b/Examples/DemoExtensions/Sources/DemoExtensionsWidget/Widget.swift new file mode 100644 index 00000000..5a7233f4 --- /dev/null +++ b/Examples/DemoExtensions/Sources/DemoExtensionsWidget/Widget.swift @@ -0,0 +1,51 @@ +import WidgetKit +import SwiftUI + +@main struct Bundle: WidgetBundle { + var body: some Widget { + DemoWidget() + } +} + +struct DemoWidget: Widget { + var body: some WidgetConfiguration { + StaticConfiguration( + kind: "DemoWidget", + provider: Provider() + ) { entry in + VStack { + Text(entry.date, style: .date) + } + .containerBackground(.fill.tertiary, for: .widget) + } + .configurationDisplayName("DemoWidget") + .description("This is an example widget.") + } + + struct Entry: TimelineEntry { + var date = Date() + } + + struct Provider: TimelineProvider { + func placeholder(in context: Context) -> Entry { + Entry() + } + + func getSnapshot( + in context: Context, + completion: @escaping (Entry) -> Void + ) { + completion(Entry()) + } + + func getTimeline( + in context: Context, + completion: @escaping (Timeline) -> Void + ) { + completion(Timeline( + entries: [Entry()], + policy: .after(.now + 3600) + )) + } + } +} diff --git a/Examples/DemoExtensions/Support/DemoExtensionsWidget-Info.plist b/Examples/DemoExtensions/Support/DemoExtensionsWidget-Info.plist new file mode 100644 index 00000000..0f118fb7 --- /dev/null +++ b/Examples/DemoExtensions/Support/DemoExtensionsWidget-Info.plist @@ -0,0 +1,11 @@ + + + + + NSExtension + + NSExtensionPointIdentifier + com.apple.widgetkit-extension + + + diff --git a/Examples/DemoExtensions/xtool.yml b/Examples/DemoExtensions/xtool.yml new file mode 100644 index 00000000..8a641a55 --- /dev/null +++ b/Examples/DemoExtensions/xtool.yml @@ -0,0 +1,6 @@ +version: 1 +bundleID: sh.xtool.examples.DemoExtensions +product: DemoExtensions +extensions: +- product: DemoExtensionsWidget + infoPath: Support/DemoExtensionsWidget-Info.plist diff --git a/Examples/DemoResources/Package.swift b/Examples/DemoResources/Package.swift new file mode 100644 index 00000000..b927075c --- /dev/null +++ b/Examples/DemoResources/Package.swift @@ -0,0 +1,31 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoResources", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoResources", + targets: ["DemoResources"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + ], + targets: [ + .target( + name: "DemoResources", + dependencies: [ + "ExampleSupport", + ], + resources: [ + .process("bundled.txt"), + ] + ), + ] +) diff --git a/Examples/DemoResources/Resources/top.txt b/Examples/DemoResources/Resources/top.txt new file mode 100644 index 00000000..07dcd7ed --- /dev/null +++ b/Examples/DemoResources/Resources/top.txt @@ -0,0 +1 @@ +Top level resource diff --git a/Examples/DemoResources/Sources/DemoResources/App.swift b/Examples/DemoResources/Sources/DemoResources/App.swift new file mode 100644 index 00000000..89ac6b5e --- /dev/null +++ b/Examples/DemoResources/Sources/DemoResources/App.swift @@ -0,0 +1,34 @@ +import ExampleSupport +import SwiftUI + +@main struct DemoApp: App { + var body: some Scene { + WindowGroup { + Text("Hello, world!") + .task { + guard let bundled = #bundle.url(forResource: "bundled", withExtension: "txt") + else { fatalError("Missing bundled.txt resource") } + assertContents(of: bundled, equal: "Bundled resource\n") + + guard let top = Bundle.main.url(forResource: "top", withExtension: "txt") + else { fatalError("Missing top.txt resource") } + assertContents(of: top, equal: "Top level resource\n") + + Example.pass() + } + } + } +} + +private func assertContents(of url: URL, equal value: String) { + let name = url.lastPathComponent + let contents: String + do { + contents = try String(contentsOf: url, encoding: .utf8) + } catch { + fatalError("Failed to read \(name): \(error)") + } + guard contents == value else { + fatalError("\(name) had unexpected contents: \(contents)") + } +} diff --git a/Examples/DemoResources/Sources/DemoResources/bundled.txt b/Examples/DemoResources/Sources/DemoResources/bundled.txt new file mode 100644 index 00000000..02121397 --- /dev/null +++ b/Examples/DemoResources/Sources/DemoResources/bundled.txt @@ -0,0 +1 @@ +Bundled resource diff --git a/Examples/DemoResources/xtool.yml b/Examples/DemoResources/xtool.yml new file mode 100644 index 00000000..c63c6692 --- /dev/null +++ b/Examples/DemoResources/xtool.yml @@ -0,0 +1,4 @@ +version: 1 +bundleID: sh.xtool.examples.DemoResources +resources: +- Resources/top.txt diff --git a/Examples/DemoSimple/Package.swift b/Examples/DemoSimple/Package.swift new file mode 100644 index 00000000..961e79d4 --- /dev/null +++ b/Examples/DemoSimple/Package.swift @@ -0,0 +1,28 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoSimple", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoSimple", + targets: ["DemoSimple"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + ], + targets: [ + .target( + name: "DemoSimple", + dependencies: [ + "ExampleSupport", + ] + ), + ] +) diff --git a/Examples/DemoSimple/Sources/DemoSimple/App.swift b/Examples/DemoSimple/Sources/DemoSimple/App.swift new file mode 100644 index 00000000..1ecb12cf --- /dev/null +++ b/Examples/DemoSimple/Sources/DemoSimple/App.swift @@ -0,0 +1,4 @@ +import ExampleSupport +import SwiftUI + +@main struct DemoApp: ExampleApp {} diff --git a/Examples/DemoSimple/xtool.yml b/Examples/DemoSimple/xtool.yml new file mode 100644 index 00000000..815fcc3f --- /dev/null +++ b/Examples/DemoSimple/xtool.yml @@ -0,0 +1,2 @@ +version: 1 +bundleID: sh.xtool.examples.DemoSimple diff --git a/Examples/DemoTCA/Package.resolved b/Examples/DemoTCA/Package.resolved new file mode 100644 index 00000000..53633835 --- /dev/null +++ b/Examples/DemoTCA/Package.resolved @@ -0,0 +1,132 @@ +{ + "originHash" : "4a80ae2e9e4c4e1e4e29c32e93dc624202a42a942e44d358035684ec5e0746f7", + "pins" : [ + { + "identity" : "combine-schedulers", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/combine-schedulers", + "state" : { + "revision" : "114354e8c1667a2edc4993700fb9fa4f90157b56", + "version" : "1.2.2" + } + }, + { + "identity" : "swift-case-paths", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-case-paths", + "state" : { + "revision" : "cb4cba6a8a46a86db0d37dbe09935ead6b24c91a", + "version" : "1.10.0" + } + }, + { + "identity" : "swift-clocks", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-clocks", + "state" : { + "revision" : "82440fa0a8b1c381a6d1e8e0fc7bbba53ec63204", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-collections", + "state" : { + "revision" : "98ef3c98609a1e31b7e157b5b619579001a789d6", + "version" : "1.7.1" + } + }, + { + "identity" : "swift-composable-architecture", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-composable-architecture.git", + "state" : { + "revision" : "377da4061db10d26337a71bb279c506bb951f50f", + "version" : "1.26.2" + } + }, + { + "identity" : "swift-concurrency-extras", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-concurrency-extras", + "state" : { + "revision" : "5fa253428866f2360c3754e88537f700ed2656b5", + "version" : "1.4.1" + } + }, + { + "identity" : "swift-custom-dump", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-custom-dump", + "state" : { + "revision" : "4fb9e7cbb4b8f5a7354005db39447f570d6b3e19", + "version" : "1.7.3" + } + }, + { + "identity" : "swift-dependencies", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-dependencies", + "state" : { + "revision" : "b476cc5761057d8d857de063fe7ac8daecf5c06b", + "version" : "1.17.1" + } + }, + { + "identity" : "swift-identified-collections", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-identified-collections", + "state" : { + "revision" : "322d9ffeeba85c9f7c4984b39422ec7cc3c56597", + "version" : "1.1.1" + } + }, + { + "identity" : "swift-issue-reporting", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-issue-reporting", + "state" : { + "revision" : "75b000cea2ca6d7527a57e1b9cc0bdf21a9abe9f", + "version" : "2.1.1" + } + }, + { + "identity" : "swift-navigation", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-navigation", + "state" : { + "revision" : "7e95e5e9ff0a64f6ad8bca59083f8d6ba381b6ff", + "version" : "2.11.2" + } + }, + { + "identity" : "swift-perception", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-perception", + "state" : { + "revision" : "597afd46249f4a71885b95d26b26cc5b893daf32", + "version" : "2.0.12" + } + }, + { + "identity" : "swift-sharing", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-sharing", + "state" : { + "revision" : "3552faf8a6c18ce896ec5a72bd8c34755f0f03e0", + "version" : "2.10.1" + } + }, + { + "identity" : "swift-syntax", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swiftlang/swift-syntax", + "state" : { + "revision" : "050f1a346fbbac0ca2cfb15a95274f7bd1cf0ccf", + "version" : "604.0.0" + } + } + ], + "version" : 3 +} diff --git a/Examples/DemoTCA/Package.swift b/Examples/DemoTCA/Package.swift new file mode 100644 index 00000000..cef2c3cd --- /dev/null +++ b/Examples/DemoTCA/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoTCA", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoTCA", + targets: ["DemoTCA"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + .package(url: "https://github.com/pointfreeco/swift-composable-architecture.git", from: "1.0.0"), + ], + targets: [ + .target( + name: "DemoTCA", + dependencies: [ + "ExampleSupport", + .product(name: "ComposableArchitecture", package: "swift-composable-architecture"), + ], + ), + ] +) diff --git a/Examples/DemoTCA/Sources/DemoTCA/ContentView.swift b/Examples/DemoTCA/Sources/DemoTCA/ContentView.swift new file mode 100644 index 00000000..ef44c9d7 --- /dev/null +++ b/Examples/DemoTCA/Sources/DemoTCA/ContentView.swift @@ -0,0 +1,24 @@ +import SwiftUI +import ComposableArchitecture +import ExampleSupport + +struct ContentView: View { + let store: StoreOf + + var body: some View { + VStack { + Image(systemName: "globe") + .imageScale(.large) + .foregroundStyle(.tint) + Text("Count: \(store.count)") + } + .padding() + .onAppear { + store.send(.appear) + } + .onChange(of: store.count) { _, count in + guard count == 1 else { return } + Example.pass() + } + } +} diff --git a/Examples/DemoTCA/Sources/DemoTCA/CounterFeature.swift b/Examples/DemoTCA/Sources/DemoTCA/CounterFeature.swift new file mode 100644 index 00000000..fb1d06db --- /dev/null +++ b/Examples/DemoTCA/Sources/DemoTCA/CounterFeature.swift @@ -0,0 +1,31 @@ +import ComposableArchitecture + +@Reducer +struct CounterFeature { + @ObservableState + struct State { + var count = 0 + } + + enum Action { + case appear + case timerElapsed + } + + @Dependency(\.continuousClock) var continuousClock + + var body: some Reducer { + Reduce { state, action in + switch action { + case .appear: + return .run { [continuousClock] send in + try await continuousClock.sleep(for: .seconds(1)) + await send(.timerElapsed) + } + case .timerElapsed: + state.count += 1 + return .none + } + } + } +} diff --git a/Examples/DemoTCA/Sources/DemoTCA/DemoTCAApp.swift b/Examples/DemoTCA/Sources/DemoTCA/DemoTCAApp.swift new file mode 100644 index 00000000..ed1ab9ea --- /dev/null +++ b/Examples/DemoTCA/Sources/DemoTCA/DemoTCAApp.swift @@ -0,0 +1,15 @@ +import SwiftUI +import ComposableArchitecture + +@main +struct DemoTCAApp: App { + private let store = Store(initialState: CounterFeature.State()) { + CounterFeature() + } + + var body: some Scene { + WindowGroup { + ContentView(store: store) + } + } +} diff --git a/Examples/DemoTCA/xtool.yml b/Examples/DemoTCA/xtool.yml new file mode 100644 index 00000000..5662c543 --- /dev/null +++ b/Examples/DemoTCA/xtool.yml @@ -0,0 +1,2 @@ +version: 1 +bundleID: sh.xtool.examples.DemoTCA diff --git a/Examples/ExampleSupport/Package.swift b/Examples/ExampleSupport/Package.swift new file mode 100644 index 00000000..a998d784 --- /dev/null +++ b/Examples/ExampleSupport/Package.swift @@ -0,0 +1,31 @@ +// swift-tools-version: 6.4 + +import PackageDescription + +let package = Package( + name: "ExampleSupport", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "ExampleSupport", + targets: ["ExampleSupport"] + ), + .library( + name: "ExampleDynamicLibrary", + type: .dynamic, + targets: ["ExampleDynamicLibrary"] + ), + ], + targets: [ + .target(name: "ExampleSupport"), + .target( + name: "ExampleDynamicLibrary", + resources: [ + .process("response.txt"), + ] + ), + ] +) diff --git a/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/ExampleDynamicLibrary.swift b/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/ExampleDynamicLibrary.swift new file mode 100644 index 00000000..449addd0 --- /dev/null +++ b/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/ExampleDynamicLibrary.swift @@ -0,0 +1,9 @@ +import Foundation + +public func exampleDynamicLibraryFunction() -> String { + guard let bundled = #bundle.url(forResource: "response", withExtension: "txt") + else { fatalError("Missing bundled.txt resource") } + // swiftlint:disable:next force_try + let contents = try! String(contentsOf: bundled, encoding: .utf8) + return "Hello \(contents)" +} diff --git a/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/response.txt b/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/response.txt new file mode 100644 index 00000000..d258eaa5 --- /dev/null +++ b/Examples/ExampleSupport/Sources/ExampleDynamicLibrary/response.txt @@ -0,0 +1 @@ +from the dynamic library diff --git a/Examples/ExampleSupport/Sources/ExampleSupport/ExampleApp.swift b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleApp.swift new file mode 100644 index 00000000..edef3ea1 --- /dev/null +++ b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleApp.swift @@ -0,0 +1,25 @@ +import SwiftUI + +public protocol ExampleApp: App {} +extension ExampleApp { + public var body: some Scene { + WindowGroup { + ContentView() + } + } +} + +private struct ContentView: View { + var body: some View { + VStack { + Image(systemName: "globe") + .imageScale(.large) + .foregroundStyle(.tint) + Text("Hello, world!") + } + .padding() + .task { + Example.pass() + } + } +} diff --git a/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift new file mode 100644 index 00000000..dae8aa27 --- /dev/null +++ b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift @@ -0,0 +1,12 @@ +import Foundation + +public enum Example {} +extension Example { + public static func pass() { + Task { + try? await Task.sleep(for: .seconds(1)) + print("xtool test succeeded") + exit(0) + } + } +} diff --git a/Examples/README.md b/Examples/README.md new file mode 100644 index 00000000..7507d5d2 --- /dev/null +++ b/Examples/README.md @@ -0,0 +1,14 @@ +# Examples + +These primarily exist as fixtures for integration tests, but also double as snippets of sample code. + +Each demo is designed to make some assertions shortly after launching, and then exit. On success, stdout will include the message "xtool test succeeded". + +## Testing + +``` +./Examples/test.sh [--build | --run] [DemoName] +``` + +- Omit `--build`/`--run` to build _and_ run +- Omit the demo name to loop through all demos diff --git a/Examples/test.sh b/Examples/test.sh new file mode 100755 index 00000000..453444af --- /dev/null +++ b/Examples/test.sh @@ -0,0 +1,118 @@ +#!/bin/bash + +set -euo pipefail +shopt -s nullglob + +usage() { + echo "Usage: $0 [--build | --run] [DemoName]" +} + +mode=both +case "${1:-}" in + --build) mode=build; shift ;; + --run) mode=run; shift ;; + -h|--help) usage; exit 0 ;; +esac + +if [[ $# -gt 1 || (${1:-} != "" && (${1:-} != Demo* || ${1:-} == */*)) ]]; then + usage >&2 + exit 1 +fi + +cd "$(dirname "$0")" +examples_dir="$PWD" +out_dir="$examples_dir/out" + +if [[ -n "${1:-}" ]]; then + demos=("$1") +elif [[ "$mode" == run ]]; then + demos=() + for app in "$out_dir"/*.app; do + name="${app##*/}" + demos+=("${name%.app}") + done +else + demos=() + for demo in "$examples_dir"/Demo*/; do + [[ -f "$demo/Package.swift" ]] || continue + demo="${demo%/}" + demos+=("${demo##*/}") + done +fi + +if [[ ${#demos[@]} -eq 0 ]]; then + echo "No demos found. Run $0 --build first if using --run." >&2 + exit 1 +fi + +logs_dir="$(mktemp -d)" +trap 'rm -rf "$logs_dir"' EXIT +trap 'exit 130' INT +trap 'exit 143' TERM + +build_demo() { + local demo="$1" + echo "Building $demo..." + mkdir -p "$out_dir" || return 1 + # Do not leave an older successful build available after a failed rebuild. + rm -rf "$out_dir/$demo.app" || return 1 + (cd "$examples_dir/$demo" && xtool dev build --triple arm64-apple-ios-simulator) || return 1 + cp -R "$examples_dir/$demo/xtool/$demo.app" "$out_dir/$demo.app" +} + +run_demo() ( + demo="$1" + app="$out_dir/$demo.app" + log="$logs_dir/$demo-run.log" + if [[ ! -d "$app" ]]; then + echo "Missing $app. Run $0 --build $demo first." >&2 + exit 1 + fi + bundle_id=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$app/Info.plist") || exit 1 + echo "Running $demo..." + xcrun simctl install booted "$app" || exit 1 + xcrun simctl launch --terminate-running-process --console booted "$bundle_id" >"$log" 2>&1 & + launch_pid=$! + # A demo should exit shortly after launch. Bound failures that hang instead. + ( + sleep_pid="" + trap '[[ -z "$sleep_pid" ]] || kill "$sleep_pid" 2>/dev/null || true' EXIT + sleep 60 & + sleep_pid=$! + wait "$sleep_pid" + sleep_pid="" + touch "$log.timeout" + kill -TERM "$launch_pid" 2>/dev/null || exit 0 + sleep 5 & + sleep_pid=$! + wait "$sleep_pid" + sleep_pid="" + kill -KILL "$launch_pid" 2>/dev/null || true + ) >/dev/null 2>&1 & + timeout_pid=$! + trap 'kill "$timeout_pid" "$launch_pid" 2>/dev/null || true' EXIT + status=0 + wait "$launch_pid" || status=$? + kill "$timeout_pid" 2>/dev/null || true + wait "$timeout_pid" 2>/dev/null || true + cat "$log" + if [[ -f "$log.timeout" ]]; then + echo "$demo did not terminate within 60 seconds." >&2 + exit 1 + fi + [[ "$status" -eq 0 ]] && grep -Fxq 'xtool test succeeded' "$log" +) + +status=0 +for demo in "${demos[@]}"; do + if [[ "$mode" != run ]] && ! build_demo "$demo"; then + echo "Build failed: $demo" >&2 + status=1 + continue + fi + if [[ "$mode" != build ]] && ! run_demo "$demo"; then + echo "Test failed: $demo" >&2 + status=1 + fi +done +exit "$status"