Skip to content

Commit

Permalink
Check the output of wasm in dev subcommand test (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
omochi authored May 28, 2024
1 parent f7d0b56 commit 305c204
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ let package = Package(
dependencies: [
"CartonFrontend",
"SwiftToolchain",
"WebDriver",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
]
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ struct CartonFrontendDevCommand: AsyncParsableCommand {
)
let localURL = try await server.start()
if !skipAutoOpen {
openInSystemBrowser(url: localURL)
do {
try openInSystemBrowser(url: localURL)
} catch {
terminal.write("open browser failed: \(error)", inColor: .red)
}
}
try await server.waitUntilStop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct BrowserTestRunner: TestRunner {
try await client.goto(url: localURL)
} else {
disposer = {}
openInSystemBrowser(url: localURL)
try openInSystemBrowser(url: localURL)
}
let hadError = try await server.waitUntilTestFinished()
try await disposer()
Expand Down
39 changes: 24 additions & 15 deletions Sources/CartonKit/Server/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ public protocol BuilderProtocol {
}

public actor Server {
public enum Error: Swift.Error & CustomStringConvertible {
case invalidURL(String)
case noOpenBrowserPlatform

public var description: String {
switch self {
case .invalidURL(let string): "Invalid URL: \(string)"
case .noOpenBrowserPlatform: "This platform cannot launch a browser."
}
}
}

final class Connection: Hashable {
let channel: Channel

Expand Down Expand Up @@ -105,8 +117,8 @@ public actor Server {

private var serverChannel: (any Channel)!

/// Local URL of this server, `https://128.0.0.1:8080/` by default.
private let localURL: String
/// Local URL of this server, `https://127.0.0.1:8080/` by default.
private let localURL: URL

/// Whether a build that could be triggered by this server is currently running.
private var isBuildCurrentlyRunning = false
Expand Down Expand Up @@ -156,7 +168,11 @@ public actor Server {
public init(
_ configuration: Configuration
) async throws {
localURL = "http://\(configuration.host):\(configuration.port)/"
let localURLString = "http://\(configuration.host):\(configuration.port)/"
guard let localURL = URL(string: localURLString) else {
throw Error.invalidURL(localURLString)
}
self.localURL = localURL
watcher = nil
self.configuration = configuration

Expand Down Expand Up @@ -222,7 +238,7 @@ public actor Server {
connections.remove(connection)
}

public func start() async throws -> String {
public func start() async throws -> URL {
let group = MultiThreadedEventLoopGroup.singleton
let upgrader = NIOWebSocketServerUpgrader(
maxFrameSize: Int(UInt32.max),
Expand Down Expand Up @@ -421,25 +437,18 @@ extension Server {
}

/// Attempts to open the specified URL string in system browser on macOS and Linux.
/// - Returns: true if launching command returns successfully.
@discardableResult
public func openInSystemBrowser(url: String) -> Bool {
public func openInSystemBrowser(url: URL) throws {
#if os(macOS)
let openCommand = "open"
#elseif os(Linux)
let openCommand = "xdg-open"
#else
return false
throw Server.Error.noOpenBrowserPlatform
#endif
let process = Process(
arguments: [openCommand, url],
arguments: [openCommand, url.absoluteString],
outputRedirection: .none,
startNewProcessGroup: true
)
do {
try process.launch()
return true
} catch {
return false
}
try process.launch()
}
4 changes: 2 additions & 2 deletions Sources/WebDriver/WebDriverClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public struct WebDriverClient {
return try encoder.encode(body)
}

public func goto(url: String) async throws {
public func goto(url: URL) async throws {
struct Request: Encodable {
let url: String
}
var request = URLRequest(url: URL(string: makeSessionURL("url"))!)
request.httpMethod = "POST"
request.httpBody = try Self.makeRequestBody(Request(url: url))
request.httpBody = try Self.makeRequestBody(Request(url: url.absoluteString))
request.addValue("carton", forHTTPHeaderField: "User-Agent")
_ = try await client.data(for: request)
}
Expand Down
39 changes: 38 additions & 1 deletion Tests/CartonCommandTests/FrontendDevServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import CartonCore
import CartonHelpers
import CartonKit
import SwiftToolchain
import WebDriver

final class FrontendDevServerTests: XCTestCase {
func testDevServerPublish() async throws {
Expand All @@ -29,13 +30,31 @@ final class FrontendDevServerTests: XCTestCase {

try await Process.run(["swift", "build", "--target", "carton-frontend"], terminal)

var gotHelloStdout = false
var gotHelloStderr = false

let devServer = Process(
arguments: [
"swift", "run", "carton-frontend", "dev",
"--skip-auto-open", "--verbose",
"--main-wasm-path", wasmFile.pathString,
"--resources", resourcesDir.pathString
]
],
outputRedirection: .stream(
stdout: { (chunk) in
let string = String(decoding: chunk, as: UTF8.self)

if string.contains("stdout: hello stdout") {
gotHelloStdout = true
}
if string.contains("stderr: hello stderr") {
gotHelloStderr = true
}

terminal.write(string)
}, stderr: { (_) in },
redirectStderr: true
)
)
try devServer.launch()
defer {
Expand Down Expand Up @@ -84,6 +103,24 @@ final class FrontendDevServerTests: XCTestCase {
let expected = try String(contentsOf: resourcesDir.appending(component: name).asURL)
XCTAssertEqual(styleCss, expected)
}

let service = try await WebDriverServices.find(terminal: terminal)
defer {
service.dispose()
}

let client = try await service.client()

try await client.goto(url: host)

try await withRetry(maxAttempts: 10, initialDelay: .seconds(3), retryInterval: .seconds(3)) {
if gotHelloStdout, gotHelloStderr {
return
}
throw CommandTestError("no output")
}

try await client.closeSession()
}

private func fetchBinary(
Expand Down
4 changes: 2 additions & 2 deletions Tests/WebDriverTests/WebDriverClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class WebDriverClientTests: XCTestCase {
let client = try await service.client(
httpClient: URLSessionWebDriverHTTPClient(session: .shared)
)
try await client.goto(url: "https://example.com")
try await client.goto(url: URL(string: "https://example.com")!)
try await client.closeSession()
}
#endif
Expand All @@ -44,7 +44,7 @@ final class WebDriverClientTests: XCTestCase {
let client = try await service.client(
httpClient: try XCTUnwrap(CurlWebDriverHTTPClient.find())
)
try await client.goto(url: "https://example.com")
try await client.goto(url: URL(string: "https://example.com")!)
try await client.closeSession()
}
}

0 comments on commit 305c204

Please sign in to comment.