diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml new file mode 100644 index 0000000..c3673e6 --- /dev/null +++ b/.github/workflows/pull-request.yaml @@ -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 }} diff --git a/tests/mocks/mock_rmq_publish.rs b/tests/mocks/mock_rmq_publish.rs new file mode 100644 index 0000000..19aab50 --- /dev/null +++ b/tests/mocks/mock_rmq_publish.rs @@ -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 { + 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(()) + } +} diff --git a/tests/mocks/mod.rs b/tests/mocks/mod.rs new file mode 100644 index 0000000..fb78fb0 --- /dev/null +++ b/tests/mocks/mod.rs @@ -0,0 +1 @@ +pub mod mock_rmq_publish; diff --git a/tests/test_publish_feeds.rs b/tests/test_publish_feeds.rs index 20ddc37..dba6016 100644 --- a/tests/test_publish_feeds.rs +++ b/tests/test_publish_feeds.rs @@ -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; @@ -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?; @@ -43,7 +48,7 @@ async fn test_rss_feeds() -> Result<(), anyhow::Error> { .map(|it| tokio::spawn(async move { it.launch_fetching().await })) .collect::>(); - 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; diff --git a/tests/tests_helper.rs b/tests/tests_helper.rs index f413eb7..41799fe 100644 --- a/tests/tests_helper.rs +++ b/tests/tests_helper.rs @@ -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?; @@ -112,6 +116,7 @@ pub async fn build_redis_cache(config: &ServiceConfig) -> Result Result, anyhow::Error> {