Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ elapsed. Exit codes are `0` for success,
the ready timeout. An interrupted run exits `130`. The viewer and local server
are stopped automatically.

To run trusted host checks after the first rendered frame, repeat
`--test-cmd` options. They execute sequentially from the sketch directory,
and the test succeeds only after both the viewer schedule and all commands
finish:

```bash
fastled examples/Blink --test --test-log=out/run.log \
--test-cmd="python scripts/check_ready.py" \
--test-cmd="python scripts/check_runtime_state.py"
```

Commands use `/bin/sh -c` on Unix and `%COMSPEC% /D /S /C` (normally
`cmd.exe`) on Windows, with null stdin and no extra console window. Pipes,
redirection, `&&`, and quoting follow the platform shell; invoke
`powershell -NoProfile -Command ...` explicitly for PowerShell syntax.
Command stdout and stderr are drained and mirrored to the terminal. With
`--test-log`, timestamped stream markers are appended, such as
`[fastled-test-cmd 0 stdout] ...` and `[fastled-test-cmd 0] exit=0`.
Commands are trusted input and inherit the user's environment and filesystem
permissions. A spawn failure or nonzero command exit returns `1`; the total
timeout (`124`) and interrupt (`130`) also terminate the active command and
its descendants.

## Features

### Browser Compatibility
Expand Down
28 changes: 28 additions & 0 deletions crates/fastled-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ pub(crate) struct Cli {
)]
pub(crate) test_ready_timeout_secs: f64,

/// Run a trusted host command after the first rendered frame. May be repeated.
#[arg(
long,
requires = "test",
value_name = "COMMAND",
help_heading = "Production testing"
)]
pub(crate) test_cmd: Vec<String>,

/// Use the latest tagged FastLED release when initialising examples with --init.
/// Defaults to `master`; tagged releases older than the meson migration cannot be built.
#[arg(long)]
Expand Down Expand Up @@ -341,6 +350,25 @@ mod tests {
assert_eq!(cli.test_screenshot, Some(PathBuf::from("out-{n:03}.png")));
assert_eq!(cli.test_log, Some(PathBuf::from("run.log")));
assert!(cli.test_exit_on_error);
assert!(cli.test_cmd.is_empty());
}

#[test]
fn repeated_test_commands_preserve_declaration_order() {
let cli = Cli::parse_from([
"fastled",
"sketch",
"--test",
"--test-cmd=first",
"--test-cmd",
"second",
]);
assert_eq!(cli.test_cmd, vec!["first", "second"]);
}

#[test]
fn test_commands_require_master_switch() {
assert!(Cli::try_parse_from(["fastled", "sketch", "--test-cmd=echo hi"]).is_err());
}

#[test]
Expand Down
71 changes: 66 additions & 5 deletions crates/fastled-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ pub(crate) fn compile_and_test(dir: &str, cli: &Cli) -> ExitCode {
let mut ready = false;
let mut page_error = false;
let mut saved = HashSet::new();
let mut viewer_done: Option<u8> = None;
let mut commands_done = plan.commands.is_empty();
let (command_tx, mut command_rx) = tokio::sync::mpsc::channel(256);
let mut command_tx = Some(command_tx);
let mut _command_task: Option<CommandTaskGuard> = None;

loop {
tokio::select! {
Expand Down Expand Up @@ -385,6 +390,19 @@ pub(crate) fn compile_and_test(dir: &str, cli: &Cli) -> ExitCode {
if !write_test_log(&mut log_file, "[fastled-test] ready") {
return test_exit(TestOutcome::Failure);
}
if !plan.commands.is_empty() {
let Some(command_sender) = command_tx.take() else {
eprintln!("fastled: command runner was already started");
return test_exit(TestOutcome::Failure);
};
_command_task = Some(CommandTaskGuard(tokio::spawn(
test_mode::run_test_commands(
plan.commands.clone(),
NormalizedPath::new(&sketch_dir),
command_sender,
),
)));
}
}
}
Some(server::TestEvent::ViewerLog(line)) => {
Expand Down Expand Up @@ -413,23 +431,66 @@ pub(crate) fn compile_and_test(dir: &str, cli: &Cli) -> ExitCode {
);
return test_exit(TestOutcome::Failure);
}
return test_exit(if plan.exit_on_error && page_error {
TestOutcome::PageError
} else {
TestOutcome::Success
});
viewer_done = Some(code);
if commands_done {
return test_exit(if plan.exit_on_error && page_error {
TestOutcome::PageError
} else { TestOutcome::Success });
}
}
None => {
eprintln!("fastled: test event channel closed unexpectedly");
return test_exit(TestOutcome::Failure);
}
}
}
command_event = command_rx.recv(), if !commands_done => {
match command_event {
Some(test_mode::TestCommandEvent::Start { index }) => {
let marker = format!("[fastled-test-cmd {index}] start");
println!("{marker}");
if !write_test_log(&mut log_file, &marker) { return test_exit(TestOutcome::Failure); }
}
Some(test_mode::TestCommandEvent::Output { index, stream, line }) => {
let stream_name = match stream { test_mode::CommandStream::Stdout => "stdout", test_mode::CommandStream::Stderr => "stderr" };
let marker = format!("[fastled-test-cmd {index} {stream_name}] {line}");
match stream { test_mode::CommandStream::Stdout => println!("{line}"), test_mode::CommandStream::Stderr => eprintln!("{line}") }
if !write_test_log(&mut log_file, &marker) { return test_exit(TestOutcome::Failure); }
}
Some(test_mode::TestCommandEvent::Exit { index, code }) => {
let marker = format!("[fastled-test-cmd {index}] exit={code}");
println!("{marker}");
if !write_test_log(&mut log_file, &marker) { return test_exit(TestOutcome::Failure); }
}
Some(test_mode::TestCommandEvent::Done(result)) => {
commands_done = true;
if let Err(message) = result {
eprintln!("fastled: {message}");
return test_exit(TestOutcome::Failure);
}
if viewer_done.is_some() {
return test_exit(if plan.exit_on_error && page_error { TestOutcome::PageError } else { TestOutcome::Success });
}
}
None => {
eprintln!("fastled: command task ended without a completion result");
return test_exit(TestOutcome::Failure);
}
}
}
}
}
})
}

struct CommandTaskGuard(tokio::task::JoinHandle<()>);

impl Drop for CommandTaskGuard {
fn drop(&mut self) {
self.0.abort();
}
}

fn test_exit(outcome: TestOutcome) -> ExitCode {
ExitCode::from(outcome.exit_code())
}
Expand Down
1 change: 1 addition & 0 deletions crates/fastled-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ mod tests {
test_exit_on_error: false,
test_timeout_secs: 120.0,
test_ready_timeout_secs: 15.0,
test_cmd: Vec::new(),
latest: false,
branch: None,
commit: None,
Expand Down
Loading
Loading