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

Update CI and Linter #50

Merged
merged 2 commits into from
Feb 22, 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
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ on:
- main

env:
DEVELOPER_DIR: /Applications/Xcode_13.3.1.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_15.2.app/Contents/Developer
HOMEBREW_NO_INSTALL_CLEANUP: TRUE

jobs:
lint:
runs-on: macos-12
runs-on: macos-14
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install Dependencies
run: brew bundle
- name: "Run Linting"
run: make lint

test:
runs-on: macos-12
runs-on: macos-14
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install Dependencies
run: brew bundle
- name: Run Tests
Expand All @@ -39,11 +39,13 @@ jobs:
f="$(basename $XCTEST_PATH .xctest)"
COV_BIN="${COV_BIN}/Contents/MacOS/$f"
fi
/usr/local/opt/llvm/bin/llvm-cov export -format="lcov" \
xcrun llvm-cov export -format="lcov" \
"${COV_BIN}" \
-instr-profile=.build/debug/codecov/default.profdata \
-ignore-filename-regex=".build|Tests"\ > info.lcov
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v4
name: Upload Code Coverage
with:
file: ./info.lcov
token: ${{ secrets.CODECOV_TOKEN }}

2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- main

env:
DEVELOPER_DIR: /Applications/Xcode_12.4.app/Contents/Developer
DEVELOPER_DIR: /Applications/Xcode_15.2.app/Contents/Developer
HOMEBREW_NO_INSTALL_CLEANUP: TRUE

jobs:
Expand Down
2 changes: 1 addition & 1 deletion .swiftformat
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
--header strip
--disable redundantRawValues
--disable trailingclosures

--disable redundantInternal
4 changes: 2 additions & 2 deletions Extensions/Mocks/HAConnection+Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public class HAMockConnection: HAConnection {
switch dataResult {
case let .success(data):
do {
completion(.success(try T(data: data)))
try completion(.success(T(data: data)))
} catch {
completion(.failure(.underlying(error as NSError)))
}
Expand Down Expand Up @@ -198,7 +198,7 @@ public class HAMockConnection: HAConnection {
) -> HACancellable {
subscribe(to: request.request, initiated: initiated, handler: { cancellable, data in
do {
handler(cancellable, try T(data: data))
try handler(cancellable, T(data: data))
} catch {}
})
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Caches/HACachePopulateInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct HACachePopulateInfo<OutgoingType> {
}, start: { connection, perform in
connection.send(retryRequest, completion: { result in
perform { current in
transform(.init(incoming: try result.get(), current: current))
try transform(.init(incoming: result.get(), current: current))
}
})
}
Expand Down
16 changes: 8 additions & 8 deletions Source/Convenience/CurrentUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public struct HAResponseCurrentUser: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
type: try data.decode("auth_provider_type"),
try self.init(
type: data.decode("auth_provider_type"),
id: data.decode("auth_provider_id", fallback: nil)
)
}
Expand Down Expand Up @@ -65,10 +65,10 @@ public struct HAResponseCurrentUser: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
id: try data.decode("id"),
name: try data.decode("name"),
isEnabled: try data.decode("enabled")
try self.init(
id: data.decode("id"),
name: data.decode("name"),
isEnabled: data.decode("enabled")
)
}

Expand All @@ -88,8 +88,8 @@ public struct HAResponseCurrentUser: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
id: try data.decode("id"),
try self.init(
id: data.decode("id"),
name: data.decode("name", fallback: nil),
isOwner: data.decode("is_owner", fallback: false),
isAdmin: data.decode("is_admin", fallback: false),
Expand Down
14 changes: 7 additions & 7 deletions Source/Convenience/Event.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public struct HAResponseEvent: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
id: try data.decode("id"),
try self.init(
id: data.decode("id"),
userId: data.decode("user_id", fallback: nil),
parentId: data.decode("parent_id", fallback: nil)
)
Expand All @@ -152,12 +152,12 @@ public struct HAResponseEvent: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
type: .init(rawValue: try data.decode("event_type")),
timeFired: try data.decode("time_fired"),
try self.init(
type: .init(rawValue: data.decode("event_type")),
timeFired: data.decode("time_fired"),
data: data.decode("data", fallback: [:]),
origin: try data.decode("origin"),
context: try data.decode("context")
origin: data.decode("origin"),
context: data.decode("context")
)
}

Expand Down
6 changes: 3 additions & 3 deletions Source/Convenience/RenderTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public struct HAResponseRenderTemplate: HADataDecodable {
/// - Parameter data: The data from the server
/// - Throws: If any required keys are missing
public init(data: HAData) throws {
self.init(
result: try data.decode("result"),
listeners: try data.decode("listeners")
try self.init(
result: data.decode("result"),
listeners: data.decode("listeners")
)
}

Expand Down
8 changes: 4 additions & 4 deletions Source/Convenience/Services.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public struct HAServiceDefinition {
/// - data: The data for the definition
/// - Throws: If any required keys are missing in the data
public init(domain: HAServicesDomain, service: HAServicesService, data: HAData) throws {
self.init(
try self.init(
domain: domain,
service: service,
name: try data.decode("name", fallback: data.decode("description")),
description: try data.decode("description"),
fields: try data.decode("fields")
name: data.decode("name", fallback: data.decode("description")),
description: data.decode("description"),
fields: data.decode("fields")
)
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Convenience/States.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public struct HAResponseEventStateChanged: HADataDecodable {
let event = try HAResponseEvent(data: data)
let eventData = HAData.dictionary(event.data)

self.init(
try self.init(
event: event,
entityId: try eventData.decode("entity_id"),
entityId: eventData.decode("entity_id"),
oldState: try? eventData.decode("old_state"),
newState: try? eventData.decode("new_state")
)
Expand Down
2 changes: 1 addition & 1 deletion Source/Data/HADataDecodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Array: HADataDecodable where Element: HADataDecodable {
throw HADataError.couldntTransform(key: "root")
}

self.init(try array.map { try Element(data: $0) })
try self.init(array.map { try Element(data: $0) })
}
}

Expand Down
22 changes: 11 additions & 11 deletions Source/Data/HAEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public struct HAEntity: HADataDecodable, Hashable {

try self.init(
entityId: entityId,
domain: try {
domain: {
guard let dot = entityId.firstIndex(of: ".") else {
throw HADataError.couldntTransform(key: "entity_id")
}

return String(entityId[..<dot])
}(),
state: try data.decode("state"),
lastChanged: try data.decode("last_changed"),
lastUpdated: try data.decode("last_updated"),
attributes: try data.decode("attributes"),
context: try data.decode("context")
state: data.decode("state"),
lastChanged: data.decode("last_changed"),
lastUpdated: data.decode("last_updated"),
attributes: data.decode("attributes"),
context: data.decode("context")
)
}

Expand Down Expand Up @@ -129,11 +129,11 @@ public struct HAEntityAttributesZone: HADataDecodable {
/// - Parameter data: The data to create from
/// - Throws: When the data is missing any required fields
public init(data: HAData) throws {
self.init(
latitude: try data.decode("latitude"),
longitude: try data.decode("longitude"),
radius: try data.decode("radius", transform: { Measurement<UnitLength>(value: $0, unit: .meters) }),
isPassive: try data.decode("passive")
try self.init(
latitude: data.decode("latitude"),
longitude: data.decode("longitude"),
radius: data.decode("radius", transform: { Measurement<UnitLength>(value: $0, unit: .meters) }),
isPassive: data.decode("passive")
)
}

Expand Down
4 changes: 1 addition & 3 deletions Source/HAConnectionInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ public struct HAConnectionInfo: Equatable {
/// - Parameter url: The raw URL
/// - Returns: A URL with common issues removed
private static func sanitize(_ url: URL) -> URL {
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
return url
}
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!

for substring in [
"/api/websocket",
Expand Down
2 changes: 1 addition & 1 deletion Tests/HACachesContainer.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class HACachesContainerTests: XCTestCase {

func testCreate() throws {
let cache1 = container[Key1.self]
XCTAssertEqual(ObjectIdentifier(try XCTUnwrap(cache1.connection)), ObjectIdentifier(connection))
XCTAssertEqual(try ObjectIdentifier(XCTUnwrap(cache1.connection)), ObjectIdentifier(connection))

XCTAssertEqual(cache1.populateInfo?.request.type, "key1_pop")
XCTAssertEqual(cache1.subscribeInfo?.count, 1)
Expand Down
18 changes: 9 additions & 9 deletions Tests/HAConnectionImpl.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ internal class HAConnectionImplTests: XCTestCase {
} else {
XCTAssertEqual(
String(data: sentRequest.httpBodyStreamAsData, encoding: .utf8),
String(
data: try JSONSerialization
try String(
data: JSONSerialization
.data(withJSONObject: request.data, options: [.sortedKeys, .fragmentsAllowed]),
encoding: .utf8
)
Expand Down Expand Up @@ -168,7 +168,7 @@ internal class HAConnectionImplTests: XCTestCase {

func testConnectionConnect() throws {
connection.connect()
let expectedURL = try HAConnectionInfo(url: try XCTUnwrap(url)).webSocketURL
let expectedURL = try HAConnectionInfo(url: XCTUnwrap(url)).webSocketURL
XCTAssertTrue(engine.events.contains(where: { event in
if case let .start(request) = event {
return request.url == expectedURL
Expand Down Expand Up @@ -204,7 +204,7 @@ internal class HAConnectionImplTests: XCTestCase {
let oldEngine = try XCTUnwrap(engine)
engine = FakeEngine()
url = try XCTUnwrap(url).appendingPathComponent("hi")
let newExpectedURL = try HAConnectionInfo(url: try XCTUnwrap(url)).webSocketURL
let newExpectedURL = try HAConnectionInfo(url: XCTUnwrap(url)).webSocketURL

connection.connect()
XCTAssertTrue(oldEngine.events.contains(.stop(CloseCode.goingAway.rawValue)))
Expand Down Expand Up @@ -636,8 +636,8 @@ internal class HAConnectionImplTests: XCTestCase {

let requestURL = try XCTUnwrap(URL(string: XCTUnwrap(url).absoluteString + "/api/some_path?key=value"))

let expectedResult = Swift.Result<(HTTPURLResponse, Data?), Error>.success((
try XCTUnwrap(HTTPURLResponse(url: requestURL, statusCode: 204, httpVersion: nil, headerFields: nil)),
let expectedResult = try Swift.Result<(HTTPURLResponse, Data?), Error>.success((
XCTUnwrap(HTTPURLResponse(url: requestURL, statusCode: 204, httpVersion: nil, headerFields: nil)),
"response data".data(using: .utf8)
))
StubbingURLProtocol.register(requestURL, result: expectedResult)
Expand All @@ -664,8 +664,8 @@ internal class HAConnectionImplTests: XCTestCase {

let requestURL = try XCTUnwrap(URL(string: XCTUnwrap(url).absoluteString + "/api/some_path?key=value"))

let expectedResult = Swift.Result<(HTTPURLResponse, Data?), Error>.success((
try XCTUnwrap(HTTPURLResponse(url: requestURL, statusCode: 204, httpVersion: nil, headerFields: nil)),
let expectedResult = try Swift.Result<(HTTPURLResponse, Data?), Error>.success((
XCTUnwrap(HTTPURLResponse(url: requestURL, statusCode: 204, httpVersion: nil, headerFields: nil)),
"response data".data(using: .utf8)
))
StubbingURLProtocol.register(requestURL, result: expectedResult)
Expand Down Expand Up @@ -827,7 +827,7 @@ internal class HAConnectionImplTests: XCTestCase {
.viabilityChanged(true),
] {
responseController.received.removeAll()
connection.didReceive(event: event, client: try XCTUnwrap(connection.connection))
try connection.didReceive(event: event, client: XCTUnwrap(connection.connection))
XCTAssertEqual(try XCTUnwrap(responseController.received.last), event)
}
}
Expand Down
16 changes: 4 additions & 12 deletions Tests/HAConnectionInfo.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ internal class HAConnectionInfoTests: XCTestCase {
XCTAssertEqual(connectionInfo.url, url)

// not easy to test WebSocket, so we test our wrapper for it
let pinning = HAStarscreamCertificatePinningImpl(
evaluateCertificate: try XCTUnwrap(connectionInfo.evaluateCertificate)
let pinning = try HAStarscreamCertificatePinningImpl(
evaluateCertificate: XCTUnwrap(connectionInfo.evaluateCertificate)
)

var secTrust: SecTrust?
SecTrustCreateWithCertificates([
try XCTUnwrap(SecCertificateCreateWithData(nil, XCTUnwrap(Data(base64Encoded: """
try SecTrustCreateWithCertificates([
XCTUnwrap(SecCertificateCreateWithData(nil, XCTUnwrap(Data(base64Encoded: """
MIIFljCCA36gAwIBAgINAgO8U1lrNMcY9QFQZjANBgkqhkiG9w0BAQsFADBHMQswCQYDVQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRy
dXN0IFNlcnZpY2VzIExMQzEUMBIGA1UEAxMLR1RTIFJvb3QgUjEwHhcNMjAwODEzMDAwMDQyWhcNMjcwOTMwMDAwMDQyWjBGMQswCQYD
VQQGEwJVUzEiMCAGA1UEChMZR29vZ2xlIFRydXN0IFNlcnZpY2VzIExMQzETMBEGA1UEAxMKR1RTIENBIDFDMzCCASIwDQYJKoZIhvcN
Expand Down Expand Up @@ -222,12 +222,4 @@ internal class HAConnectionInfoTests: XCTestCase {
XCTAssertEqual(connectionInfo.url, expected)
}
}

func testInvalidURLComponentsURL() throws {
// example of valid URL invalid URLComponents - https://stackoverflow.com/questions/55609012
let url = try XCTUnwrap(URL(string: "a://@@/api/websocket"))
let connectionInfo = try HAConnectionInfo(url: url)
XCTAssertEqual(connectionInfo.url, url)
XCTAssertEqual(connectionInfo.webSocket().request.url, url.appendingPathComponent("api/websocket"))
}
}
12 changes: 6 additions & 6 deletions Tests/HAData.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ internal class HADataTests: XCTestCase {
let value = HAData(value: ["some_key": "2021-02-20T05:14:52+00:00"])
let date: Date = try XCTUnwrap(value.decode("some_key") as Date?)

let components = Calendar.current.dateComponents(
in: try XCTUnwrap(TimeZone(identifier: "GMT+0600")),
let components = try Calendar.current.dateComponents(
in: XCTUnwrap(TimeZone(identifier: "GMT+0600")),
from: date
)
XCTAssertEqual(components.year, 2021)
Expand All @@ -232,8 +232,8 @@ internal class HADataTests: XCTestCase {
let value = HAData(value: ["some_key": "2021-02-20T05:14:52.647932+00:00"])
let date: Date = try XCTUnwrap(value.decode("some_key") as Date?)

let components = Calendar.current.dateComponents(
in: try XCTUnwrap(TimeZone(identifier: "GMT+0600")),
let components = try Calendar.current.dateComponents(
in: XCTUnwrap(TimeZone(identifier: "GMT+0600")),
from: date
)
XCTAssertEqual(components.year, 2021)
Expand All @@ -249,8 +249,8 @@ internal class HADataTests: XCTestCase {
let value = HAData(value: ["some_key": "2021-02-20T05:14:52.647932+00:00"])
let date: Date = try value.decode("some_key")

let components = Calendar.current.dateComponents(
in: try XCTUnwrap(TimeZone(identifier: "GMT+0600")),
let components = try Calendar.current.dateComponents(
in: XCTUnwrap(TimeZone(identifier: "GMT+0600")),
from: date
)
XCTAssertEqual(components.year, 2021)
Expand Down
Loading