Skip to content

Commit 9944d8a

Browse files
committed
Add README and helper method
1 parent 782f588 commit 9944d8a

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

FirebaseVertexAI/Tests/Unit/Snippets/FirebaseAppSnippetsUtil.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@ import Foundation
1717
import XCTest
1818

1919
extension FirebaseApp {
20-
// These snippets are not invoked in CI but may be run manually by placing a
21-
// GoogleService-Info.plist in the the FirebaseVertexAI/Tests/Unit/Resources folder.
22-
static func configureForSnippets() throws {
20+
/// Configures the default `FirebaseApp` for use in snippets tests.
21+
///
22+
/// Uses a `GoogleService-Info.plist` file from the
23+
/// [`Resources`](https://github.com/firebase/firebase-ios-sdk/tree/main/FirebaseVertexAI/Tests/Unit/Resources)
24+
/// directory.
25+
///
26+
/// > Note: This is typically called in a snippet test's set up; overriding
27+
/// > `setUpWithError() throws` works well since it supports throwing errors.
28+
static func configureDefaultAppForSnippets() throws {
2329
guard let plistPath = Bundle.module.path(
2430
forResource: "GoogleService-Info",
2531
ofType: "plist"
@@ -35,4 +41,17 @@ extension FirebaseApp {
3541
return
3642
}
3743
}
44+
45+
/// Deletes the default `FirebaseApp` if configured.
46+
///
47+
/// > Note: This is typically called in a snippet test's tear down; overriding
48+
/// > `tearDown() async throws` works well since deletion is asynchronous.
49+
static func deleteDefaultAppForSnippets() async {
50+
// Checking if `isDefaultAppConfigured()` before calling `FirebaseApp.app()` suppresses a log
51+
// message that "The default Firebase app has not yet been configured." during `tearDown` when
52+
// the tests are skipped. This reduces extraneous noise in the test logs.
53+
if FirebaseApp.isDefaultAppConfigured(), let app = FirebaseApp.app() {
54+
await app.delete()
55+
}
56+
}
3857
}

FirebaseVertexAI/Tests/Unit/Snippets/FunctionCallingSnippets.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ import FirebaseCore
1616
import FirebaseVertexAI
1717
import XCTest
1818

19+
// These snippet tests are intentionally skipped in CI jobs; see the README file in this directory
20+
// for instructions on running them manually.
21+
1922
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2023
final class FunctionCallingSnippets: XCTestCase {
2124
override func setUpWithError() throws {
22-
try FirebaseApp.configureForSnippets()
25+
try FirebaseApp.configureDefaultAppForSnippets()
2326
}
2427

2528
override func tearDown() async throws {
26-
if FirebaseApp.isDefaultAppConfigured(), let app = FirebaseApp.app() {
27-
await app.delete()
28-
}
29+
await FirebaseApp.deleteDefaultAppForSnippets()
2930
}
3031

3132
func testFunctionCalling() async throws {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Vertex AI in Firebase Code Snippet Tests
2+
3+
These "tests" are for verifying that the code snippets provided in our
4+
documentation continue to compile. They are intentionally skipped in CI but can
5+
be manually run to verify expected behavior / outputs.
6+
7+
To run the tests, place a valid `GoogleService-Info.plist` file in the
8+
[`FirebaseVertexAI/Tests/Unit/Resources`](https://github.com/firebase/firebase-ios-sdk/tree/main/FirebaseVertexAI/Tests/Unit/Resources)
9+
folder. They may then be invoked individually or alongside the rest of the unit
10+
tests in Xcode.

FirebaseVertexAI/Tests/Unit/Snippets/StructuredOutputSnippets.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ import FirebaseCore
1616
import FirebaseVertexAI
1717
import XCTest
1818

19+
// These snippet tests are intentionally skipped in CI jobs; see the README file in this directory
20+
// for instructions on running them manually.
21+
1922
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
2023
final class StructuredOutputSnippets: XCTestCase {
2124
override func setUpWithError() throws {
22-
try FirebaseApp.configureForSnippets()
25+
try FirebaseApp.configureDefaultAppForSnippets()
2326
}
2427

2528
override func tearDown() async throws {
26-
if let app = FirebaseApp.app() {
27-
await app.delete()
28-
}
29+
await FirebaseApp.deleteDefaultAppForSnippets()
2930
}
3031

3132
func testStructuredOutputJSONBasic() async throws {

0 commit comments

Comments
 (0)