Skip to content

Commit

Permalink
Get rid of dev-server dependency from test runner (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun authored Nov 17, 2024
1 parent 2fdf063 commit e861472
Show file tree
Hide file tree
Showing 32 changed files with 718 additions and 798 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
- name: Install dependencies for macOS
run: brew bundle

- run: npm install && npx playwright install chromium

- name: Build the project
run: swift build --build-tests

Expand Down Expand Up @@ -89,6 +91,8 @@ jobs:
sudo apt-get update && sudo apt-get install -y
wabt binaryen
- run: npm install && npx playwright install chromium

- name: Install WebAssembly runtime
uses: bytecodealliance/actions/wasmtime/setup@v1

Expand Down
92 changes: 46 additions & 46 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import PackageDescription

#if swift(<5.9.2)
#warning("Swift 5.9.1 or earlier is not supported by carton")
#warning("Swift 5.9.1 or earlier is not supported by carton")
#endif

let package = Package(
Expand Down Expand Up @@ -33,7 +33,7 @@ let package = Package(
.target(
name: "CartonDriver",
dependencies: [
"SwiftToolchain",
"SwiftToolchain"
]
),
.executableTarget(
Expand All @@ -57,52 +57,52 @@ let package = Package(
]
),
.plugin(
name: "CartonBundlePlugin",
capability: .command(
intent: .custom(
verb: "carton-bundle",
description: "Produces an optimized app bundle for distribution."
)
),
dependencies: ["carton-frontend-slim"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md"
]
name: "CartonBundlePlugin",
capability: .command(
intent: .custom(
verb: "carton-bundle",
description: "Produces an optimized app bundle for distribution."
)
),
dependencies: ["carton-frontend-slim"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md",
]
),
.plugin(
name: "CartonTestPlugin",
capability: .command(
intent: .custom(
verb: "carton-test",
description: "Run the tests in a WASI environment."
)
),
dependencies: ["carton-frontend"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md"
]
name: "CartonTestPlugin",
capability: .command(
intent: .custom(
verb: "carton-test",
description: "Run the tests in a WASI environment."
)
),
dependencies: ["carton-frontend-slim"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md",
]
),
.plugin(
name: "CartonDevPlugin",
capability: .command(
intent: .custom(
verb: "carton-dev",
description: "Watch the current directory, host the app, rebuild on change."
)
),
dependencies: ["carton-frontend"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md"
]
name: "CartonDevPlugin",
capability: .command(
intent: .custom(
verb: "carton-dev",
description: "Watch the current directory, host the app, rebuild on change."
)
),
dependencies: ["carton-frontend"],
exclude: [
"CartonCore/README.md",
"CartonPluginShared/README.md",
]
),
.executableTarget(name: "carton-plugin-helper"),
.target(
name: "CartonFrontend",
dependencies: [
"CartonKit",
"CartonKit"
]
),
.target(
Expand All @@ -113,7 +113,6 @@ let package = Package(
.product(name: "NIO", package: "swift-nio"),
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"CartonHelpers",
"WebDriver",
"WasmTransformer",
],
exclude: ["Utilities/README.md"],
Expand All @@ -124,27 +123,27 @@ let package = Package(
.target(
name: "SwiftToolchain",
dependencies: [
"CartonCore",
"CartonCore"
],
exclude: ["Utilities/README.md"]
),
.target(
/** Shim target to import missing C headers in Darwin and Glibc modulemap. */
name: "TSCclibc",
cSettings: [
.define("_GNU_SOURCE", .when(platforms: [.linux])),
.define("_GNU_SOURCE", .when(platforms: [.linux]))
]
),
.target(
/** Cross-platform access to bare `libc` functionality. */
name: "TSCLibc"
/** Cross-platform access to bare `libc` functionality. */
name: "TSCLibc"
),
.target(
name: "CartonHelpers",
dependencies: [
"TSCclibc",
"TSCLibc",
"CartonCore"
"CartonCore",
],
exclude: ["Basics/README.md"]
),
Expand All @@ -156,7 +155,7 @@ let package = Package(
name: "WebDriver",
dependencies: [
.product(name: "NIO", package: "swift-nio"),
"CartonHelpers"
"CartonHelpers",
]
),
// This target is used only for release automation tasks and
Expand All @@ -165,6 +164,7 @@ let package = Package(
name: "carton-release",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"CartonHelpers",
"CartonCore",
]
),
Expand Down
42 changes: 22 additions & 20 deletions Plugins/CartonTestPlugin/CartonTestPluginCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ struct CartonTestPluginCommand: CommandPlugin {

func performCommand(context: PluginContext, arguments: [String]) async throws {
try checkSwiftVersion()
try checkHelpFlag(arguments, subcommand: "test", context: context)
try checkHelpFlag(
arguments, frontend: "carton-frontend-slim", subcommand: "test", context: context)

let productName = "\(context.package.displayName)PackageTests"

Expand Down Expand Up @@ -71,25 +72,25 @@ struct CartonTestPluginCommand: CommandPlugin {
let wasmFileName = "\(productName).wasm"
testProductArtifactPath = buildDirectory.appending(subpath: wasmFileName).string
#if compiler(>=5.10)
var buildParameters = PackageManager.BuildParameters()
options.environment.applyBuildParameters(&buildParameters)
applyExtraBuildFlags(from: &extractor, parameters: &buildParameters)
var buildParameters = PackageManager.BuildParameters()
options.environment.applyBuildParameters(&buildParameters)
applyExtraBuildFlags(from: &extractor, parameters: &buildParameters)

let build = try packageManager.build(.product(productName), parameters: buildParameters)
guard build.succeeded else {
throw Error("Failed to build test product: \(build.logText)")
}
guard FileManager.default.fileExists(atPath: testProductArtifactPath) else {
throw Error("Product \(productName) did not produce \(buildDirectory)!?")
}
let build = try packageManager.build(.product(productName), parameters: buildParameters)
guard build.succeeded else {
throw Error("Failed to build test product: \(build.logText)")
}
guard FileManager.default.fileExists(atPath: testProductArtifactPath) else {
throw Error("Product \(productName) did not produce \(buildDirectory)!?")
}
#else
// NOTE: Old SwiftPM does not allow to build *only tests* from plugin, so we expect
// the test product to be built already by external wrapper command.
guard FileManager.default.fileExists(atPath: testProductArtifactPath) else {
throw Error(
"Failed to find \"\(wasmFileName)\" in \(buildDirectory). Please build \"\(productName)\" product first"
)
}
// NOTE: Old SwiftPM does not allow to build *only tests* from plugin, so we expect
// the test product to be built already by external wrapper command.
guard FileManager.default.fileExists(atPath: testProductArtifactPath) else {
throw Error(
"Failed to find \"\(wasmFileName)\" in \(buildDirectory). Please build \"\(productName)\" product first"
)
}
#endif
}

Expand All @@ -107,14 +108,15 @@ struct CartonTestPluginCommand: CommandPlugin {
"test",
"--prebuilt-test-bundle-path", testProductArtifactPath,
"--environment", options.environment.rawValue,
"--plugin-work-directory", context.pluginWorkDirectory.string
"--plugin-work-directory", context.pluginWorkDirectory.string,
]
args += (options.pid.map { ["--pid", $0] } ?? [])
args += resourcesPaths.flatMap {
["--resources", $0.string]
}
args += extractor.remainingArguments
let frontend = try makeCartonFrontendProcess(context: context, arguments: args)
let frontend = try makeCartonFrontendProcess(
context: context, frontend: "carton-frontend-slim", arguments: args)
try frontend.checkRun(printsLoadingMessage: false, forwardExit: true)
}

Expand Down
3 changes: 1 addition & 2 deletions Sources/CartonFrontend/CartonFrontendCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public struct CartonFrontendCommand: AsyncParsableCommand {
abstract: "📦 Watcher, bundler, and test runner for your SwiftWasm apps.",
version: cartonVersion,
subcommands: [
CartonFrontendDevCommand.self,
CartonFrontendTestCommand.self
CartonFrontendDevCommand.self
]
)

Expand Down
11 changes: 5 additions & 6 deletions Sources/CartonFrontend/Commands/CartonFrontendDevCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

import ArgumentParser
import CartonHelpers
import CartonCore
import CartonHelpers
import CartonKit
import Foundation

Expand All @@ -28,8 +28,10 @@ enum DevCommandError: Error & CustomStringConvertible {

var description: String {
switch self {
case .noBuildRequestOption: "--build-request option is necessary if you want to watch, but has not been specified."
case .noBuildResponseOption: "--build-response option is necessary if you want to watch, but has not been specified."
case .noBuildRequestOption:
"--build-request option is necessary if you want to watch, but has not been specified."
case .noBuildResponseOption:
"--build-response option is necessary if you want to watch, but has not been specified."
case .failedToOpenBuildRequestPipe: "failed to open build request pipe."
case .failedToOpenBuildResponsePipe: "failed to open build response pipe."
case .pluginConnectionClosed: "connection with the plugin has been closed."
Expand All @@ -50,9 +52,6 @@ struct CartonFrontendDevCommand: AsyncParsableCommand {
@Flag(help: "When specified, build in the release mode.")
var release = false

@Option(help: "Turn on runtime checks for various behavior.")
private var sanitize: SanitizeVariant?

@Flag(name: .shortAndLong, help: "Don't clear terminal window after files change.")
var verbose = false

Expand Down
Loading

0 comments on commit e861472

Please sign in to comment.