LeaksTests.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. func testForLeaks() {
  32. // Sets up an atexit handler that invokes the leaks tool.
  33. atexit {
  34. @discardableResult
  35. func leaksTo(_ file: String) -> Process {
  36. let out = FileHandle(forWritingAtPath: file)!
  37. defer {
  38. if #available(macOS 10.15, *) {
  39. try! out.close()
  40. } else {
  41. // Fallback on earlier versions
  42. }
  43. }
  44. let process = Process()
  45. process.launchPath = "/usr/bin/leaks"
  46. process.arguments = ["\(getpid())"]
  47. process.standardOutput = out
  48. process.standardError = out
  49. process.launch()
  50. process.waitUntilExit()
  51. return process
  52. }
  53. let process = leaksTo("/dev/null")
  54. guard process.terminationReason == .exit && [0, 1].contains(process.terminationStatus) else {
  55. print("Process terminated: \(process.terminationReason): \(process.terminationStatus)")
  56. exit(255)
  57. }
  58. if process.terminationStatus == 1 {
  59. print("================")
  60. print("Leaks Detected!!!")
  61. leaksTo("/dev/tty")
  62. }
  63. exit(process.terminationStatus)
  64. }
  65. }
  66. }
  67. #endif