Pārlūkot izejas kodu

Expose replay xcconfig values via environment variables

Sam King 5 mēneši atpakaļ
vecāks
revīzija
409f210d61

+ 4 - 0
TrioTests/Info.plist

@@ -18,5 +18,9 @@
 	<string>$(ENABLE_REPLAY_TESTS)</string>
 	<key>ReplayTestTimezone</key>
 	<string>$(REPLAY_TEST_TIMEZONE)</string>
+	<key>HttpFilesOffset</key>
+	<string>$(HTTP_FILES_OFFSET)</string>
+	<key>HttpFilesLength</key>
+	<string>$(HTTP_FILES_LENGTH)</string>
 </dict>
 </plist>

+ 9 - 4
TrioTests/OpenAPSSwiftTests/utils/HttpFiles.swift

@@ -13,18 +13,23 @@ struct HttpFiles {
         let url = URL(string: "http://localhost:8123/list")!
         let (data, _) = try await URLSession.shared.data(from: url)
         let allFiles = try JSONDecoder().decode([String].self, from: data)
-        
+
         let files: [String]
         let env = ProcessInfo.processInfo.environment
-        if let offsetString = env["HTTP_FILES_OFFSET"], let lengthString = env["HTTP_FILES_LENGTH"], let offset = Int(offsetString), let length = Int(lengthString) {
+        if let offset = ReplayTests.filesOffset, let length = ReplayTests.filesLength
+        {
             // Both variables exist and are valid integers
             let endIndex = min(offset + length, allFiles.count)
             let startIndex = min(offset, allFiles.count)
-            files = Array(allFiles[startIndex..<endIndex])
+            files = Array(allFiles[startIndex ..< endIndex])
         } else {
             files = allFiles
         }
-        
+
+        if files.count > 5000 {
+            fatalError("too many files: \(files.count) \(ProcessInfo.processInfo.environment)")
+        }
+
         return files
     }
 

+ 53 - 8
TrioTests/OpenAPSSwiftTests/utils/ReplayTests.swift

@@ -1,19 +1,59 @@
 import Foundation
 
-/// Flag to enable replay tests.
-///
-/// These test are only used for debugging so normally they should be disabled. But
-/// if you're debugging the oref-swift functions they are extremely useful. To enable them
-/// add these lines to your ConfigOverride.xcconfig file:
-/// ```
-/// ENABLE_REPLAY_TESTS = YES
-/// ```
 enum ReplayTests {
+    /// Flag to enable replay tests.
+    ///
+    /// These test are only used for debugging so normally they should be disabled. But
+    /// if you're debugging the oref-swift functions they are extremely useful. To enable them
+    /// add these lines to your ConfigOverride.xcconfig file:
+    /// ```
+    /// ENABLE_REPLAY_TESTS = YES
+    /// ```
     static var enabled: Bool {
+        let env = ProcessInfo.processInfo.environment
+        if env["ENABLE_REPLAY_TESTS"] == "YES" {
+            return true
+        }
+
         let bundle = Bundle(for: BundleReference.self)
         return bundle.object(forInfoDictionaryKey: "EnableReplayTests") as? String == "YES"
     }
 
+    /// The offset for pagination of replay input files
+    ///
+    /// Set this offset using an environment variable or the ConfigOverride.xcconfig file.
+    /// For this change to take effect you must also set the length
+    /// ```
+    /// HTTP_FILES_OFFSET = 2000
+    /// ```
+    static var filesOffset: Int? {
+        let env = ProcessInfo.processInfo.environment
+        if let offset = env["HTTP_FILES_OFFSET"].flatMap({ Int($0) }) {
+            return offset
+        }
+
+        let bundle = Bundle(for: BundleReference.self)
+        let offsetString = bundle.object(forInfoDictionaryKey: "HttpFilesOffset") as? String
+        return offsetString.flatMap { Int($0) }
+    }
+
+    /// Length for pagination of replay input files
+    ///
+    /// Set this length using an environment variable or the ConfigOverride.xcconfig file.
+    /// ```
+    /// HTTP_FILES_LENGTH = 3500
+    /// ```
+    static var filesLength: Int? {
+        let env = ProcessInfo.processInfo.environment
+        if let length = env["HTTP_FILES_LENGTH"].flatMap({ Int($0) }) {
+            return length
+        }
+
+        let bundle = Bundle(for: BundleReference.self)
+        let lengthString = bundle.object(forInfoDictionaryKey: "HttpFilesLength") as? String
+        return lengthString.flatMap { Int($0) }
+    }
+
     /// Timezone to use for replay tests.
     ///
     /// This is used to filter replay test files by timezone. If not set, it defaults to "America/Los_Angeles".
@@ -22,6 +62,11 @@ enum ReplayTests {
     /// REPLAY_TEST_TIMEZONE = Europe/Berlin
     /// ```
     static var timezone: String {
+        let env = ProcessInfo.processInfo.environment
+        if let timezone = env["REPLAY_TEST_TIMEZONE"], !timezone.isEmpty {
+            return timezone
+        }
+
         let bundle = Bundle(for: BundleReference.self)
         return bundle.object(forInfoDictionaryKey: "ReplayTestTimezone") as? String ?? "America/Los_Angeles"
     }