Skip to content

Commit

Permalink
Snapshot Testing: Integration in CI & Docs & Typos
Browse files Browse the repository at this point in the history
* Add needed environment variable to CI pipeline for snapshot tests.
* Replace unwrap() calls with more context.
* Add Documentation in code.
* Fix typos.
  • Loading branch information
AmmarAbouZor committed Oct 29, 2024
1 parent 7bb2eca commit de749d0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/lint_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,7 @@ jobs:
- name: install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Execute tests
run: cargo chipmunk test core wrapper wasm -u print
env:
# Environment variable is needed for snapshot testing in Rust via `insta` crate
CI: true
run: cargo chipmunk test core wrapper wasm -u print
5 changes: 4 additions & 1 deletion .github/workflows/pullrequest_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,7 @@ jobs:
- name: install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- name: Execute tests
run: cargo chipmunk test core wrapper wasm -u print
env:
# Environment variable is needed for snapshot testing in Rust via `insta` crate
CI: true
run: cargo chipmunk test core wrapper wasm -u print
17 changes: 17 additions & 0 deletions application/apps/indexer/session/tests/snapshot_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
//! Snapshot testing is set here using the crate `insta` and optionally its CLI tool `cargo-insta`.
//!
//! Each test will created a snapshot file for its results, which will be saved with the path
//! './snapshots/{test_name}.snap'. These files will be used as reference for future tests then
//! tests will fail on changes.
//! Changes can be seen in our build CLI tool and can be accepted via CLI arguments, however it's
//! better to use the CLI tool `cargo insta` because it's output is colored and more clear compared
//! to the build CLI tool and it provides the possibility to review and accept each change one by
//! one, on the contrary to our build tool than can only accept all changes at once.
//!
//! Snapshot files are parts of the repository and valid changes in them must be committed as well.
//!
//! Running the tests via `cargo test` will create temporary files with the name pattern
//! '{name}.snap.new'. These files are used by `cargo-insta` tool and they aren't part of the
//! repo history.
//! Running the tests via `cargo chipmunk test` will avoid creating these temporary files.

mod utls;

use std::path::PathBuf;
Expand Down
16 changes: 10 additions & 6 deletions application/apps/indexer/session/tests/snapshot_tests/utls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl SessionFiles {
.to_string(),
content_lines: std::fs::read_to_string(entry_path)
.map(|content| content.lines().map(ToString::to_string).collect())
.unwrap_or_default(),
.unwrap_or_default(), // Use empty content for binary files.
});

attachments.extend(atts_iter);
Expand All @@ -80,7 +80,7 @@ pub fn cleanup_session_files(session_file: &PathBuf) {
let session_dir = session_dir_form_file(session_file);
if session_dir.exists() {
std::fs::remove_dir_all(session_dir)
.expect("Removing generated temporary session attachments directory shouldn't fial");
.expect("Removing generated temporary session attachments directory shouldn't fail");
}
}

Expand All @@ -92,7 +92,7 @@ fn session_dir_form_file(session_file: &Path) -> PathBuf {
.to_str()
.and_then(|file| file.strip_suffix(SESSION_FILE_SUFFIX))
.map(PathBuf::from)
.unwrap()
.expect("Session path can't fail while converting to string")
}

/// Runs a processor observe session generating the session files in Chipmunk temporary directory
Expand Down Expand Up @@ -125,13 +125,17 @@ pub async fn run_observe_session<P: Into<PathBuf>>(
while let Some(feedback) = receiver.recv().await {
match feedback {
CallbackEvent::FileRead | CallbackEvent::SessionDestroyed => break,
CallbackEvent::SessionError(err) => panic!("Recieved session error: {err:#?}"),
CallbackEvent::SessionError(err) => panic!("Received session error: {err:#?}"),
CallbackEvent::OperationError { error, .. } => {
panic!("Recieved operation error: {error:#?}")
panic!("Received operation error: {error:#?}")
}
_ => {}
}
}

session.get_state().get_session_file().await.unwrap()
session
.get_state()
.get_session_file()
.await
.expect("We must have a session file after observe session is done")
}
7 changes: 5 additions & 2 deletions cli/src/jobs_runner/jobs_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ pub struct JobsState {
pub struct JobsConfig {
fail_fast: bool,
is_release_build: bool,
// Custom specifications for the given jobs.
/// Custom specifications for the given jobs.
custom_specs: Vec<String>,
/// Set to accept the changes in snapshot testing.
/// Snapshot testing is currently implemented in some Rust parts only.
accept_snapshots: bool,
}

Expand Down Expand Up @@ -162,7 +164,8 @@ impl JobsState {
self.configuration.custom_specs.as_slice()
}

/// Gets if changes of all snapshots in test should be accepted.
/// Gets if changes of all snapshots in tests should be accepted.
/// Snapshot testing is implemented only for some Rust parts.
pub fn accept_snapshots(&self) -> bool {
self.configuration.accept_snapshots
}
Expand Down

0 comments on commit de749d0

Please sign in to comment.