Skip to content

Commit

Permalink
feat: add --dry-run option to run command (#76)
Browse files Browse the repository at this point in the history
This option is useful user check their config file, and run tests in CI.
May be used in unit tests in the future.
  • Loading branch information
wangl-cc authored Oct 13, 2023
1 parent 778d641 commit 13de53b
Show file tree
Hide file tree
Showing 5 changed files with 559 additions and 208 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ jobs:
echo "MAA_CORE_DIR=$MAA_CORE_DIR" >> $GITHUB_ENV
- name: Try run with MaaCore
if: matrix.arch == 'x86_64'
env:
MAA_CONFIG_DIR: ${{ github.workspace }}/config_examples
run: |
export DYLD_FALLBACK_LIBRARY_PATH="$MAA_CORE_DIR"
cargo run -- version
cargo run -- run daily --dry-run --batch
- name: Test (maa-sys, static)
if: matrix.arch == 'x86_64'
run: |
Expand Down
2 changes: 1 addition & 1 deletion config_examples/asst.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
user_resource = true
resources = ["platform_diff/macOS"]
resources = ["platform_diff/iOS"]

[connection]
type = "ADB"
Expand Down
144 changes: 88 additions & 56 deletions maa-cli/src/config/asst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub struct AsstConfig {
#[serde(default)]
pub connection: Connection,
#[serde(default)]
pub instance_options: InstanceOption,
pub instance_options: InstanceOptions,
}

#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Deserialize, Default)]
pub struct InstanceOption {
pub struct InstanceOptions {
#[serde(default)]
pub touch_mode: Option<TouchMode>,
pub deployment_with_pause: Option<bool>,
Expand All @@ -33,9 +33,9 @@ pub enum TouchMode {
MacPlayTools,
}

impl<'a> From<TouchMode> for &'a str {
fn from(mode: TouchMode) -> Self {
match mode {
impl AsRef<str> for TouchMode {
fn as_ref(&self) -> &str {
match self {
TouchMode::ADB => "adb",
TouchMode::MiniTouch => "minitouch",
TouchMode::MAATouch => "maatouch",
Expand All @@ -46,13 +46,13 @@ impl<'a> From<TouchMode> for &'a str {

impl std::fmt::Display for TouchMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", <&str>::from(*self))
write!(f, "{}", self.as_ref())
}
}

impl maa_sys::ToCString for TouchMode {
fn to_cstring(self) -> maa_sys::Result<std::ffi::CString> {
<&str>::from(self).to_cstring()
self.as_ref().to_cstring()
}
}

Expand Down Expand Up @@ -117,57 +117,89 @@ impl super::FromFile for AsstConfig {}
mod tests {
use super::*;

#[test]
fn deserialize_example() {
let config: AsstConfig =
toml::from_str(&std::fs::read_to_string("../config_examples/asst.toml").unwrap())
.unwrap();
assert_eq!(
config,
AsstConfig {
user_resource: true,
resources: vec![String::from("platform_diff/macOS")],
connection: Connection::ADB {
adb_path: String::from("adb"),
device: String::from("emulator-5554"),
config: String::from("CompatMac"),
},
instance_options: InstanceOption {
touch_mode: Some(TouchMode::MAATouch),
deployment_with_pause: Some(false),
adb_lite_enabled: Some(false),
kill_adb_on_exit: Some(false),
},
}
);
mod touch_mode {
use super::*;

use std::ffi::CString;

use maa_sys::ToCString;

#[test]
fn to_cstring() {
assert_eq!(
TouchMode::ADB.to_cstring().unwrap(),
CString::new("adb").unwrap()
);
assert_eq!(
TouchMode::MiniTouch.to_cstring().unwrap(),
CString::new("minitouch").unwrap()
);
assert_eq!(
TouchMode::MAATouch.to_cstring().unwrap(),
CString::new("maatouch").unwrap()
);
assert_eq!(
TouchMode::MacPlayTools.to_cstring().unwrap(),
CString::new("MacPlayTools").unwrap()
);
}
}

#[test]
fn deserialize_empty() {
let config: AsstConfig = toml::from_str("").unwrap();
assert_eq!(
config,
AsstConfig {
user_resource: false,
resources: vec![],
connection: Connection::ADB {
adb_path: String::from("adb"),
device: String::from("emulator-5554"),
config: if cfg!(target_os = "macos") {
String::from("CompatMac")
} else if cfg!(target_os = "linux") {
String::from("CompatPOSIXShell")
} else {
String::from("General")
mod serde {
use super::*;

#[test]
fn deserialize_example() {
let config: AsstConfig =
toml::from_str(&std::fs::read_to_string("../config_examples/asst.toml").unwrap())
.unwrap();
assert_eq!(
config,
AsstConfig {
user_resource: true,
resources: vec![String::from("platform_diff/iOS")],
connection: Connection::ADB {
adb_path: String::from("adb"),
device: String::from("emulator-5554"),
config: String::from("CompatMac"),
},
instance_options: InstanceOptions {
touch_mode: Some(TouchMode::MAATouch),
deployment_with_pause: Some(false),
adb_lite_enabled: Some(false),
kill_adb_on_exit: Some(false),
},
},
instance_options: InstanceOption {
touch_mode: None,
deployment_with_pause: None,
adb_lite_enabled: None,
kill_adb_on_exit: None,
},
}
);
}
);
}

#[test]
fn deserialize_empty() {
let config: AsstConfig = toml::from_str("").unwrap();
assert_eq!(
config,
AsstConfig {
user_resource: false,
resources: vec![],
connection: Connection::ADB {
adb_path: String::from("adb"),
device: String::from("emulator-5554"),
config: if cfg!(target_os = "macos") {
String::from("CompatMac")
} else if cfg!(target_os = "linux") {
String::from("CompatPOSIXShell")
} else {
String::from("General")
},
},
instance_options: InstanceOptions {
touch_mode: None,
deployment_with_pause: None,
adb_lite_enabled: None,
kill_adb_on_exit: None,
},
}
);
}
}
}
Loading

0 comments on commit 13de53b

Please sign in to comment.