FirebaseAppSnippetsUtil.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseCore
  15. import Foundation
  16. import XCTest
  17. extension FirebaseApp {
  18. /// Configures the default `FirebaseApp` for use in snippets tests.
  19. ///
  20. /// Uses a `GoogleService-Info.plist` file from the
  21. /// [`Resources`](https://github.com/firebase/firebase-ios-sdk/tree/main/FirebaseVertexAI/Tests/Unit/Resources)
  22. /// directory.
  23. ///
  24. /// > Note: This is typically called in a snippet test's set up; overriding
  25. /// > `setUpWithError() throws` works well since it supports throwing errors.
  26. static func configureDefaultAppForSnippets() throws {
  27. guard let plistPath = BundleTestUtil.bundle().path(
  28. forResource: "GoogleService-Info",
  29. ofType: "plist"
  30. ) else {
  31. throw XCTSkip("No GoogleService-Info.plist found in FirebaseVertexAI/Tests/Unit/Resources.")
  32. }
  33. let options = try XCTUnwrap(FirebaseOptions(contentsOfFile: plistPath))
  34. FirebaseApp.configure(options: options)
  35. guard FirebaseApp.isDefaultAppConfigured() else {
  36. XCTFail("Default Firebase app not configured.")
  37. return
  38. }
  39. }
  40. /// Deletes the default `FirebaseApp` if configured.
  41. ///
  42. /// > Note: This is typically called in a snippet test's tear down; overriding
  43. /// > `tearDown() async throws` works well since deletion is asynchronous.
  44. static func deleteDefaultAppForSnippets() async {
  45. // Checking if `isDefaultAppConfigured()` before calling `FirebaseApp.app()` suppresses a log
  46. // message that "The default Firebase app has not yet been configured." during `tearDown` when
  47. // the tests are skipped. This reduces extraneous noise in the test logs.
  48. if FirebaseApp.isDefaultAppConfigured(), let app = FirebaseApp.app() {
  49. await app.delete()
  50. }
  51. }
  52. }