-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add snapshot test for 2 wire objects
Summary: This diff adds a way to do snapshot tests in SC, by simply calling a macro. It uses that as an example on two wire objects to show it works. Motivation: - Using snapshot testing in wire object will make it super clear what changes are happening on the wire format, which is useful to prevent breakages. (see quip) - I'm going with approach #3 from proposal, but both #2 and #3 would need snapshot testing. How? - I'm using [insta](https://docs.rs/insta/1.8.0/insta/), a rust crate for snapshot testing. Unfortunately it depends on some cargo-specific stuff that doesn't work with buck, but I was able to get it working without patching the package by bypassing the Cargo dependent stuff. - Insta also provides some cargo exensions on top of it to compare snapshots, which we can't have, so I made it clear on the test errors (see test plan) how to update the snapshots (via flag). Future - On future diffs I'll add a snapshot test for all the wire objects, this just adds to a first few to show it works and set up the plumbing. - Writing objects manually for snapshot tests for **every** wire object would be a large amount of code, and troublesome when adding new (specially big) wire objects. We already have wire objects implementing `Arbitrary`, so I think it should be possible to use this to generate simple snapshot tests on automatically generated objects. (It will need some extra work to make sure they are created consistently.) Reviewed By: markbt Differential Revision: D30958077 fbshipit-source-id: 3d8663e7897e5f6eb4b97c24f47b37ef2f138e5a
- Loading branch information
1 parent
a95661f
commit 30cc107
Showing
7 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# @generated by autocargo from //eden/scm/lib/insta_ext:insta_ext | ||
[package] | ||
name = "insta_ext" | ||
version = "0.1.0" | ||
edition = "2018" | ||
|
||
[dependencies] | ||
insta = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This crate allows using the insta crate, for snapshot tests, without needing to use Cargo. |
6 changes: 6 additions & 0 deletions
6
eden/scm/lib/insta_ext/snapshots/edenapi_types__wire__commit__tests__snapshot-2.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: commit.rs | ||
expression: "WireEphemeralPrepareResponse{bubble_id: NonZeroU64::new(12),}" | ||
|
||
--- | ||
{"1":12} |
6 changes: 6 additions & 0 deletions
6
eden/scm/lib/insta_ext/snapshots/edenapi_types__wire__commit__tests__snapshot.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
source: commit.rs | ||
expression: "WireSnapshotState{}" | ||
|
||
--- | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This software may be used and distributed according to the terms of the | ||
* GNU General Public License version 2. | ||
*/ | ||
|
||
pub fn setup() { | ||
const WORKSPACE: &str = "INSTA_WORKSPACE_ROOT"; | ||
const UPDATE: &str = "INSTA_UPDATE"; | ||
if std::env::var(WORKSPACE).is_err() { | ||
let mut root = std::path::PathBuf::from(file!()); | ||
root.pop(); | ||
root.pop(); | ||
std::env::set_var(WORKSPACE, root); | ||
} | ||
if std::env::var(UPDATE).is_err() { | ||
std::env::set_var(UPDATE, "no"); | ||
} | ||
} | ||
|
||
pub fn run(is_cargo: bool, snapshot: &str, file: &str, module: &str, line: u32, expr: &str) { | ||
let command = if is_cargo { | ||
"INSTA_UPDATE=1 cargo test ..." | ||
} else { | ||
"buck test ... -- --env INSTA_UPDATE=1" | ||
}; | ||
println!( | ||
"{:=^80}\n", | ||
format!(" Run `{}` to update snapshots ", command) | ||
); | ||
let file_name = std::path::Path::new(file) | ||
.file_name() | ||
.and_then(|p| p.to_str()) | ||
.unwrap(); | ||
insta::_macro_support::assert_snapshot( | ||
insta::_macro_support::AutoName.into(), | ||
snapshot, | ||
"unused", | ||
// buck builds have a _unittest module suffix which cargo doesn't | ||
// this makes the snapshot location consistent on both | ||
&module.replacen("_unittest", "", 1), | ||
file_name, | ||
line, | ||
expr, | ||
) | ||
.unwrap(); | ||
} | ||
|
||
/// Assert that the serde json representation of given expression matches the snapshot | ||
/// stored on disk. | ||
#[macro_export] | ||
macro_rules! assert_json { | ||
($value: expr) => {{ | ||
$crate::setup(); | ||
|
||
$crate::run( | ||
option_env!("CARGO_MANIFEST_DIR").is_some(), | ||
&serde_json::to_string(&$value).unwrap(), | ||
file!(), | ||
module_path!(), | ||
line!(), | ||
stringify!($value), | ||
); | ||
}}; | ||
} |