Skip to content

Commit

Permalink
feat(tests): add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomio committed May 11, 2022
1 parent 0a7a77a commit 42ce400
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 32 deletions.
36 changes: 18 additions & 18 deletions .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,41 +67,41 @@ jobs:
runs-on: ${{ matrix.os }}
continue-on-error: true
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Rust
- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1.0.7
with:
toolchain: nightly
override: true
profile: minimal
target: ${{ matrix.target }}

- name: Build | Build
- name: Build the binary
uses: actions-rs/cargo@844f36862e911db73fe0815f00a4a2602c279505 # tag=v1.0.3
with:
command: build
args: --release --locked --target ${{ matrix.target }}
use-cross: ${{ matrix.os == 'ubuntu-latest' }}

- name: Post Build | Prepare artifacts [Windows]
- name: Prepare artifacts [Windows]
if: matrix.os == 'windows-latest'
run: |
cd target/${{ matrix.target }}/release
strip website-screenshot.exe
7z a ../../../${{ matrix.name }} website-screenshot.exe
cd -
- name: Post Build | Prepare artifacts [-nix]
- name: Prepare artifacts [-nix]
if: matrix.os != 'windows-latest'
run: |
cd target/${{ matrix.target }}/release
strip website-screenshot || true
tar czvf ../../../${{ matrix.name }} website-screenshot
cd -
- name: Deploy | Upload artifacts
- name: Upload artifacts
uses: actions/upload-artifact@6673cd052c4cd6fcf4b4e6e60ea986c889389535 # tag=v3
with:
name: ${{ matrix.name }}
Expand All @@ -112,18 +112,18 @@ jobs:
needs: [release_please, github_build]
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3
with:
fetch-depth: 0

- name: Setup | Artifacts
- name: Download the artifacts
uses: actions/download-artifact@fb598a63ae348fa914e94cd0ff38f362e927b741 # tag=v3

- name: Setup | Checksums
- name: Setup the Checksums
run: for file in website-screenshot-*/website-screenshot-*; do openssl dgst -sha256 -r "$file" | awk '{print $1}' > "${file}.sha256"; done

- name: Build | Add Artifacts to Release
- name: Add Artifacts to Release
uses: softprops/action-gh-release@1e07f4398721186383de40550babbdf2b84acfc5 # tag=v1
with:
files: website-screenshot-*/website-screenshot-*
Expand All @@ -135,37 +135,37 @@ jobs:
needs: release_please
if: ${{ needs.release_please.outputs.release_created == 'true' }}
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Rust
- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1.0.7
with:
toolchain: nightly
profile: minimal
override: true

- name: Build | Publish
- name: Publish to crates.io
run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}

publish_docker:
name: Publish website-screenshot image to container registries
name: Publish website-screenshot image
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Docker Buildx
- name: Setup Docker
uses: docker/setup-buildx-action@dc7b9719a96d48369863986a06765841d7ea23f6 # tag=v2.0.0

- name: Build | Login to GitHub Container Registry
- name: Login to GitHub Container Registry
uses: docker/login-action@49ed152c8eca782a232dede0303416e8f356c37b # tag=v2.0.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build | Push Docker Image
- name: Push Docker Image
uses: docker/build-push-action@e551b19e49efd4e98792db7592c17c09b89db8d8 # tag=v3.0.0
with:
push: true
Expand Down
72 changes: 59 additions & 13 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,87 @@ on:
pull_request:

jobs:
test:
name: Integration Tests
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
name:
- MSRV
- Nightly
- MacOS
- Windows

include:
- name: MSRV
toolchain: nightly-2022-05-04
os: ubuntu-latest

- name: Nightly
toolchain: nightly
os: ubuntu-latest

- name: MacOS
toolchain: nightly-2022-05-04
os: macOS-latest

- name: Windows
toolchain: nightly-2022-05-04
os: windows-latest

steps:
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true

- name: Install Nextest
uses: taiki-e/install-action@nextest

- name: Run Tests
run: cargo nextest run

msrv:
name: MSRV
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Rust
- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1
with:
profile: minimal
toolchain: nightly-2022-05-04
override: true

- name: Check | Verify the MSRV
- name: Verify the MSRV
run: cargo build

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Rust
- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1
with:
profile: minimal
toolchain: nightly
override: true

- name: Setup | Install rustfmt
- name: Install rustfmt
run: rustup component add rustfmt

- name: Check | Lint
- name: Run rustfmt
uses: actions-rs/cargo@844f36862e911db73fe0815f00a4a2602c279505 # tag=v1
with:
command: fmt
Expand All @@ -51,28 +97,28 @@ jobs:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Setup | Rust
- name: Install Rust
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af # tag=v1
with:
profile: minimal
toolchain: nightly
override: true

- name: Setup | Install clippy
- name: Install clippy
run: rustup component add clippy

- name: Check | Clippy
- name: Run clippy
run: cargo clippy -- -D warnings

dprint:
name: Dprint
runs-on: ubuntu-latest
steps:
- name: Setup | Checkout
- name: Checkout the Repository
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # tag=v3

- name: Check | Dprint
- name: Run Dprint
uses: dprint/check@22f4fe0ab3ac8ce823e23e43043333fc46b76e04 # tag=v2.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Creates a screenshot.
- `url` - The url of the website. (string, required)
- `fullscreen` - If you want to take a fullscreen screenshot. (boolean, optional, overrides the `FULLSCREEN_SCREENSHOT` environment variable)
- `check_nsfw` - If you want to check if the url is marked as NSFW. (boolean, optional, overrides the `CHECK_IF_NSFW` environment variable)
- `dark_mode` - If you want to take a screenshot in dark mode if the website supports it. (boolean, optional, overrides the `DARK_MODE` environment variable)

Example Payload:

Expand Down
29 changes: 29 additions & 0 deletions src/routes/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,32 @@ use actix_web::{get, HttpRequest};
pub async fn index(_req: HttpRequest) -> &'static str {
"Hello, world!"
}

#[cfg(test)]
mod tests {
use actix_web::body::to_bytes;
use actix_web::web::Bytes;
use actix_web::{test, App};

use super::*;

trait BodyTest {
fn as_str(&self) -> &str;
}

impl BodyTest for Bytes {
fn as_str(&self) -> &str {
unsafe { std::str::from_utf8_unchecked(self) }
}
}

#[actix_web::test]
async fn test_index() {
let app = test::init_service(App::new().service(index)).await;
let req = test::TestRequest::default().to_request();
let res = test::call_service(&app, req).await;

assert!(res.status().is_success());
assert_eq!("Hello, world!", to_bytes(res.into_body()).await.unwrap().as_str());
}
}
3 changes: 2 additions & 1 deletion src/routes/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ pub async fn screenshot(
"metadata": {
"url": &payload.url,
"fullscreen": payload.fullscreen,
"check_nsfw": payload.check_nsfw
"check_nsfw": payload.check_nsfw,
"dark_mode": payload.dark_mode
}
})))
}

0 comments on commit 42ce400

Please sign in to comment.