Skip to content

Commit

Permalink
chore: set up actual test CI
Browse files Browse the repository at this point in the history
  • Loading branch information
crepererum committed Aug 12, 2023
1 parent 491425b commit b5fa44f
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 10 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ jobs:
toolchain: stable
override: true

- name: Install Geckodriver
uses: browser-actions/setup-geckodriver@latest
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Start Geckodriver
run: geckodriver &

- name: Cache
uses: Swatinem/rust-cache@v2

Expand All @@ -57,6 +65,17 @@ jobs:
with:
command: test
args: --all-features --workspace
env:
TUTANOTA_CLI_USERNAME: ${{ secrets.TUTANOTA_CLI_USERNAME }}
TUTANOTA_CLI_PASSWORD: ${{ secrets.TUTANOTA_CLI_PASSWORD }}

- name: Preserve Screenshots
uses: actions/upload-artifact@v3
if: failure()
with:
name: screenshots
path: *.png
if-no-files-found: ignore

- name: cargo doc
uses: actions-rs/cargo@v1
Expand Down
106 changes: 105 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ tokio = { version = "1.30.0", features = ["macros", "rt-multi-thread"] }
tracing = "0.1.38"
tracing-log = "0.1.3"
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }

[dev-dependencies]
assert_cmd = "2.0.12"
predicates = { version = "3.0.3", default-features = false }
4 changes: 2 additions & 2 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ pub struct LoggingCLIConfig {
pub fn setup_logging(config: LoggingCLIConfig) -> Result<()> {
LogTracer::init()?;

let filter = match config.log_verbose_count {
let base_filter = match config.log_verbose_count {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
};
let filter = EnvFilter::try_new(filter)?;
let filter = EnvFilter::try_new(format!("{base_filter},hyper=info"))?;

let subscriber = FmtSubscriber::builder().with_env_filter(filter).finish();

Expand Down
25 changes: 25 additions & 0 deletions src/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pub async fn perform_login(config: LoginCLIConfig, webdriver: &WebDriver) -> Res
.context("wait for login")??;
debug!("login done");

confirm_dialog(webdriver)
.await
.context("confirm potential dialog")?;

Ok(())
}

Expand All @@ -94,3 +98,24 @@ async fn has_new_email_button(webdriver: &WebDriver) -> Result<bool> {

Ok(false)
}

async fn confirm_dialog(webdriver: &WebDriver) -> Result<()> {
debug!("confirm potential dialogs");

let Some(dialog) = webdriver.find_at_most_one(By::ClassName("dialog")).await.context("find dialog box")? else {
debug!("no dialog found");
return Ok(());
};
debug!("found dialog, trying to click OK");

let ok_button = dialog
.find_one_with_attr(By::Tag("button"), "title", "Ok")
.await
.context("find OK button")?;
debug!("found OK button");

ok_button.click().await.context("click OK button")?;
debug!("clicked OK button");

Ok(())
}
32 changes: 25 additions & 7 deletions src/webdriver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::Path;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::Parser;
Expand All @@ -13,6 +13,14 @@ use crate::error::MultiResultExt;
/// Webdriver CLI config.
#[derive(Debug, Parser)]
pub struct WebdriverCLIConfig {
/// Create screenshot on failure.
#[clap(long, default_value_t = false)]
screenshot_on_failure: bool,

/// Path for screenshot.
#[clap(long, default_value = "./screenshot.png")]
screenshot_path: PathBuf,

/// Webdriver port.
#[clap(long, default_value_t = 4444)]
webdriver_port: u16,
Expand Down Expand Up @@ -50,15 +58,25 @@ where
let driver = WebDriver::new(&addr, caps)
.await
.context("webdriver setup")?;
driver.maximize_window().await.context("maximize window")?;
debug!("webdriver setup done");

let res = f(&driver).await;
let res_f = f(&driver).await;

driver
.quit()
.await
.context("webdriver shutdown")
.combine(res)
let res_screenshot = if res_f.is_err() && config.screenshot_on_failure {
driver
.screenshot(&config.screenshot_path)
.await
.context("create screenshot")
} else {
Ok(())
};

let res_shutdown = driver.quit().await.context("webdriver shutdown");

res_shutdown
.combine(res_screenshot)
.combine(res_f)
.map_err(|e| e.into_anyhow())?;
debug!("webdriver shutdown done");

Expand Down
53 changes: 53 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::sync::{LockResult, Mutex, MutexGuard};

use assert_cmd::Command;
use predicates::prelude::*;

/// We can only have a single webdriver session.
static WEBDRIVER_MUTEX: Mutex<()> = Mutex::new(());

#[test]
fn test_help() {
let mut cmd = cmd();
cmd.arg("--help").assert().success();
}

#[test]
fn test_list_folders() {
let _guard = webdriver_mutex();
let mut cmd = cmd();
cmd.arg("--screenshot-on-failure")
.arg("screenshot-path=test_list_folders.png")
.arg("-vv")
.arg("list-folders")
.assert()
.success()
.stdout(predicate::str::contains(
["Inbox", "Drafts", "Sent", "Trash", "Archive", "Spam"].join("\n"),
));
}

#[test]
fn test_export() {
let _guard = webdriver_mutex();
let mut cmd = cmd();
cmd.arg("--screenshot-on-failure")
.arg("screenshot-path=test_export.png")
.arg("-vv")
.arg("export")
.arg("--folder=Archive")
.assert()
.success();
}

fn cmd() -> Command {
Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap()
}

fn webdriver_mutex() -> MutexGuard<'static, ()> {
match WEBDRIVER_MUTEX.lock() {
LockResult::Ok(guard) => guard,
// poisoned locks are OK
LockResult::Err(e) => e.into_inner(),
}
}

0 comments on commit b5fa44f

Please sign in to comment.