Skip to content

Commit

Permalink
Feature: Impl GitHub Actions (#3)
Browse files Browse the repository at this point in the history
* feat(actions): Impled github actions for PR checking

* chore(tests): Impled mock for rabbit unit tests

* chore(pr): Fixed fmt warnings

---------

Co-authored-by: Bread White <breadrock1@email.net>
  • Loading branch information
breadrock1 and Bread White authored Oct 26, 2024
1 parent 0846e3d commit e4d7959
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Pull Request Actions

on:
push:
branches:
- master

pull_request:
branches:
- master
types:
- opened
- reopened
- synchronize

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose

fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Fmt
run: cargo fmt --all --verbose --check

clippy:
runs-on: ubuntu-latest
permissions: write-all
steps:
- uses: actions/checkout@v4
- name: Install clippy
run: rustup component add clippy
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features

test:
needs: [build, clippy, fmt]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Test
run: cargo test --all --verbose

build-platforms:
if: github.event_name == 'push'
strategy:
matrix:
platform:
- os-name: Linux-x86_64
runs-on: ubuntu-20.04
target: x86_64-unknown-linux-gnu

- os-name: Windows-x86_64
runs-on: windows-latest
target: x86_64-pc-windows-msvc

- os-name: macOS-x86_64
runs-on: macOS-latest
target: x86_64-apple-darwin

runs-on: ${{ matrix.platform.runs-on }}
permissions: write-all
needs: [test]
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
with:
target: ${{ matrix.platform.target }}
- name: Add rustup target ${{ matrix.platform.target }}
run: rustup target add ${{ matrix.platform.target }}
- name: Build app for ${{matrix.platform.target }}
run: cargo build --release --target ${{ matrix.platform.target }}
27 changes: 27 additions & 0 deletions tests/mocks/mock_rmq_publish.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use news_rss::publish::models::PublishNews;
use news_rss::publish::rabbit::config::RabbitConfig;
use news_rss::publish::Publisher;
use news_rss::ServiceConnect;

pub struct MockRabbitPublisher {}

#[async_trait::async_trait]
impl ServiceConnect for MockRabbitPublisher {
type Config = RabbitConfig;
type Error = anyhow::Error;
type Client = Self;

async fn connect(_config: &Self::Config) -> Result<Self::Client, Self::Error> {
Ok(MockRabbitPublisher {})
}
}

#[async_trait::async_trait]
impl Publisher for MockRabbitPublisher {
type Error = ();

async fn publish(&self, msg_body: &PublishNews) -> Result<(), Self::Error> {
tracing::info!("rabbit confirm message successful: {}", msg_body.id());
Ok(())
}
}
1 change: 1 addition & 0 deletions tests/mocks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod mock_rmq_publish;
9 changes: 7 additions & 2 deletions tests/test_publish_feeds.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod mocks;
mod tests_helper;

use mocks::mock_rmq_publish::MockRabbitPublisher;
use news_rss::config::ServiceConfig;
use news_rss::feeds::rss_feeds::RssFeeds;
use news_rss::feeds::FetchTopic;
use news_rss::{logger, ServiceConnect};
use std::sync::Arc;
use std::time::Duration;

const TEST_TIME_EXECUTION: u64 = 5;
Expand All @@ -13,7 +16,9 @@ async fn test_rss_feeds() -> Result<(), anyhow::Error> {
let config = ServiceConfig::new()?;
logger::init_logger(config.logger())?;

let publish = tests_helper::build_rmq_publish(&config).await?;
let publish = MockRabbitPublisher::connect(config.publish().rmq()).await?;
let publish = Arc::new(publish);

#[cfg(feature = "publish-offline")]
let publish = tests_helper::build_pgsql_publish(&config).await?;

Expand Down Expand Up @@ -43,7 +48,7 @@ async fn test_rss_feeds() -> Result<(), anyhow::Error> {
.map(|it| tokio::spawn(async move { it.launch_fetching().await }))
.collect::<Vec<_>>();

let _ = tests_helper::rabbit_consumer(config.publish().rmq()).await?;
// let _ = tests_helper::rabbit_consumer(config.publish().rmq()).await?;

tokio::time::sleep(Duration::from_secs(TEST_TIME_EXECUTION)).await;

Expand Down
5 changes: 5 additions & 0 deletions tests/tests_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ use news_rss::publish::rabbit::RabbitPublisher;
use news_rss::ServiceConnect;
use std::sync::Arc;

#[allow(dead_code)]
const TEST_AMQP_CONSUMER_TAG: &str = "test-news-rss-consumer";

#[allow(dead_code)]
#[allow(unused_assignments)]
#[allow(unused_variables)]
pub async fn rabbit_consumer(config: &RabbitConfig) -> Result<(), anyhow::Error> {
let conn_props = ConnectionProperties::default();
let connection = Connection::connect(config.address(), conn_props).await?;
Expand Down Expand Up @@ -112,6 +116,7 @@ pub async fn build_redis_cache(config: &ServiceConfig) -> Result<Arc<RedisClient
Ok(cache)
}

#[allow(dead_code)]
pub async fn build_rmq_publish(
config: &ServiceConfig,
) -> Result<Arc<RabbitPublisher>, anyhow::Error> {
Expand Down

0 comments on commit e4d7959

Please sign in to comment.