TestUtilities.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2025 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 Foundation
  15. enum Constants {
  16. static let firebaseNamespace = "firebase"
  17. static let perfNamespace = "fireperf"
  18. static let defaultFirebaseAppName = "__FIRAPP_DEFAULT"
  19. static let secondFirebaseAppName = "secondFIRApp"
  20. }
  21. public class TestUtilities {
  22. public static func generatedTestAppName(for testName: String) -> String {
  23. // Filter out any characters not valid for FIRApp's naming scheme.
  24. let invalidCharacters = CharacterSet.alphanumerics.inverted
  25. // This will result in a string with the class name, a space, and the test
  26. // name. We only care about the test name so split it into components and
  27. // return the last item.
  28. let friendlyTestName = testName.trimmingCharacters(in: invalidCharacters)
  29. let components = friendlyTestName.components(separatedBy: " ")
  30. return components.last ?? ""
  31. }
  32. public static func remoteConfigTestDatabasePath() -> String {
  33. #if os(tvOS)
  34. let dirPaths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
  35. #else
  36. let dirPaths = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory,
  37. .userDomainMask, true)
  38. #endif
  39. let storageDirPath = dirPaths[0]
  40. let dbPath = URL(fileURLWithPath: storageDirPath)
  41. .appendingPathComponent("Google/RemoteConfig")
  42. .appendingPathComponent("test-\(Date().timeIntervalSince1970 * 1000).sqlite3").path
  43. removeDatabase(at: dbPath)
  44. return dbPath
  45. }
  46. public static func removeDatabase(at dbPath: String) {
  47. try? FileManager.default.removeItem(atPath: dbPath)
  48. }
  49. public static func userDefaultsTestSuiteName() -> String {
  50. "group.\(Bundle.main.bundleIdentifier!).test-\(Date().timeIntervalSince1970 * 1000)"
  51. }
  52. }