Skip to content

Commit

Permalink
🦄 refactor: Simplify test apis, add doc
Browse files Browse the repository at this point in the history
- Merge a lot of functions into script_run
  • Loading branch information
wychlw committed Sep 19, 2024
1 parent e3f01de commit 5473150
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 310 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ jobs:
- runner: ubuntu-latest
target: ppc64le
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libssl-dev openssl
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand Down Expand Up @@ -72,11 +72,11 @@ jobs:
- runner: ubuntu-latest
target: armv7
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libssl-dev openssl
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand All @@ -103,10 +103,10 @@ jobs:
- runner: windows-latest
target: x86
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
choco install -y openssl
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand All @@ -133,10 +133,10 @@ jobs:
- runner: macos-14
target: aarch64
steps:
- uses: actions/checkout@v4
- name: Install dependencies
run: |
brew install [email protected]
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.x
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/target
*.log
*.cast
*.deprecated

# Byte-compiled / optimized / DLL files

Expand Down
16 changes: 9 additions & 7 deletions src/exec/cli_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ use std::error::Error;
use crate::cli::tty::InnerTty;

pub trait CliTestApi: InnerTty {
fn script_run(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>>;
fn assert_script_run(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>>;
///
///
/// You may found this func includes assert_script_run and script_output
fn script_run(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn Error>>;
fn background_script_run(&mut self, script: &str) -> Result<(), Box<dyn Error>>;
fn writeln(&mut self, script: &str) -> Result<(), Box<dyn Error>>;
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<(), Box<dyn Error>>;
// fn script_output(&mut self, script: &str) -> Result<Vec<u8>, Box<dyn Error>>;
// fn validate_script_output(&mut self, script: &str, expected_output: &str) -> Result<(), Box<dyn Error>>;
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<String, Box<dyn Error>>;
}

pub trait SudoCliTestApi: CliTestApi {
fn script_sudo(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>>;
fn assert_script_sudo(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>>;
///
///
/// You may found this func includes assert_script_sudo and script_output
fn script_sudo(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn Error>>;
}
112 changes: 87 additions & 25 deletions src/exec/cli_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
consts::DURATION, err, info, cli::tty::{DynTty, InnerTty, Tty, WrapperTty}, util::{anybase::AnyBase, util::rand_string}
};

use super::cli_api::CliTestApi;
use super::cli_api::{CliTestApi, SudoCliTestApi};

pub struct CliTester {
inner: DynTty,
Expand Down Expand Up @@ -71,7 +71,7 @@ impl InnerTty for CliTester {
}

impl CliTestApi for CliTester {
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<(), Box<dyn Error>> {
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<String, Box<dyn Error>> {
let begin = Instant::now();
let mut buf = Vec::new();
info!("Waiting for string {{{}}}", expected);
Expand All @@ -94,9 +94,10 @@ impl CliTestApi for CliTester {
}
}

Ok(())
let res = String::from_utf8(buf)?;
Ok(res)
}
fn script_run(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>> {
fn script_run(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn Error>> {
let mut cmd = script.to_owned();
let echo_content_rand = String::from_utf8(rand_string(8)).unwrap();

Expand All @@ -106,27 +107,7 @@ impl CliTestApi for CliTester {

self.run_command(&cmd)?;

let res = self.wait_serial(&echo_content_rand, timeout);
if let Err(e) = res {
if e.to_string() == "Timeout" {
return Ok(());
}
return Err(e);
}
Ok(())
}
fn assert_script_run(&mut self, script: &str, timeout: u32) -> Result<(), Box<dyn Error>> {
let mut cmd = script.to_owned();
let echo_content_rand = String::from_utf8(rand_string(8)).unwrap();

cmd += "&& echo ";
cmd += &echo_content_rand;
cmd += " \n";

self.run_command(&cmd)?;

self.wait_serial(&echo_content_rand, timeout)?;
Ok(())
self.wait_serial(&echo_content_rand, timeout)
}
fn background_script_run(&mut self, script: &str) -> Result<(), Box<dyn Error>> {
let mut cmd = script.to_owned();
Expand All @@ -139,3 +120,84 @@ impl CliTestApi for CliTester {
self.run_command(&cmd)
}
}

pub struct SudoCliTester {
inner: CliTester,
}

impl SudoCliTester {
pub fn build(inner: DynTty) -> SudoCliTester {
SudoCliTester {
inner: CliTester::build(inner),
}
}
}

impl AnyBase for SudoCliTester {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn into_any(self: Box<Self>) -> Box<dyn Any> {
self
}
}

impl Tty for SudoCliTester {
// Note: This will SKIP the logic in the tester
fn read(&mut self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
self.inner.read()
}
// Note: This will SKIP the logic in the tester
fn read_line(&mut self) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
self.inner.read_line()
}
fn write(&mut self, data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
self.inner.write(data)
}
}

impl WrapperTty for SudoCliTester {
fn exit(self) -> DynTty {
self.inner.exit()
}
}

impl InnerTty for SudoCliTester {
fn inner_ref(&self) -> &DynTty {
self.inner.inner_ref()
}
fn inner_mut(&mut self) -> &mut DynTty {
self.inner.inner_mut()
}
}

impl CliTestApi for SudoCliTester {
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<String, Box<dyn std::error::Error>> {
self.inner.wait_serial(expected, timeout)
}
fn script_run(&mut self, script: &str, timeout: u32) -> Result<String, Box<dyn std::error::Error>> {
self.inner.script_run(script, timeout)
}
fn background_script_run(&mut self, script: &str) -> Result<(), Box<dyn std::error::Error>> {
self.inner.background_script_run(script)
}
fn writeln(&mut self, script: &str) -> Result<(), Box<dyn std::error::Error>> {
self.inner.writeln(script)
}
}

impl SudoCliTestApi for SudoCliTester {
fn script_sudo(
&mut self,
script: &str,
timeout: u32,
) -> Result<String, Box<dyn std::error::Error>> {
let mut cmd = String::from("sudo ");
cmd += script;
cmd += "\n";
self.inner.script_run(&cmd, timeout)
}
}
108 changes: 0 additions & 108 deletions src/exec/cli_exec_sudo.rs

This file was deleted.

28 changes: 28 additions & 0 deletions src/exec/gui_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Api for GUI part testing
//!
//! For GUI part, basicially we need to check the following:
//! - Is the current screen is the expected screen?
//! - Does the screen have the expected elements?
//! - Can we interact with the screen?
use std::error::Error;

use crate::gui::screen::Screen;

use super::needle::Needle;

/// The API can used for testing, with bypass [`Screen`] operations.
pub trait GuiTestApi: Screen {
/// Check if the current screen is the expected screen
fn assert_screen(&mut self, needle: Needle) -> Result<(), Box<dyn Error>>;
/// Check and click the target position
///
/// Suggest using assert_screen and click seperately, as futher consider adding relative position etc.
fn assert_screen_click(&mut self, needle: Needle) -> Result<(), Box<dyn Error>>;

/// Wait until current screen changed
fn wait_screen_change(&mut self, timeout: u32) -> Result<(), Box<dyn Error>>;

/// Wait and assert the screen won't change in timeout
fn wait_still_screen(&mut self, timeout: u32) -> Result<(), Box<dyn Error>>;
}
5 changes: 5 additions & 0 deletions src/exec/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod cli_api;
pub mod cli_exec;
pub mod gui_api;

pub mod needle;
Loading

0 comments on commit 5473150

Please sign in to comment.