TestFunctions.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. import XCTest
  30. private func diff(expected: String, actual: String) throws -> String {
  31. let process = Process()
  32. process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
  33. process.arguments = [
  34. "bash", "-c",
  35. "diff -U5 --label=expected <(echo '\(expected)') --label=actual <(echo '\(actual)')",
  36. ]
  37. let pipe = Pipe()
  38. process.standardOutput = pipe
  39. try process.run()
  40. process.waitUntilExit()
  41. let pipeData = try XCTUnwrap(
  42. pipe.fileHandleForReading.readToEnd(),
  43. """
  44. No output from command:
  45. \(process.executableURL!.path) \(process.arguments!.joined(separator: " "))
  46. """
  47. )
  48. return String(decoding: pipeData, as: UTF8.self)
  49. }
  50. internal func XCTAssertEqualWithDiff(
  51. _ actual: String,
  52. _ expected: String,
  53. file: StaticString = #filePath,
  54. line: UInt = #line
  55. ) throws {
  56. if actual == expected { return }
  57. XCTFail(
  58. """
  59. XCTAssertEqualWithDiff failed (click for diff)
  60. \(try diff(expected: expected, actual: actual))
  61. """,
  62. file: file,
  63. line: line
  64. )
  65. }