Skip to content

Commit

Permalink
Merge branch 'main' into dfed--codecov-badge
Browse files Browse the repository at this point in the history
  • Loading branch information
dfed committed Oct 12, 2022
2 parents a10de39 + 16e6563 commit ea12b06
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 54 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: macOS-11
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand All @@ -24,7 +24,7 @@ jobs:
runs-on: macOS-11
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand All @@ -45,7 +45,7 @@ jobs:
fail-fast: false
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand All @@ -54,9 +54,6 @@ jobs:
run: Scripts/github/prepare-simulators.sh ${{ matrix.platforms }}
- name: Build and Test Framework
run: Scripts/build.swift ${{ matrix.platforms }}
- name: Upload Coverage Reports
if: success()
run: Scripts/upload-coverage-reports.sh ${{ matrix.platforms }}
spm-12:
name: Build Xcode 12
runs-on: macOS-11
Expand All @@ -69,7 +66,7 @@ jobs:
fail-fast: false
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand All @@ -78,9 +75,6 @@ jobs:
run: Scripts/github/prepare-simulators.sh ${{ matrix.platforms }}
- name: Build and Test Framework
run: Scripts/build.swift ${{ matrix.platforms }}
- name: Upload Coverage Reports
if: success()
run: Scripts/upload-coverage-reports.sh ${{ matrix.platforms }}
spm-13:
name: Build Xcode 13
runs-on: macOS-12
Expand All @@ -93,7 +87,7 @@ jobs:
fail-fast: false
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand All @@ -102,15 +96,17 @@ jobs:
run: Scripts/github/prepare-simulators.sh ${{ matrix.platforms }}
- name: Build and Test Framework
run: Scripts/build.swift ${{ matrix.platforms }}
- name: Prepare Coverage Reports
run: ./Scripts/prepare-coverage-reports.sh
- name: Upload Coverage Reports
if: success()
run: Scripts/upload-coverage-reports.sh ${{ matrix.platforms }}
uses: codecov/codecov-action@v3
spm-13-swift:
name: Swift Build Xcode 13
runs-on: macOS-12
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Bundle Install
run: bundle install
- name: Select Xcode Version
Expand Down
35 changes: 35 additions & 0 deletions Scripts/prepare-coverage-reports.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/zsh -l
set -e

function exportlcov() {
build_type=$1
executable_name=$2

executable=$(find "${directory}" -type f -name $executable_name)
profile=$(find "${directory}" -type f -name 'Coverage.profdata')
output_file_name="$executable_name.lcov"

can_proceed=true
if [[ $build_type == watchOS* ]]; then
echo "\tAborting creation of $output_file_name – watchOS not supported."
elif [[ -z $profile ]]; then
echo "\tAborting creation of $output_file_name – no profile found."
elif [[ -z $executable ]]; then
echo "\tAborting creation of $output_file_name – no executable found."
else
output_dir=".build/artifacts/$build_type"
mkdir -p $output_dir

output_file="$output_dir/$output_file_name"
echo "\tExporting $output_file"
xcrun llvm-cov export -format="lcov" $executable -instr-profile $profile > $output_file
fi
}

for directory in $(git rev-parse --show-toplevel)/.build/derivedData/*/; do
build_type=$(basename $directory)
echo "Finding coverage information for $build_type"

exportlcov $build_type 'CacheAdvanceTests'
exportlcov $build_type 'CADCacheAdvanceTests'
done
9 changes: 0 additions & 9 deletions Scripts/upload-coverage-reports.sh

This file was deleted.

23 changes: 5 additions & 18 deletions Sources/CADCacheAdvance/CADCacheAdvance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,17 @@ public final class __ObjectiveCCompatibleCacheAdvanceWithGenericData: NSObject {
/// A decoder that treats all messages as if they are `Data`.
final class PassthroughDataDecoder: MessageDecoder {
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
if let data = data as? T {
return data
} else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: [],
debugDescription: "Type was not Data"))
}
// Force cast because this type is only used with a CacheAdvance<Data> type.
return data as! T
}
}

// MARK: - PassthroughDataDecoder
// MARK: - PassthroughDataEncoder

/// A encoder that treats all messages as if they are `Data`.
final class PassthroughDataEncoder: MessageEncoder {
func encode<T>(_ value: T) throws -> Data where T : Encodable {
if let value = value as? Data {
return value
} else {
throw EncodingError.invalidValue(
value,
EncodingError.Context(
codingPath: [],
debugDescription: "Value was not Data"))
}
// Force cast because this type is only used with a CacheAdvance<Data> type.
return value as! Data
}
}
3 changes: 3 additions & 0 deletions Sources/CacheAdvance/BigEndianHostSwappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ protocol BigEndianHostSwappable where Self: FixedWidthInteger {
/// Converts the big-endian value in x to the current endian format and returns the resulting value.
static func swapToHost(_ x: Self) -> Self

/// The maximum representable integer in this type.
static var max: Self { get }

}

extension BigEndianHostSwappable {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CacheAdvance/CacheAdvance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public final class CacheAdvance<T: Codable> {
throw CacheAdvanceError.fileNotWritable
}

let encodableMessage = EncodableMessage(message: message, encoder: encoder)
let encodableMessage = EncodableMessage<T, MessageSpan>(message: message, encoder: encoder)
let messageData = try encodableMessage.encodedData()
let bytesNeededToStoreMessage = Bytes(messageData.count)

Expand Down
4 changes: 2 additions & 2 deletions Sources/CacheAdvance/EncodableMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Foundation
/// `[messageSize][data]`
/// - `messageSize` is a big-endian encoded `MessageSpan` of length `messageSpanStorageLength`.
/// - `data` is length `messageSize`.
struct EncodableMessage<T: Codable> {
struct EncodableMessage<T: Codable, Size: BigEndianHostSwappable> {

// MARK: Initialization

Expand All @@ -40,7 +40,7 @@ struct EncodableMessage<T: Codable> {
/// The encoded message, prefixed with the size of the message blob.
func encodedData() throws -> Data {
let messageData = try encoder.encode(message)
guard messageData.count < MessageSpan.max else {
guard messageData.count < Size.max else {
// We can't encode the length this message in a MessageSpan.
throw CacheAdvanceError.messageLargerThanCacheCapacity
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftTryCatch/SwiftTryCatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ NS_ASSUME_NONNULL_BEGIN
Provides try catch functionality for swift by wrapping around Objective-C
*/

+ (void)try:(__attribute__((noescape)) void(^ _Nonnull)(void))try catch:(__attribute__((noescape)) void(^ _Nonnull)(NSException *exception))catch finally:(__attribute__((noescape)) void(^ _Nullable)(void))finally;
+ (void)try:(__attribute__((noescape)) void(^ _Nonnull)(void))try catch:(__attribute__((noescape)) void(^ _Nonnull)(NSException *exception))catch;
@end

NS_ASSUME_NONNULL_END
7 changes: 1 addition & 6 deletions Sources/SwiftTryCatch/SwiftTryCatch.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,14 @@ @implementation SwiftTryCatch
/**
Provides try catch functionality for swift by wrapping around Objective-C
*/
+ (void)try:(__attribute__((noescape)) void(^ _Nonnull)(void))try catch:(__attribute__((noescape)) void(^ _Nonnull)(NSException *exception))catch finally:(__attribute__((noescape)) void(^ _Nullable)(void))finally;
+ (void)try:(__attribute__((noescape)) void(^ _Nonnull)(void))try catch:(__attribute__((noescape)) void(^ _Nonnull)(NSException *exception))catch;
{
@try {
try();
}
@catch (NSException *exception) {
catch(exception);
}
@finally {
if (finally != NULL) {
finally();
}
}
}

@end
2 changes: 1 addition & 1 deletion Tests/CacheAdvanceTests/CacheAdvanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ final class CacheAdvanceTests: XCTestCase {
let encoder = JSONEncoder()
return try FileHeader.expectedEndOfHeaderInFile
+ messages.reduce(0) { allocatedSize, message in
let encodableMessage = EncodableMessage(message: message, encoder: encoder)
let encodableMessage = EncodableMessage<T, MessageSpan>(message: message, encoder: encoder)
let data = try encodableMessage.encodedData()
return allocatedSize + UInt64(data.count)
}
Expand Down
11 changes: 8 additions & 3 deletions Tests/CacheAdvanceTests/EncodableMessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class EncodableMessageTests: XCTestCase {
func test_encodedData_encodesCorrectSize() throws {
let message = TestableMessage("This is a test")
let data = try encoder.encode(message)
let encodedMessage = EncodableMessage<TestableMessage>(message: message, encoder: encoder)
let encodedMessage = EncodableMessage<TestableMessage, MessageSpan>(message: message, encoder: encoder)
let encodedData = try encodedMessage.encodedData()

let prefix = encodedData.subdata(in: 0..<MessageSpan.storageLength)
Expand All @@ -37,19 +37,24 @@ final class EncodableMessageTests: XCTestCase {
func test_encodedData_isOfCorrectLength() throws {
let message = TestableMessage("This is a test")
let data = try encoder.encode(message)
let encodedMessage = EncodableMessage<TestableMessage>(message: message, encoder: encoder)
let encodedMessage = EncodableMessage<TestableMessage, MessageSpan>(message: message, encoder: encoder)
let encodedData = try encodedMessage.encodedData()
XCTAssertEqual(encodedData.count, data.count + MessageSpan.storageLength)
}

func test_encodedData_hasDataPostfix() throws {
let message = TestableMessage("This is a test")
let data = try encoder.encode(message)
let encodedMessage = EncodableMessage<TestableMessage>(message: message, encoder: encoder)
let encodedMessage = EncodableMessage<TestableMessage, MessageSpan>(message: message, encoder: encoder)
let encodedData = try encodedMessage.encodedData()
XCTAssertEqual(encodedData.advanced(by: MessageSpan.storageLength), data)
}

func test_encodedData_whenMessageDataTooLarge_throwsError() throws {
let encodedMessage = EncodableMessage<Data, UInt8>(message: Data(count: Int(UInt8.max)), encoder: encoder)
XCTAssertThrowsError(try encodedMessage.encodedData())
}

// MARK: Private

private let encoder = JSONEncoder()
Expand Down
11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@ comment:
layout: "reach,diff,flags,tree"
behavior: default
require_changes: no

coverage:
status:
project:
default:
threshold: 0.25%
patch: off

ignore:
- "Sources/LorumIpsum"
- "Tests"

0 comments on commit ea12b06

Please sign in to comment.