Skip to content

Commit 0dcf50b

Browse files
authored
Merge pull request #58 from firefly-zero/test-cmd
firefly_cli test
2 parents 50a7d47 + 8dafbaa commit 0dcf50b

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/args.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ pub enum Commands {
3333
/// Launch firefly-emulator.
3434
Emulator(EmulatorArgs),
3535

36+
/// Run tests.
37+
#[clap(alias("tests"), alias("pytest"))]
38+
Test(TestArgs),
39+
3640
/// List all badges (aka achievements) defined in the given app.
3741
#[clap(alias("badge"), alias("achievements"), alias("achievement"))]
3842
Badges(BadgesArgs),
@@ -160,6 +164,9 @@ pub struct NameSetArgs {
160164
pub name: String,
161165
}
162166

167+
#[derive(Debug, Parser)]
168+
pub struct TestArgs {}
169+
163170
#[derive(Debug, Parser, Default)]
164171
pub struct BuildArgs {
165172
/// Path to the project root.

src/cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub fn run_command(vfs: PathBuf, command: &Commands) -> anyhow::Result<()> {
1010
Export(args) => cmd_export(&vfs, args),
1111
Import(args) => cmd_import(&vfs, args),
1212
New(args) => cmd_new(args),
13+
Test(args) => cmd_test(args),
1314
Emulator(args) => cmd_emulator(args),
1415
Badges(args) => cmd_badges(&vfs, args),
1516
Boards(args) => cmd_boards(&vfs, args),

src/commands/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod new;
1515
mod repl;
1616
mod runtime;
1717
mod shots;
18+
mod test;
1819
mod vfs;
1920

2021
pub use badges::cmd_badges;
@@ -34,4 +35,5 @@ pub use new::cmd_new;
3435
pub use repl::cmd_repl;
3536
pub use runtime::{cmd_exit, cmd_id, cmd_launch, cmd_restart, cmd_screenshot};
3637
pub use shots::cmd_shots_download;
38+
pub use test::cmd_test;
3739
pub use vfs::cmd_vfs;

src/commands/test.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use crate::{args::TestArgs, langs::check_output};
2+
use anyhow::{bail, Context, Ok, Result};
3+
use std::{path::Path, process::Command};
4+
5+
/// Run tests.
6+
pub fn cmd_test(_args: &TestArgs) -> Result<()> {
7+
let venv_path = Path::new(".venv");
8+
let bin_path = venv_path.join("bin");
9+
let pytest_path = bin_path.join("pytest");
10+
11+
// Ensure venv exists.
12+
if !venv_path.is_dir() {
13+
println!("⏳️ creating venv...");
14+
let mut cmd = Command::new("python3");
15+
let cmd = cmd.args(["-m", "venv", ".venv"]);
16+
let output = cmd.output().context("create venv")?;
17+
check_output(&output).context("create venv")?;
18+
}
19+
20+
// Ensure pytest and firefly-test are installed.
21+
if !pytest_path.is_file() {
22+
println!("⏳️ installing dependencies...");
23+
let pip_path = bin_path.join("pip");
24+
let mut cmd = Command::new(&pip_path);
25+
let cmd = cmd.args(["install", "pytest", "firefly-test"]);
26+
let output = cmd.output().context("install firefly-test")?;
27+
check_output(&output).context("install firefly-test")?;
28+
}
29+
30+
// Run pytest
31+
println!("⏳️ running pytest...");
32+
let mut cmd = Command::new(&pytest_path);
33+
let status = cmd.status().context("run pytest")?;
34+
if !status.success() {
35+
let code = status.code().unwrap_or(1);
36+
bail!("subprocess exited with status code {code}");
37+
}
38+
39+
Ok(())
40+
}

0 commit comments

Comments
 (0)