-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb6c84b
commit 4d23075
Showing
8 changed files
with
160 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
Tests/ValidatorTests/Fixtures/SemanticVersion-Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// swift-tools-version:5.6 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SemanticVersion", | ||
products: [ | ||
.library( | ||
name: "SemanticVersion", | ||
targets: ["SemanticVersion"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target(name: "SemanticVersion", | ||
dependencies: [], | ||
resources: [.process("Documentation.docc")]), | ||
.testTarget(name: "SemanticVersionTests", dependencies: ["SemanticVersion"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright Dave Verwer, Sven A. Schmidt, and other contributors. | ||
// | ||
// 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. | ||
|
||
import XCTest | ||
|
||
@testable import ValidatorCore | ||
|
||
import AsyncHTTPClient | ||
import NIO | ||
import NIOHTTP1 | ||
|
||
|
||
final class PackageTests: XCTestCase { | ||
|
||
func test_decode_multiple_manifests() async throws { | ||
// This tests package dump for a package with four versioned package manifest files. | ||
// We use a captured response for the SwiftyJSON package which lists four manifest files | ||
// and then save no data for three of them that shouldn't be used and mock in the | ||
// SemanticVersion manifest files for the one that should be decoded. | ||
// setup | ||
Current = .mock | ||
Current.fileManager = .live | ||
var manifestsFetched = 0 | ||
Current.fetch = { client, url in | ||
switch url.absoluteString { | ||
case "https://raw.githubusercontent.com/org/1/main/[email protected]": | ||
// Package.decode -> fetch manifestURL data | ||
manifestsFetched += 1 | ||
return client.eventLoopGroup.next().makeSucceededFuture( | ||
try! .fixture(for: "SemanticVersion-Package.swift") | ||
) | ||
case "https://raw.githubusercontent.com/org/1/main/Package.swift", | ||
"https://raw.githubusercontent.com/org/1/main/[email protected]", | ||
"https://raw.githubusercontent.com/org/1/main/[email protected]": | ||
// Package.decode -> fetch manifestURL data - save bad data in the unrelated manifests to raise an error if used | ||
manifestsFetched += 1 | ||
return client.eventLoopGroup.next().makeSucceededFuture( | ||
.init() | ||
) | ||
case "https://api.github.com/repos/org/1/git/trees/main": | ||
// getManifestURLs -> Github.listRepositoryFilePaths -> Github.fetch | ||
return client.eventLoopGroup.next().makeSucceededFuture( | ||
// github-files-response-SwiftyJSON has multiple manifest files | ||
try! .fixture(for: "github-files-response-SwiftyJSON.json") | ||
) | ||
default: | ||
return client.eventLoopGroup.next().makeFailedFuture( | ||
Error.unexpectedCall("Current.fetch \(url.absoluteString)") | ||
) | ||
} | ||
} | ||
Current.shell = .live | ||
|
||
let client = MockClient(response: { .mock(status: .ok) }) | ||
|
||
// MUT | ||
let pkg = try await Package.decode(client: client, repository: .init(defaultBranch: "main", owner: "org", name: "1")) | ||
|
||
// validate | ||
XCTAssertEqual(manifestsFetched, 4) | ||
XCTAssertEqual(pkg.name, "SemanticVersion") | ||
} | ||
|
||
} | ||
|
||
|
||
struct MockClient: Client { | ||
var response: () -> HTTPClient.Response | ||
|
||
func execute(request: HTTPClient.Request, deadline: NIODeadline?) -> EventLoopFuture<HTTPClient.Response> { | ||
eventLoopGroup.next().makeSucceededFuture(response()) | ||
} | ||
|
||
let eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
} | ||
|
||
|
||
extension HTTPClient.Response { | ||
static func mock(status: HTTPResponseStatus) -> Self { | ||
.init(host: "host", status: status, version: .http1_1, headers: [:], body: nil) | ||
} | ||
} | ||
|
||
|
||
private enum Error: Swift.Error { | ||
case unexpectedCall(String) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters