Skip to content

Commit

Permalink
Suppress #file/#filePath warnings on recent compilers (grpc#782)
Browse files Browse the repository at this point in the history
Motivation:

Newer compilers warn when a `#file` is passed where a `#filePath` is
expected, such as in `XCTAssert*`.

Modifications:

- Remove use of `#file` for helpers which are private to a file
- Add a `magicFile()` which does the right thing based on Swift version

Result:

No warnings on recent compilers
  • Loading branch information
glbrntt authored May 4, 2020
1 parent d975125 commit 1b18470
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Tests/GRPCTests/EventLoopFuture+Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension EventLoopFuture where Value: Equatable {
/// - Parameters:
/// - expected: The expected value.
/// - expectation: A test expectation to fulfill once the future has completed.
func assertEqual(_ expected: Value, fulfill expectation: XCTestExpectation, file: StaticString = #file, line: UInt = #line) {
func assertEqual(_ expected: Value, fulfill expectation: XCTestExpectation, file: StaticString = magicFile(), line: UInt = #line) {
self.whenComplete { result in
defer {
expectation.fulfill()
Expand All @@ -50,7 +50,7 @@ extension EventLoopFuture {
/// - Parameters:
/// - expectation: A test expectation to fulfill once the future has completed.
/// - handler: A block to run additional verification on the error. Defaults to no-op.
func assertError(fulfill expectation: XCTestExpectation, file: StaticString = #file, line: UInt = #line, handler: @escaping (Error) -> Void = { _ in }) {
func assertError(fulfill expectation: XCTestExpectation, file: StaticString = magicFile(), line: UInt = #line, handler: @escaping (Error) -> Void = { _ in }) {
self.whenComplete { result in
defer {
expectation.fulfill()
Expand Down
18 changes: 9 additions & 9 deletions Tests/GRPCTests/FunctionalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,21 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
XCTAssertNoThrow(try doTestClientStreaming(messages: lotsOfStrings))
}

func doTestServerStreaming(messages: [String], file: StaticString = #file, line: UInt = #line) throws {
private func doTestServerStreaming(messages: [String], line: UInt = #line) throws {
let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
let statusExpectation = self.makeStatusExpectation()

var iterator = messages.enumerated().makeIterator()
let call = client.expand(Echo_EchoRequest(text: messages.joined(separator: " "))) { response in
if let (index, message) = iterator.next() {
XCTAssertEqual(Echo_EchoResponse(text: "Swift echo expand (\(index)): \(message)"), response, file: file, line: line)
XCTAssertEqual(Echo_EchoResponse(text: "Swift echo expand (\(index)): \(message)"), response, line: line)
responseExpectation.fulfill()
} else {
XCTFail("Too many responses received", file: file, line: line)
XCTFail("Too many responses received", line: line)
}
}

call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, line: line)
self.wait(for: [responseExpectation, statusExpectation], timeout: self.defaultTestTimeout)
}

Expand All @@ -165,7 +165,7 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
XCTAssertNoThrow(try doTestServerStreaming(messages: lotsOfStrings))
}

private func doTestBidirectionalStreaming(messages: [String], waitForEachResponse: Bool = false, file: StaticString = #file, line: UInt = #line) throws {
private func doTestBidirectionalStreaming(messages: [String], waitForEachResponse: Bool = false, line: UInt = #line) throws {
let responseExpectation = self.makeResponseExpectation(expectedFulfillmentCount: messages.count)
let statusExpectation = self.makeStatusExpectation()

Expand All @@ -174,19 +174,19 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
var iterator = messages.enumerated().makeIterator()
let call = client.update { response in
if let (index, message) = iterator.next() {
XCTAssertEqual(Echo_EchoResponse(text: "Swift echo update (\(index)): \(message)"), response, file: file, line: line)
XCTAssertEqual(Echo_EchoResponse(text: "Swift echo update (\(index)): \(message)"), response, line: line)
responseExpectation.fulfill()
responseReceived?.signal()
} else {
XCTFail("Too many responses received", file: file, line: line)
XCTFail("Too many responses received", line: line)
}
}

call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, file: file, line: line)
call.status.map { $0.code }.assertEqual(.ok, fulfill: statusExpectation, line: line)

messages.forEach { part in
call.sendMessage(Echo_EchoRequest(text: part), promise: nil)
XCTAssertNotEqual(responseReceived?.wait(timeout: .now() + .seconds(1)), .some(.timedOut), file: file, line: line)
XCTAssertNotEqual(responseReceived?.wait(timeout: .now() + .seconds(1)), .some(.timedOut), line: line)
}
call.sendEnd(promise: nil)

Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCTests/GRPCInteroperabilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class GRPCInsecureInteroperabilityTests: GRPCTestCase {
super.tearDown()
}

func doRunTest(_ testCase: InteroperabilityTestCase, file: StaticString = #file, line: UInt = #line) {
private func doRunTest(_ testCase: InteroperabilityTestCase, line: UInt = #line) {
// Does the server support the test?
let implementedFeatures = TestServiceProvider.implementedFeatures
let missingFeatures = testCase.requiredServerFeatures.subtracting(implementedFeatures)
Expand All @@ -82,7 +82,7 @@ class GRPCInsecureInteroperabilityTests: GRPCTestCase {
)
test.configure(builder: builder)
self.clientConnection = builder.connect(host: "localhost", port: self.serverPort)
XCTAssertNoThrow(try test.run(using: self.clientConnection), file: file, line: line)
XCTAssertNoThrow(try test.run(using: self.clientConnection), line: line)
}

func testEmptyUnary() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCTests/GRPCTypeSizeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import XCTest
class GRPCTypeSizeTests: GRPCTestCase {
let existentialContainerBufferSize = 24

func checkSize<T>(of: T.Type, file: StaticString = #file, line: UInt = #line) {
XCTAssertLessThanOrEqual(MemoryLayout<T>.size, self.existentialContainerBufferSize, file: file, line: line)
private func checkSize<T>(of: T.Type, line: UInt = #line) {
XCTAssertLessThanOrEqual(MemoryLayout<T>.size, self.existentialContainerBufferSize, line: line)
}

// `GRPCStatus` isn't wrapped in `NIOAny` but is passed around through functions taking a type
Expand Down
3 changes: 1 addition & 2 deletions Tests/GRPCTests/HeaderNormalizationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ import XCTest
class EchoMetadataValidator: Echo_EchoProvider {
private func assertCustomMetadataIsLowercased(
_ headers: HTTPHeaders,
file: StaticString = #file,
line: UInt = #line
) {
// Header lookup is case-insensitive so we need to pull out the values we know the client sent
// as custom-metadata and then compare a new set of headers.
let customMetadata = HTTPHeaders(headers.filter { name, value in value == "client" })
XCTAssertEqual(customMetadata, ["client": "client"], file: file, line: line)
XCTAssertEqual(customMetadata, ["client": "client"], line: line)
}

func get(
Expand Down
8 changes: 4 additions & 4 deletions Tests/GRPCTests/LengthPrefixedMessageReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ class LengthPrefixedMessageReaderTests: GRPCTestCase {
] + twoByteMessage
}

func assertMessagesEqual(expected expectedBytes: [UInt8], actual buffer: ByteBuffer?, file: StaticString = #file, line: UInt = #line) {
private func assertMessagesEqual(expected expectedBytes: [UInt8], actual buffer: ByteBuffer?, line: UInt = #line) {
guard let buffer = buffer else {
XCTFail("buffer is nil", file: file, line: line)
XCTFail("buffer is nil", line: line)
return
}

guard let bytes = buffer.getBytes(at: buffer.readerIndex, length: expectedBytes.count) else {
XCTFail("Expected \(expectedBytes.count) bytes, but only \(buffer.readableBytes) bytes are readable", file: file, line: line)
XCTFail("Expected \(expectedBytes.count) bytes, but only \(buffer.readableBytes) bytes are readable", line: line)
return
}

XCTAssertEqual(expectedBytes, bytes, file: file, line: line)
XCTAssertEqual(expectedBytes, bytes, line: line)
}

func testNextMessageReturnsNilWhenNoBytesAppended() throws {
Expand Down
31 changes: 31 additions & 0 deletions Tests/GRPCTests/MagicFile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// SE-0274 renames #file to #filePath so that #file may refer to the name of a file instead of
// its path. From Swift 5.3+ XCTAssert* accepts #filePath and warns when a #file is passed to it.
// These functions are used to work around that.
//
// https://github.com/apple/swift-evolution/blob/master/proposals/0274-magic-file.md

#if swift(>=5.3)
func magicFile(file: StaticString = #filePath) -> StaticString {
return file
}
#else
func magicFile(file: StaticString = #file) -> StaticString {
return file
}
#endif
2 changes: 1 addition & 1 deletion Tests/GRPCTests/SampleCertificate+Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import XCTest
import GRPCSampleData

extension SampleCertificate {
func assertNotExpired(file: StaticString = #file, line: UInt = #line) {
func assertNotExpired(file: StaticString = magicFile(), line: UInt = #line) {
XCTAssertFalse(self.isExpired, "Certificate expired at \(self.notAfter)", file: file, line: line)
}
}
2 changes: 1 addition & 1 deletion Tests/GRPCTests/ServerWebTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ServerWebTests: EchoTestCaseBase {
handler(data, error)
sem.signal()
}.resume()
_ = sem.wait()
sem.wait()
}
}

Expand Down

0 comments on commit 1b18470

Please sign in to comment.