Skip to content

Commit

Permalink
Merge pull request #3 from NeedleInAJayStack/fix/tests
Browse files Browse the repository at this point in the history
Fixes tests & impoves platform isolation
  • Loading branch information
NeedleInAJayStack authored Jun 10, 2024
2 parents bd945ee + 36c6fc3 commit 1302dbd
Show file tree
Hide file tree
Showing 29 changed files with 146 additions and 72 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push: { branches: [ main ] }

jobs:
darwin-test:
macos-test:
runs-on: macos-latest
steps:
- uses: maxim-lobanov/setup-xcode@v1
Expand All @@ -13,11 +13,11 @@ jobs:
- uses: actions/checkout@v2
- name: Darwin build & test
run: swift test --skip IntegrationTests
linux-build:
linux-test:
runs-on: ubuntu-latest
container:
image: swift:latest
steps:
- uses: actions/checkout@v2
- name: Linux build
run: swift build --target HaystackClientNIO
- name: Linux build & test
run: swift test --skip IntegrationTests
58 changes: 58 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import PackageDescription

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
let package = Package(
name: "Haystack",
platforms: [
Expand Down Expand Up @@ -81,3 +82,60 @@ let package = Package(
),
]
)
#else
let package = Package(
name: "Haystack",
products: [
.library(
name: "Haystack",
targets: ["Haystack"]
),
.library(
name: "HaystackClientNIO",
targets: [
"HaystackClient",
"HaystackClientNIO"
]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0"),
.package(url: "https://github.com/swift-server/async-http-client.git", from: "1.9.0")
],
targets: [
.target(
name: "Haystack",
dependencies: []
),
.target(
name: "HaystackClient",
dependencies: [
"Haystack",
.product(name: "Crypto", package: "swift-crypto"),
]
),
.target(
name: "HaystackClientNIO",
dependencies: [
"Haystack",
"HaystackClient",
.product(name: "AsyncHTTPClient", package: "async-http-client"),
]
),

// Tests
.testTarget(
name: "HaystackTests",
dependencies: ["Haystack"]
),
.testTarget(
name: "HaystackClientTests",
dependencies: ["HaystackClient"]
),
.testTarget(
name: "HaystackClientNIOIntegrationTests",
dependencies: ["HaystackClientNIO"]
),
]
)
#endif
4 changes: 2 additions & 2 deletions Sources/Haystack/Coord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension Coord: Codable {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#coord)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -68,7 +68,7 @@ extension Coord: Codable {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#coord)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(latitude, forKey: .lat)
try container.encode(longitude, forKey: .lng)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Date {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#date)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -110,7 +110,7 @@ extension Date {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#date)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(isoString, forKey: .val)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/DateTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ extension DateTime {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#dateTime)
public init(from decoder: Decoder) throws {
guard let container = try? decoder.container(keyedBy: Self.CodingKeys) else {
guard let container = try? decoder.container(keyedBy: Self.CodingKeys.self) else {
throw DecodingError.typeMismatch(
Self.self,
.init(
Expand Down Expand Up @@ -248,7 +248,7 @@ extension DateTime {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#dateTime)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
let isoString: String
if hasMilliseconds {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Grid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ extension Grid {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#grid)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -110,7 +110,7 @@ extension Grid {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#grid)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(meta, forKey: .meta)
if cols.isEmpty {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Marker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Marker {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#marker)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand All @@ -58,7 +58,7 @@ extension Marker {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#marker)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
}
}
4 changes: 2 additions & 2 deletions Sources/Haystack/NA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension NA {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#na)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand All @@ -58,7 +58,7 @@ extension NA {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#na)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
}
}
4 changes: 2 additions & 2 deletions Sources/Haystack/Number.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ extension Number {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#number)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -129,7 +129,7 @@ extension Number {
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#number)
public func encode(to encoder: Encoder) throws {
if unit != nil || val.isNaN || val.isInfinite {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)

if val.isNaN {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Ref.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension Ref {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#ref)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -74,7 +74,7 @@ extension Ref {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#ref)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(val, forKey: .val)
try container.encode(dis, forKey: .dis)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Remove.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension Remove {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#remove)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand All @@ -52,7 +52,7 @@ extension Remove {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#remove)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
}
}
4 changes: 2 additions & 2 deletions Sources/Haystack/Symbol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ extension Symbol {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#symbol)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand All @@ -63,7 +63,7 @@ extension Symbol {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#symbol)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(val, forKey: .val)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Time.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ extension Time {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#date)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -137,7 +137,7 @@ extension Time {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#time)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(isoString, forKey: .val)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/Uri.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Uri {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#uri)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand All @@ -58,7 +58,7 @@ extension Uri {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#uri)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(val, forKey: .val)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Haystack/XStr.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension XStr {
/// Read from decodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#xstr)
public init(from decoder: Decoder) throws {
if let container = try? decoder.container(keyedBy: Self.CodingKeys) {
if let container = try? decoder.container(keyedBy: Self.CodingKeys.self) {
guard try container.decode(String.self, forKey: ._kind) == Self.kindValue else {
throw DecodingError.typeMismatch(
Self.self,
Expand Down Expand Up @@ -68,7 +68,7 @@ extension XStr {
/// Write to encodable data
/// See [JSON format](https://project-haystack.org/doc/docHaystack/Json#xstr)
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Self.CodingKeys)
var container = encoder.container(keyedBy: Self.CodingKeys.self)
try container.encode(Self.kindValue, forKey: ._kind)
try container.encode(type, forKey: .type)
try container.encode(val, forKey: .val)
Expand Down
4 changes: 2 additions & 2 deletions Sources/HaystackClientNIO/Client+HTTPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public extension Client {
username: String,
password: String,
format: DataFormat = .zinc,
eventLoopGroup: EventLoopGroup
httpClient: HTTPClient
) throws {
let fetcher = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup)).haystackFetcher()
let fetcher = httpClient.haystackFetcher()
try self.init(
baseUrl: baseUrl,
username: username,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import XCTest
import AsyncHTTPClient
import Haystack
import HaystackClientNIO
import NIO
import XCTest

/// To use these tests, run a [Haxall](https://github.com/haxall/haxall) server and set the username and password
/// in the `HAYSTACK_USER` and `HAYSTACK_PASSWORD` environment variables
final class HaystackClientNIOIntegrationTests: XCTestCase {
var client: Client = try! Client(
baseUrl: "http://localhost:8080/api/",
username: ProcessInfo.processInfo.environment["HAYSTACK_USER"] ?? "su",
password: ProcessInfo.processInfo.environment["HAYSTACK_PASSWORD"] ?? "su",
eventLoopGroup: MultiThreadedEventLoopGroup(numberOfThreads: 1)
)
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
var httpClient: HTTPClient!
var client: Client!

override func setUp() async throws {
httpClient = HTTPClient(eventLoopGroupProvider: .shared(eventLoopGroup))
client = try Client(
baseUrl: "http://localhost:8080/api/",
username: ProcessInfo.processInfo.environment["HAYSTACK_USER"] ?? "su",
password: ProcessInfo.processInfo.environment["HAYSTACK_PASSWORD"] ?? "su",
httpClient: httpClient
)
try await client.open()
}

override func tearDown() async throws {
try await client.close()
try httpClient.syncShutdown()
}

func testCloseAndOpen() async throws {
Expand Down
5 changes: 3 additions & 2 deletions Tests/HaystackTests/CoordTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ final class CoordTests: XCTestCase {
let value = try Coord(latitude: 40, longitude: -111.84)
let jsonString = #"{"_kind":"coord","lat":40,"lng":-111.84}"#

// Must encode/decode b/c JSON ordering is not deterministic
let encodedData = try JSONEncoder().encode(value)
XCTAssertEqual(
String(data: encodedData, encoding: .utf8),
jsonString
try JSONDecoder().decode(Coord.self, from: encodedData),
value
)

let decodedData = try XCTUnwrap(jsonString.data(using: .utf8))
Expand Down
Loading

0 comments on commit 1302dbd

Please sign in to comment.