swiftwasm / carton

📦 Watcher, bundler, and test runner for your SwiftWasm apps
Apache License 2.0
406 stars 44 forks source link

ProcessInfo is empty when invoking `carton test` #400

Open stephencelis opened 1 year ago

stephencelis commented 1 year ago

I am trying to figure out how to detect, at runtime, from SwiftWasm, if it's being run from carton test, but ProcessInfo arguments and environment are totally empty. I was hoping to be able to see something like ["carton", "test", "--environment", "node"] and use this.

(I can't rely on arguments passed by the user, since this is library code.)

kateinoigakukun commented 1 year ago

The CommandLine.arguments (as well ProcessInfo also IIRC) follow the WASI arguments concept, which cannot see the host environment arguments. For example, when you are running a WASI app with wasmtime and run the following command:

$ wasmtime run main.wasm -- --help

The WASI runtime passes only ["main.wasm", "--help"] and don't pass ["wasmtime", "run"] to the guest WASI process. So it's impossible to detect it from given arguments.

That being said, we can consider injecting some environment variables that appear only for testing. Does it help your scenario?

stephencelis commented 1 year ago

That would definitely help, yup! This is a reliable way to detect XCTest runs from both Linux/Windows SPM and Xcode, so a way of doing the same for Wasm would be great.

stephencelis commented 1 year ago

(Also, just to confirm, is there any other existing way for Swift code to detect if it executing from carton tests? The subject of the issue is just one way I attempted to do so.)

kateinoigakukun commented 1 year ago

There's no way for now as far as I remember.