Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0106c19
Make pfw testable.
mbrandonw Jan 25, 2026
4e958cf
wip
mbrandonw Jan 25, 2026
5a4c419
wip
mbrandonw Jan 25, 2026
9623250
tests for login
mbrandonw Jan 25, 2026
1353703
more tests
mbrandonw Jan 25, 2026
2fa7d76
improve file system
mbrandonw Jan 25, 2026
91f7fb4
wip
mbrandonw Jan 25, 2026
b135b89
getting closer to install tests
mbrandonw Jan 25, 2026
7986079
support symlinks in FileSystem
mbrandonw Jan 25, 2026
b803cc3
tests for install
mbrandonw Jan 26, 2026
e8b1fa0
require either tool or path
mbrandonw Jan 26, 2026
fd9d370
more install tests
mbrandonw Jan 26, 2026
09715da
fix assertCommandThrows
mbrandonw Jan 26, 2026
93114cf
fix tests
mbrandonw Jan 26, 2026
5f34084
tests for expired token
mbrandonw Jan 26, 2026
ceadfff
clean up
mbrandonw Jan 26, 2026
67d1ffa
ds store
mbrandonw Jan 26, 2026
5b3d47e
delete ds store
mbrandonw Jan 26, 2026
6280edf
moved test dependencies to test target
mbrandonw Jan 26, 2026
19c5b41
wip
mbrandonw Jan 26, 2026
0407847
require instead of force unwrap
mbrandonw Jan 26, 2026
8d56e09
missing imports
mbrandonw Jan 26, 2026
95aca8e
fix auth dependency
mbrandonw Jan 26, 2026
18aaa3f
format
mbrandonw Jan 26, 2026
3a51864
test and format in ci
mbrandonw Jan 26, 2026
ed350fb
linux fix
mbrandonw Jan 26, 2026
9b52fc5
test only
mbrandonw Jan 26, 2026
3e7a14e
more linux fixes
mbrandonw Jan 26, 2026
5a74e17
another linux fix
mbrandonw Jan 26, 2026
652ed50
format
mbrandonw Jan 26, 2026
5ce263f
format
mbrandonw Jan 26, 2026
5d8c597
linux fix
mbrandonw Jan 26, 2026
f4e1c7b
format fix
mbrandonw Jan 26, 2026
06e4574
bring back nonisolated nonsending
mbrandonw Jan 26, 2026
818f6c9
wip
mbrandonw Jan 26, 2026
589b625
linux fix
mbrandonw Jan 26, 2026
7a9c1d6
nonisolated nonsending by default
mbrandonw Jan 26, 2026
ce62fdb
build only for linux
mbrandonw Jan 26, 2026
36331be
wip
stephencelis Jan 28, 2026
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
Binary file added .DS_Store
Binary file not shown.
96 changes: 96 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/pfw.xcscheme
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>
74 changes: 73 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,32 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"),
.package(url: "https://github.com/pointfreeco/swift-concurrency-extras", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/xctest-dynamic-overlay", from: "1.0.0"),
.package(url: "https://github.com/weichsel/ZIPFoundation", from: "0.9.20"),
],
targets: [
.executableTarget(
name: "pfw",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"),
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "ZIPFoundation", package: "ZIPFoundation")
]
),
.testTarget(
name: "pfwTests",
dependencies: [
.product(name: "DependenciesTestSupport", package: "swift-dependencies"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
"pfw"
]
)
],
swiftLanguageModes: [.v6]
Expand Down
Binary file added Sources/pfw/.DS_Store
Binary file not shown.
156 changes: 156 additions & 0 deletions Sources/pfw/Dependencies/Auth.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
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
}
Comment on lines +10 to +13
Copy link
Copy Markdown
Member Author

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.


struct InMemoryAuth: Auth {
enum Error: Swift.Error, Equatable {
case missingRedirectURL
case missingToken
}

var redirectURL: URL?
var token: String?

init(redirectURL: URL? = nil, token: String? = nil) {
self.redirectURL = redirectURL
self.token = token
}

func start() async throws -> URL {
guard let redirectURL else {
throw Error.missingRedirectURL
}
return redirectURL
}

func waitForToken() async throws -> String {
guard let token else {
throw Error.missingToken
}
return token
}
}

enum AuthKey: DependencyKey {
#if canImport(Network)
static var liveValue: any Auth { try! LocalAuthServer() }
#endif
static var testValue: any Auth { InMemoryAuth() }
}

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
Loading
Loading