-
-
Notifications
You must be signed in to change notification settings - Fork 46
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
Check the output of wasm in dev subcommand test #473
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. localhostは127です |
||
private let localURL: URL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 早めにURL型にします |
||
|
||
/// Whether a build that could be triggered by this server is currently running. | ||
private var isBuildCurrentlyRunning = false | ||
|
@@ -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 | ||
|
||
|
@@ -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), | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stringを受け取ってBoolを返していましたが、 引数については型を活用します。 おそらく元々の実装はブラウザオープンに失敗しても動作を止めないことを想定していますが、 |
||
#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() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,13 +29,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 { | ||
|
@@ -84,6 +102,16 @@ final class FrontendDevServerTests: XCTestCase { | |
let expected = try String(contentsOf: resourcesDir.appending(component: name).asURL) | ||
XCTAssertEqual(styleCss, expected) | ||
} | ||
|
||
try openInSystemBrowser(url: host) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. そうですね。 |
||
|
||
for _ in 0..<30 { | ||
try await Task.sleep(for: .seconds(1)) | ||
if gotHelloStdout, gotHelloStderr { break } | ||
} | ||
|
||
XCTAssertTrue(gotHelloStdout) | ||
XCTAssertTrue(gotHelloStderr) | ||
} | ||
|
||
private func fetchBinary( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ final class WebDriverClientTests: XCTestCase { | |
let client = try await WebDriverClient.newSession( | ||
endpoint: checkRemoteURL(), httpClient: .shared | ||
) | ||
try await client.goto(url: "https://example.com") | ||
try await client.goto(url: URL(string: "https://github.com/swiftwasm/carton")!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 一応自分たちが管理してるURLの方が良いと思います。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The example.com is managed by ICANN. It should be more reliable than GitHub IMO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. なるほど、OKです。戻します。 |
||
try await client.closeSession() | ||
} | ||
} |
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.
従来通り、起動しなくても処理は続ける