-
Notifications
You must be signed in to change notification settings - Fork 12
Test coverage for pfw tool #5
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
Merged
Changes from 20 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
0106c19
Make pfw testable.
mbrandonw 4e958cf
wip
mbrandonw 5a4c419
wip
mbrandonw 9623250
tests for login
mbrandonw 1353703
more tests
mbrandonw 2fa7d76
improve file system
mbrandonw 91f7fb4
wip
mbrandonw b135b89
getting closer to install tests
mbrandonw 7986079
support symlinks in FileSystem
mbrandonw b803cc3
tests for install
mbrandonw e8b1fa0
require either tool or path
mbrandonw fd9d370
more install tests
mbrandonw 09715da
fix assertCommandThrows
mbrandonw 93114cf
fix tests
mbrandonw 5f34084
tests for expired token
mbrandonw ceadfff
clean up
mbrandonw 67d1ffa
ds store
mbrandonw 5b3d47e
delete ds store
mbrandonw 6280edf
moved test dependencies to test target
mbrandonw 19c5b41
wip
mbrandonw 0407847
require instead of force unwrap
mbrandonw 8d56e09
missing imports
mbrandonw 95aca8e
fix auth dependency
mbrandonw 18aaa3f
format
mbrandonw 3a51864
test and format in ci
mbrandonw ed350fb
linux fix
mbrandonw 9b52fc5
test only
mbrandonw 3e7a14e
more linux fixes
mbrandonw 5a74e17
another linux fix
mbrandonw 652ed50
format
mbrandonw 5ce263f
format
mbrandonw 5d8c597
linux fix
mbrandonw f4e1c7b
format fix
mbrandonw 06e4574
bring back nonisolated nonsending
mbrandonw 818f6c9
wip
mbrandonw 589b625
linux fix
mbrandonw 7a9c1d6
nonisolated nonsending by default
mbrandonw ce62fdb
build only for linux
mbrandonw 36331be
wip
stephencelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -60,3 +60,5 @@ fastlane/report.xml | |
| fastlane/Preview.html | ||
| fastlane/screenshots/**/*.png | ||
| fastlane/test_output | ||
|
|
||
| *.DS_STORE | ||
This file contains hidden or 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,96 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <Scheme | ||
| LastUpgradeVersion = "2620" | ||
| version = "1.7"> | ||
| <BuildAction | ||
| parallelizeBuildables = "YES" | ||
| buildImplicitDependencies = "YES" | ||
| buildArchitectures = "Automatic"> | ||
| <BuildActionEntries> | ||
| <BuildActionEntry | ||
| buildForTesting = "YES" | ||
| buildForRunning = "YES" | ||
| buildForProfiling = "YES" | ||
| buildForArchiving = "YES" | ||
| buildForAnalyzing = "YES"> | ||
| <BuildableReference | ||
| BuildableIdentifier = "primary" | ||
| BlueprintIdentifier = "pfw" | ||
| BuildableName = "pfw" | ||
| BlueprintName = "pfw" | ||
| ReferencedContainer = "container:"> | ||
| </BuildableReference> | ||
| </BuildActionEntry> | ||
| </BuildActionEntries> | ||
| </BuildAction> | ||
| <TestAction | ||
| buildConfiguration = "Debug" | ||
| selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
| selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
| shouldUseLaunchSchemeArgsEnv = "YES" | ||
| shouldAutocreateTestPlan = "YES"> | ||
| <Testables> | ||
| <TestableReference | ||
| skipped = "NO"> | ||
| <BuildableReference | ||
| BuildableIdentifier = "primary" | ||
| BlueprintIdentifier = "pfwTests" | ||
| BuildableName = "pfwTests" | ||
| BlueprintName = "pfwTests" | ||
| ReferencedContainer = "container:"> | ||
| </BuildableReference> | ||
| </TestableReference> | ||
| </Testables> | ||
| </TestAction> | ||
| <LaunchAction | ||
| buildConfiguration = "Debug" | ||
| selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
| selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
| launchStyle = "0" | ||
| useCustomWorkingDirectory = "NO" | ||
| ignoresPersistentStateOnLaunch = "NO" | ||
| debugDocumentVersioning = "YES" | ||
| debugServiceExtension = "internal" | ||
| allowLocationSimulation = "YES"> | ||
| <BuildableProductRunnable | ||
| runnableDebuggingMode = "0"> | ||
| <BuildableReference | ||
| BuildableIdentifier = "primary" | ||
| BlueprintIdentifier = "pfw" | ||
| BuildableName = "pfw" | ||
| BlueprintName = "pfw" | ||
| ReferencedContainer = "container:"> | ||
| </BuildableReference> | ||
| </BuildableProductRunnable> | ||
| <CommandLineArguments> | ||
| <CommandLineArgument | ||
| argument = "--version" | ||
| isEnabled = "YES"> | ||
| </CommandLineArgument> | ||
| </CommandLineArguments> | ||
| </LaunchAction> | ||
| <ProfileAction | ||
| buildConfiguration = "Release" | ||
| shouldUseLaunchSchemeArgsEnv = "YES" | ||
| savedToolIdentifier = "" | ||
| useCustomWorkingDirectory = "NO" | ||
| debugDocumentVersioning = "YES"> | ||
| <BuildableProductRunnable | ||
| runnableDebuggingMode = "0"> | ||
| <BuildableReference | ||
| BuildableIdentifier = "primary" | ||
| BlueprintIdentifier = "pfw" | ||
| BuildableName = "pfw" | ||
| BlueprintName = "pfw" | ||
| ReferencedContainer = "container:"> | ||
| </BuildableReference> | ||
| </BuildableProductRunnable> | ||
| </ProfileAction> | ||
| <AnalyzeAction | ||
| buildConfiguration = "Debug"> | ||
| </AnalyzeAction> | ||
| <ArchiveAction | ||
| buildConfiguration = "Release" | ||
| revealArchiveInOrganizer = "YES"> | ||
| </ArchiveAction> | ||
| </Scheme> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,126 @@ | ||
| import ArgumentParser | ||
| import Dependencies | ||
| import Foundation | ||
| import Synchronization | ||
|
|
||
| #if canImport(Network) | ||
| import Network | ||
| #endif | ||
|
|
||
| protocol Auth: Sendable { | ||
| func start() async throws -> URL | ||
| func waitForToken() async throws -> String | ||
| } | ||
|
|
||
| enum AuthKey: DependencyKey { | ||
| #if canImport(Network) | ||
| static var liveValue: any Auth { try! LocalAuthServer() } | ||
| #endif | ||
| } | ||
|
|
||
| extension DependencyValues { | ||
| var auth: any Auth { | ||
| get { self[AuthKey.self] } | ||
| set { self[AuthKey.self] = newValue } | ||
| } | ||
| } | ||
|
|
||
| #if canImport(Network) | ||
| actor LocalAuthServer: Auth { | ||
| private let listener: NWListener | ||
| private let queue = DispatchQueue(label: "pfw.auth.server") | ||
| private var tokenContinuation: CheckedContinuation<String, Error>? | ||
|
|
||
| init() throws { | ||
| listener = try NWListener(using: .tcp, on: .any) | ||
| } | ||
|
|
||
| func start() async throws -> URL { | ||
| let hasStarted = Mutex(false) | ||
| listener.stateUpdateHandler = { state in | ||
| guard state == .ready | ||
| else { return } | ||
| hasStarted.withLock { $0 = true } | ||
| } | ||
| listener.newConnectionHandler = { [weak self] connection in | ||
| Task { | ||
| connection.start(queue: self?.queue ?? .main) | ||
| await self?.receiveToken(from: connection) | ||
| } | ||
| } | ||
| listener.start(queue: queue) | ||
| while !hasStarted.withLock(\.self) { | ||
| try await Task.sleep(for: .seconds(0.1)) | ||
| // TODO: Timeout if takes too long | ||
| } | ||
| guard let port = listener.port else { | ||
| throw ValidationError("Unable to determine callback port.") | ||
| } | ||
| return URL(string: "http://127.0.0.1:\(port)/callback")! | ||
| } | ||
|
|
||
| func waitForToken() async throws -> String { | ||
| try await withCheckedThrowingContinuation { continuation in | ||
| tokenContinuation = continuation | ||
| } | ||
| } | ||
|
|
||
| private func receiveToken(from connection: NWConnection) { | ||
| connection.receive(minimumIncompleteLength: 1, maximumLength: 8_192) { | ||
| [weak self] data, _, _, error in | ||
| Task { | ||
| if let error { | ||
| await self?.finish(with: .failure(error)) | ||
| connection.cancel() | ||
| return | ||
| } | ||
| guard let data, let request = String(data: data, encoding: .utf8) else { | ||
| await self?.finish(with: .failure(ValidationError("Invalid request."))) | ||
| connection.cancel() | ||
| return | ||
| } | ||
| let token = Self.token(from: request) | ||
| if let token { | ||
| await self?.respond(connection: connection, success: true) | ||
| await self?.finish(with: .success(token)) | ||
| } else { | ||
| await self?.respond(connection: connection, success: false) | ||
| await self?.finish(with: .failure(ValidationError("Missing token in redirect."))) | ||
| } | ||
| connection.cancel() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func respond(connection: NWConnection, success: Bool) { | ||
| let message = | ||
| success | ||
| ? "You can return to the terminal. Login complete." | ||
| : "Login failed. Please return to the terminal." | ||
| let body = "<html><body><p>\(message)</p></body></html>" | ||
| let response = """ | ||
| HTTP/1.1 200 OK\r | ||
| Content-Type: text/html; charset=utf-8\r | ||
| Content-Length: \(body.utf8.count)\r | ||
| Connection: close\r | ||
| \r | ||
| \(body) | ||
| """ | ||
| connection.send(content: response.data(using: .utf8), completion: .contentProcessed { _ in }) | ||
| } | ||
|
|
||
| private func finish(with result: Result<String, Error>) { | ||
| tokenContinuation?.resume(with: result) | ||
| tokenContinuation = nil | ||
| listener.cancel() | ||
| } | ||
|
|
||
| private static func token(from request: String) -> String? { | ||
| return String( | ||
| request | ||
| .dropFirst("GET /callback?token=".count) | ||
| .prefix(while: { $0 != " " }) | ||
| ) | ||
| } | ||
| } | ||
| #endif | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This represents the local auth server we start up and ask for a token. There's an in-memory version below that can just immediately give back the callback URL and token when asked. We should beef it up to allow emitting errors too and get test coverage on that, but we can do that later.