LeaksTests.swift 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // LeaksTests.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import XCTest
  25. // Only build when built through SPM, as tests run through Xcode don't like this.
  26. // Add LEAKS flag once we figure out a way to automate this.
  27. // Can run by invoking swift test -c debug -Xswiftc -DLEAKS in the Alamofire directory.
  28. // Sample code from the Swift forums: https://forums.swift.org/t/test-for-memory-leaks-in-ci/36526/19
  29. #if SWIFT_PACKAGE && LEAKS && os(macOS)
  30. final class LeaksTests: XCTestCase {
  31. @MainActor
  32. func testForLeaks() {
  33. // Sets up an atexit handler that invokes the leaks tool.
  34. atexit {
  35. @discardableResult
  36. func leaksTo(_ file: String) -> Process {
  37. let out = FileHandle(forWritingAtPath: file)!
  38. defer {
  39. if #available(macOS 10.15, *) {
  40. try! out.close()
  41. } else {
  42. // Fallback on earlier versions
  43. }
  44. }
  45. let process = Process()
  46. process.launchPath = "/usr/bin/leaks"
  47. process.arguments = ["\(getpid())"]
  48. process.standardOutput = out
  49. process.standardError = out
  50. process.launch()
  51. process.waitUntilExit()
  52. return process
  53. }
  54. let process = leaksTo("/dev/null")
  55. guard process.terminationReason == .exit && [0, 1].contains(process.terminationStatus) else {
  56. print("Process terminated: \(process.terminationReason): \(process.terminationStatus)")
  57. exit(255)
  58. }
  59. if process.terminationStatus == 1 {
  60. print("================")
  61. print("Leaks Detected!!!")
  62. leaksTo("/dev/tty")
  63. }
  64. exit(process.terminationStatus)
  65. }
  66. }
  67. }
  68. #endif