TestFunctions.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //===----------------------------------------------------------------------===//
  17. //
  18. // This source file is part of the SwiftOpenAPIGenerator open source project
  19. //
  20. // Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
  21. // Licensed under Apache License v2.0
  22. //
  23. // See LICENSE.txt for license information
  24. // See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
  25. //
  26. // SPDX-License-Identifier: Apache-2.0
  27. //
  28. //===----------------------------------------------------------------------===//
  29. #if os(macOS) || os(Linux) // swift-format doesn't like canImport(Foundation.Process)
  30. import XCTest
  31. private func diff(expected: String, actual: String) throws -> String {
  32. let process = Process()
  33. process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
  34. process.arguments = [
  35. "bash", "-c",
  36. "diff -U5 --label=expected <(echo '\(expected)') --label=actual <(echo '\(actual)')",
  37. ]
  38. let pipe = Pipe()
  39. process.standardOutput = pipe
  40. try process.run()
  41. process.waitUntilExit()
  42. let pipeData = try XCTUnwrap(
  43. pipe.fileHandleForReading.readToEnd(),
  44. """
  45. No output from command:
  46. \(process.executableURL!.path) \(process.arguments!.joined(separator: " "))
  47. """
  48. )
  49. return String(decoding: pipeData, as: UTF8.self)
  50. }
  51. internal func XCTAssertEqualWithDiff(
  52. _ actual: String,
  53. _ expected: String,
  54. file: StaticString = #filePath,
  55. line: UInt = #line
  56. ) throws {
  57. if actual == expected { return }
  58. XCTFail(
  59. """
  60. XCTAssertEqualWithDiff failed (click for diff)
  61. \(try diff(expected: expected, actual: actual))
  62. """,
  63. file: file,
  64. line: line
  65. )
  66. }
  67. #endif // os(macOS) || os(Linux)