Skip to content

Commit 50aec7a

Browse files
committed
Add a local_espflash flag to allow running espflash without re-building (CI) and re-building (locally)
1 parent 55004e5 commit 50aec7a

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

.github/workflows/hil.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ jobs:
119119
echo "$PWD/xtask_app" >> "$GITHUB_PATH"
120120
121121
- name: Run all tests
122-
run: xtask run-tests --chip ${{ matrix.board.mcu }} -t 60
122+
run: xtask run-tests --chip ${{ matrix.board.mcu }} -t 60 --build=false

espflash/tests/flash_content.bin

Lines changed: 0 additions & 1 deletion
This file was deleted.

xtask/src/test_runner.rs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{
1313
};
1414

1515
use clap::Args;
16+
use log::info;
1617

1718
use crate::Result;
1819

@@ -36,6 +37,10 @@ pub struct RunTestsArgs {
3637
/// Timeout for test commands in seconds
3738
#[clap(short, long, default_value = "15")]
3839
pub timeout: u64,
40+
41+
/// Whether to build espflash before running tests, true by default
42+
#[arg(long, value_parser = clap::value_parser!(bool))]
43+
pub local_espflash: Option<bool>,
3944
}
4045

4146
/// A struct to manage and run tests for the espflash
@@ -48,16 +53,24 @@ pub struct TestRunner {
4853
pub timeout: Duration,
4954
/// Optional chip target for tests
5055
pub chip: Option<String>,
56+
/// Build espflash before running tests
57+
pub local_espflash: Option<bool>,
5158
}
5259

5360
impl TestRunner {
5461
/// Creates a new [TestRunner] instance
55-
pub fn new(workspace: &Path, tests_dir: PathBuf, timeout_secs: u64) -> Self {
62+
pub fn new(
63+
workspace: &Path,
64+
tests_dir: PathBuf,
65+
timeout_secs: u64,
66+
local_espflash: bool,
67+
) -> Self {
5668
Self {
5769
workspace: workspace.to_path_buf(),
5870
tests_dir,
5971
timeout: Duration::from_secs(timeout_secs),
6072
chip: None,
73+
local_espflash: Some(local_espflash),
6174
}
6275
}
6376

@@ -85,7 +98,8 @@ impl TestRunner {
8598
}
8699
}
87100

88-
fn spawn_and_capture_output(mut cmd: Command) -> Result<SpawnedCommandOutput> {
101+
fn spawn_and_capture_output(cmd: &mut Command) -> Result<SpawnedCommandOutput> {
102+
info!("Spawning command: {cmd:?}");
89103
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
90104
let mut child = cmd.spawn()?;
91105
let stdout = child.stdout.take().expect("Failed to capture stdout");
@@ -192,9 +206,36 @@ impl TestRunner {
192206
Ok(naturally_terminated)
193207
}
194208

209+
fn build_espflash(&self) {
210+
let mut cmd = Command::new("cargo");
211+
212+
log::info!("Building espflash...");
213+
cmd.args(["build", "-p", "espflash", "--release", "--"]);
214+
215+
let status = cmd.status().expect("Failed to build espflash");
216+
if !status.success() {
217+
panic!("espflash build failed with status: {status}");
218+
}
219+
}
220+
195221
fn create_espflash_command(&self, args: &[&str]) -> Command {
196222
let mut cmd = Command::new("cargo");
197-
cmd.args(["run", "-p", "espflash", "--release", "--"]);
223+
224+
// we need to distinguish between local and CI runs, on CI we are building
225+
// espflash and then copying the binary, so we can use just `espflash`
226+
match self.local_espflash {
227+
None | Some(true) => {
228+
log::info!("Running cargo run...");
229+
cmd.args(["run", "-p", "espflash", "--release", "--quiet", "--"]);
230+
}
231+
Some(false) => {
232+
log::info!("Using system espflash");
233+
let mut cmd = Command::new("espflash");
234+
cmd.args(args);
235+
return cmd;
236+
}
237+
}
238+
198239
cmd.args(args);
199240

200241
cmd
@@ -213,7 +254,7 @@ impl TestRunner {
213254
let mut cmd = self.create_espflash_command(args);
214255

215256
if let Some(expected) = expected_contains {
216-
let (mut child, output, h1, h2) = Self::spawn_and_capture_output(cmd)?;
257+
let (mut child, output, h1, h2) = Self::spawn_and_capture_output(&mut cmd)?;
217258
let start_time = Instant::now();
218259
let mut terminated_naturally = false;
219260

@@ -270,7 +311,7 @@ impl TestRunner {
270311
let mut cmd = self.create_espflash_command(args);
271312

272313
if let Some(expected) = expected_contains {
273-
let (mut child, output, h1, h2) = Self::spawn_and_capture_output(cmd)?;
314+
let (mut child, output, h1, h2) = Self::spawn_and_capture_output(&mut cmd)?;
274315
thread::sleep(duration);
275316
let _ = child.kill();
276317
let _ = child.wait();
@@ -885,7 +926,18 @@ pub fn run_tests(workspace: &Path, args: RunTestsArgs) -> Result<()> {
885926
log::info!("Running espflash tests");
886927

887928
let tests_dir = workspace.join("espflash").join("tests");
888-
let test_runner = TestRunner::new(workspace, tests_dir, args.timeout);
929+
let test_runner = TestRunner::new(
930+
workspace,
931+
tests_dir,
932+
args.timeout,
933+
args.local_espflash.unwrap_or(true),
934+
);
935+
936+
// Build espflash before running test(s) so we are not "waisting" test's
937+
// duration or timeout
938+
if args.local_espflash.unwrap_or(true) {
939+
test_runner.build_espflash();
940+
}
889941

890942
match args.test.as_str() {
891943
"all" => {

0 commit comments

Comments
 (0)