Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up for swift-format #1712

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
62 changes: 62 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"version" : 1,
"indentation" : {
"spaces" : 4
},
"tabWidth" : 4,
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"spacesAroundRangeFormationOperators" : false,
"indentConditionalCompilationBlocks" : false,
"indentSwitchCaseLabels" : false,
"lineBreakAroundMultilineExpressionChainComponents" : false,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : true,
"lineLength" : 120,
"maximumBlankLines" : 1,
"respectsExistingLineBreaks" : true,
"prioritizeKeepingFunctionOutputTogether" : true,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : false,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : false,
"NoParensAroundConditions" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : true,
"OneVariableDeclarationPerLine" : true,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"UseEarlyExits" : false,
"UseExplicitNilCheckInConditions" : false,
"UseLetInEveryBoundCaseVariable" : false,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : false,
"UseSynthesizedInitializer" : false,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : false,
"ValidateDocumentationComments" : false
}
}
10 changes: 5 additions & 5 deletions CompileTests/InternalImportsByDefault/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ let package = Package(
.executableTarget(
name: "InternalImportsByDefault",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "SwiftProtobuf", package: "swift-protobuf")
],
exclude: [
"Protos/SomeProtoWithBytes.proto",
"Protos/ServiceOnly.proto"
"Protos/ServiceOnly.proto",
],
swiftSettings: [
.enableExperimentalFeature("InternalImportsByDefault"),
.enableExperimentalFeature("AccessLevelOnImport"),
// Enable warnings as errors so the build fails if warnings are
// present in generated code.
.unsafeFlags(["-warnings-as-errors"])
.unsafeFlags(["-warnings-as-errors"]),
],
plugins: [
.plugin(name: "SwiftProtobufPlugin", package: "swift-protobuf")
]
),
)
]
)
#else
Expand All @@ -48,7 +48,7 @@ let package = Package(
exclude: [
"swift-protobuf-config.json",
"Protos/SomeProtoWithBytes.proto",
"Protos/ServiceOnly.proto"
"Protos/ServiceOnly.proto",
]
)
]
Expand Down
2 changes: 1 addition & 1 deletion CompileTests/MultiModule/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
.target(
name: "ModuleA",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.product(name: "SwiftProtobuf", package: "swift-protobuf")
]
),
.target(
Expand Down
46 changes: 23 additions & 23 deletions CompileTests/MultiModule/Tests/Test1/test1.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import ImportsAPublicly
// Don't need to import ModuleA because of the file being a `import public`

import XCTest

final class ExampleTests: XCTestCase {
func testA() {
let anA = A.with { $0.e = .a }
XCTAssertEqual(anA.e, .a)
}
// Don't need to import ModuleA because of the file being a `import public`

func testImportsAPublicly() {
let imports = ImportsAPublicly.with { $0.a.e = .a }
XCTAssertEqual(imports.a.e, .a)
}
final class ExampleTests: XCTestCase {
func testA() {
let anA = A.with { $0.e = .a }
XCTAssertEqual(anA.e, .a)
}

func testInterop() {
let anA = A.with { $0.e = .b }
let imports = ImportsAPublicly.with {
$0.a = anA
$0.e = .b
func testImportsAPublicly() {
let imports = ImportsAPublicly.with { $0.a.e = .a }
XCTAssertEqual(imports.a.e, .a)
}
XCTAssertEqual(imports.a.e, imports.e)
let transitively = UsesATransitively.with {
$0.a = anA
$0.e = imports.e

func testInterop() {
let anA = A.with { $0.e = .b }
let imports = ImportsAPublicly.with {
$0.a = anA
$0.e = .b
}
XCTAssertEqual(imports.a.e, imports.e)
let transitively = UsesATransitively.with {
$0.a = anA
$0.e = imports.e
}
XCTAssertEqual(transitively.a, anA)
XCTAssertEqual(transitively.e, imports.e)
}
XCTAssertEqual(transitively.a, anA)
XCTAssertEqual(transitively.e, imports.e)
}
}
46 changes: 23 additions & 23 deletions CompileTests/MultiModule/Tests/Test2/test2.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import ImportsImportsAPublicly
// Don't need to import ModuleA because of the file being a `import public`

import XCTest

final class ExampleTests: XCTestCase {
func testA() {
let anA = A.with { $0.e = .a }
XCTAssertEqual(anA.e, .a)
}
// Don't need to import ModuleA because of the file being a `import public`

func testImportsImportsAPublicly() {
let imports = ImportsImportsAPublicly.with { $0.a.e = .a }
XCTAssertEqual(imports.a.e, .a)
}
final class ExampleTests: XCTestCase {
func testA() {
let anA = A.with { $0.e = .a }
XCTAssertEqual(anA.e, .a)
}

func testInterop() {
let anA = A.with { $0.e = .b }
let imports = ImportsImportsAPublicly.with {
$0.a = anA
$0.e = .b
func testImportsImportsAPublicly() {
let imports = ImportsImportsAPublicly.with { $0.a.e = .a }
XCTAssertEqual(imports.a.e, .a)
}
XCTAssertEqual(imports.a.e, imports.e)
let transitively = UsesATransitively2.with {
$0.a = anA
$0.e = imports.e

func testInterop() {
let anA = A.with { $0.e = .b }
let imports = ImportsImportsAPublicly.with {
$0.a = anA
$0.e = .b
}
XCTAssertEqual(imports.a.e, imports.e)
let transitively = UsesATransitively2.with {
$0.a = anA
$0.e = imports.e
}
XCTAssertEqual(transitively.a, anA)
XCTAssertEqual(transitively.e, imports.e)
}
XCTAssertEqual(transitively.a, anA)
XCTAssertEqual(transitively.e, imports.e)
}
}
25 changes: 16 additions & 9 deletions FuzzTesting/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,39 @@ import PackageDescription
let package = Package(
name: "FuzzTesting",
platforms: [
.macOS(.v10_15),
.macOS(.v10_15)
],
dependencies: [
.package(name: "SwiftProtobuf", path: ".."),
.package(name: "SwiftProtobuf", path: "..")
],
targets: [
.target(
name: "FuzzCommon",
dependencies: ["SwiftProtobuf"]),
dependencies: ["SwiftProtobuf"]
),
.target(
name: "FuzzBinary",
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
dependencies: ["SwiftProtobuf", "FuzzCommon"]
),
.target(
name: "FuzzBinaryDelimited",
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
dependencies: ["SwiftProtobuf", "FuzzCommon"]
),
.target(
name: "FuzzAsyncMessageSequence",
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
dependencies: ["SwiftProtobuf", "FuzzCommon"]
),
.target(
name: "FuzzJSON",
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
dependencies: ["SwiftProtobuf", "FuzzCommon"]
),
.target(
name: "FuzzTextFormat",
dependencies: ["SwiftProtobuf", "FuzzCommon"]),
dependencies: ["SwiftProtobuf", "FuzzCommon"]
),
.testTarget(
name: "FuzzCommonTests",
dependencies: ["FuzzCommon"]),
dependencies: ["FuzzCommon"]
),
]
)
67 changes: 33 additions & 34 deletions FuzzTesting/Sources/FuzzAsyncMessageSequence/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,48 @@
// -----------------------------------------------------------------------------

import Foundation

import FuzzCommon

import SwiftProtobuf

fileprivate func asyncByteStream(bytes: UnsafeRawBufferPointer) -> AsyncStream<UInt8> {
AsyncStream(UInt8.self) { continuation in
for i in 0..<bytes.count {
continuation.yield(bytes.loadUnaligned(fromByteOffset: i, as: UInt8.self))
private func asyncByteStream(bytes: UnsafeRawBufferPointer) -> AsyncStream<UInt8> {
AsyncStream(UInt8.self) { continuation in
for i in 0..<bytes.count {
continuation.yield(bytes.loadUnaligned(fromByteOffset: i, as: UInt8.self))
}
continuation.finish()
}
continuation.finish()
}
}

@_cdecl("LLVMFuzzerTestOneInput")
public func FuzzAsyncMessageSequence(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
// No decoding options here, a leading zero is actually valid (zero length message),
// so we rely on the other Binary fuzz tester to test options, and just let this
// one focus on issue around framing of the messages on the stream.
let bytes = UnsafeRawBufferPointer(start: start, count: count)
let asyncBytes = asyncByteStream(bytes: bytes)
let decoding = asyncBytes.binaryProtobufDelimitedMessages(
of: SwiftProtoTesting_Fuzz_Message.self,
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions)
// No decoding options here, a leading zero is actually valid (zero length message),
// so we rely on the other Binary fuzz tester to test options, and just let this
// one focus on issue around framing of the messages on the stream.
let bytes = UnsafeRawBufferPointer(start: start, count: count)
let asyncBytes = asyncByteStream(bytes: bytes)
let decoding = asyncBytes.binaryProtobufDelimitedMessages(
of: SwiftProtoTesting_Fuzz_Message.self,
extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions
)

let semaphore = DispatchSemaphore(value: 0)
Task {
do {
for try await _ in decoding {
// TODO: Test serialization for completeness.
// We could serialize individual messages like this:
// let _: [UInt8] = try! msg.serializedBytes()
// but we don't have a stream writer which is what
// we really want to exercise here.
let semaphore = DispatchSemaphore(value: 0)
Task {
do {
for try await _ in decoding {
// TODO: Test serialization for completeness.
// We could serialize individual messages like this:
// let _: [UInt8] = try! msg.serializedBytes()
// but we don't have a stream writer which is what
// we really want to exercise here.

// Also, serialization here more than doubles the total
// run time, leading to timeouts for the fuzz tester. :(
}
} catch {
// Error parsing are to be expected since not all input will be well formed.
// Also, serialization here more than doubles the total
// run time, leading to timeouts for the fuzz tester. :(
}
} catch {
// Error parsing are to be expected since not all input will be well formed.
}
semaphore.signal()
}
semaphore.signal()
}
semaphore.wait()
return 0
semaphore.wait()
return 0
}
Loading