Skip to content

Commit

Permalink
test: replace real OBS tests with mock server
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Jan 2, 2025
1 parent b8870a1 commit 0f9c1a8
Show file tree
Hide file tree
Showing 30 changed files with 2,006 additions and 497 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ jobs:
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
toolchain: [stable, "1.70"]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
uses: dtolnay/rust-toolchain@stable
- name: Configure cache
uses: Swatinem/rust-cache@v2
- name: Install cargo-nextest
uses: taiki-e/install-action@cargo-nextest
- name: Test
run: cargo test
run: cargo nextest run --all-features
lint:
name: Lint
runs-on: ubuntu-latest
Expand Down
48 changes: 48 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Deploy coverage report
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
build:
name: Build coverage report
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-toolchain@stable
with:
components: llvm-tools
- name: Configure cache
uses: Swatinem/rust-cache@v2
- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov
- name: Install just
uses: taiki-e/install-action@just
- name: Setup Pages
id: pages
uses: actions/configure-pages@v5
- name: Run tests with coverage
run: just coverage
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./target/llvm-cov/html
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Improve feature flag documentation and enable feature markers on items in docs.rs, that show under what conditions certain items are available.
- Revamp the integration tests to use a mocking server instead of running against a real OBS instance. This was long overdue as the tests didn't work anymore and it became harder and harder to make all tests work due to bugs or behavior in OBS.

## [0.14.0] - 2025-01-01

Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ uuid = { version = "1.11.0", features = ["serde"] }
anyhow = "1.0.95"
dotenvy = "0.15.7"
serde_test = "1.0.177"
test-log = { version = "0.2.14", default-features = false, features = ["trace"] }
tokio = { version = "1.38.1", features = ["fs", "macros", "rt-multi-thread", "time"] }
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
uuid = { version = "1.11.0", features = ["v8"] }

[features]
default = []
Expand Down
26 changes: 7 additions & 19 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,21 @@
set dotenv-load := true

_default:
@just --list --unsorted

# format all Rust source code
fmt:
cargo +nightly fmt --all

# run unit and integration tests
test:
cargo test
cargo test --all-features --test integration -- --test-threads 1
cargo nextest run --all-features

# run integration tests with coverage
coverage:
cargo install cargo-llvm-cov
rustup component add llvm-tools-preview

cargo llvm-cov --remap-path-prefix --html --all-features -- --test-threads 1
cargo llvm-cov --remap-path-prefix --no-run --json --summary-only | \
cargo llvm-cov --html --all-features
cargo llvm-cov --no-run --json --summary-only | \
jq -c '.data[0].totals.lines.percent | { \
schemaVersion: 1, \
label: "coverage", \
message: "\(.|round)%", \
color: (if . < 70 then "red" elif . < 80 then "yellow" else "green" end) \
}' > target/llvm-cov/html/coverage.json

# upload coverage to GitHub Pages
upload-coverage: coverage
git checkout gh-pages
rm -rf coverage coverage.json index.html style.css
cp -R target/llvm-cov/html/ .
git add -A coverage coverage.json index.html style.css
git commit -m "Coverage for $(git rev-parse --short main)"
git push
git checkout main
7 changes: 2 additions & 5 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,7 @@ impl Client {
let (mut write, mut read) = socket.split();

let receivers = Arc::new(ReceiverList::default());
let receivers2 = Arc::clone(&receivers);

let reidentify_receivers = Arc::new(ReidentifyReceiverList::default());
let reidentify_receivers2 = Arc::clone(&reidentify_receivers);

#[cfg(feature = "events")]
let (event_sender, _) = broadcast::channel(config.broadcast_capacity);
Expand All @@ -315,8 +312,8 @@ impl Client {
read,
#[cfg(feature = "events")]
events_tx,
receivers2,
reidentify_receivers2,
Arc::clone(&receivers),
Arc::clone(&reidentify_receivers),
));

let write = Mutex::new(write);
Expand Down
12 changes: 12 additions & 0 deletions src/requests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ pub(crate) struct RequestBatch<'a> {
/// Bit flags for possible event subscriptions, that can be enabled when connecting to the OBS
/// instance.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
#[cfg_attr(
feature = "test-integration",
derive(serde::Deserialize),
serde(from = "u32")
)]
#[serde(into = "u32")]
pub struct EventSubscription(u32);

Expand Down Expand Up @@ -211,6 +216,13 @@ impl From<EventSubscription> for u32 {
}
}

#[cfg(feature = "test-integration")]
impl From<u32> for EventSubscription {
fn from(value: u32) -> Self {
Self::from_bits_truncate(value)
}
}

#[allow(dead_code)]
#[derive(Serialize_repr)]
#[repr(i8)]
Expand Down
8 changes: 8 additions & 0 deletions src/requests/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Requests related to the user interface.
use std::fmt::{self, Display};

use bitflags::bitflags;
use serde::Serialize;

Expand Down Expand Up @@ -234,6 +236,12 @@ impl Default for QtGeometry {
}
}

impl Display for QtGeometry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.serialize())
}
}

/// Request information for [`crate::client::Ui::open_video_mix_projector`] and
/// [`crate::client::Ui::open_source_projector`] as part of [`QtGeometry`].
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand Down
20 changes: 0 additions & 20 deletions tests/README.md

This file was deleted.

7 changes: 4 additions & 3 deletions tests/integration/client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use anyhow::Result;
use obws::requests::EventSubscription;
use test_log::test;

use crate::common;

#[tokio::test]
#[test(tokio::test)]
async fn client() -> Result<()> {
let client = common::new_client().await?;
let (client, server) = common::new_client().await?;

client.reidentify(EventSubscription::ALL).await?;

Ok(())
server.stop().await
}
Loading

0 comments on commit 0f9c1a8

Please sign in to comment.