Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exp-to-insta][9/n] Migrate move-package tests #21080

Merged
merged 11 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
85 changes: 46 additions & 39 deletions external-crates/move/crates/move-command-line-common/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

use std::path::Path;

use crate::env::read_bool_env_var;

/// Extension for raw output files
Expand Down Expand Up @@ -65,28 +67,37 @@ pub fn format_diff(expected: impl AsRef<str>, actual: impl AsRef<str>) -> String
}

/// See `insta_assert!` for documentation.
pub struct InstaOptions<Info: serde::Serialize, Suffix: Into<String>> {
pub info: Option<Info>,
pub suffix: Option<Suffix>,
pub struct InstaOptions {
info_set: bool,
suffix_set: bool,
settings: insta::Settings,
}

impl<Info: serde::Serialize, Suffix: Into<String>> InstaOptions<Info, Suffix> {
impl InstaOptions {
/// See `insta_assert!` for documentation.
pub fn new() -> Self {
Self {
info: None,
suffix: None,
info_set: false,
suffix_set: false,
settings: insta::Settings::clone_current(),
}
}
}

impl InstaOptions<(), String> {
/// See `insta_assert!` for documentation.
pub fn none() -> Self {
Self {
info: None,
suffix: None,
}
pub fn info<Info: serde::Serialize>(&mut self, info: Info) {
assert!(!self.info_set);
self.settings.set_info(&info);
self.info_set = true;
}

pub fn suffix<Suffix: Into<String>>(&mut self, suffix: Suffix) {
assert!(!self.suffix_set);
self.settings.set_snapshot_suffix(suffix);
self.suffix_set = true;
}

#[doc(hidden)]
pub fn into_settings(self) -> insta::Settings {
self.settings
}
}

Expand Down Expand Up @@ -144,45 +155,26 @@ macro_rules! insta_assert {
} => {{
let name: String = $name.into();
let i: &std::path::Path = $input.as_ref();
let i = i.canonicalize().unwrap();
let c = $contents;
let $crate::testing::InstaOptions { info, suffix } = $options;
let mut settings = $crate::testing::insta::Settings::clone_current();
let output = settings.set_snapshot_path( i.canonicalize().unwrap().parent().unwrap());
if let Some(info) = info {
settings.set_info(&info);
}
if let Some(suffix) = suffix {
settings.set_snapshot_suffix(suffix);
}
let mut settings = $options.into_settings();
settings.set_snapshot_path(i.parent().unwrap());
settings.set_prepend_module_to_snapshot(false);
settings.set_omit_expression(true);
settings.bind(|| {
$crate::testing::insta::assert_snapshot!(name, c);
});
}};
{
name: $name:expr,
input_path: $input:expr,
contents: $contents:expr
$(,)?
} => {{
insta_assert! {
name: $name,
input_path: $input,
contents: $contents,
options: $crate::testing::InstaOptions::none(),
}
}};
{
name: $name:expr,
input_path: $input:expr,
contents: $contents:expr,
$($k:ident: $v:expr),+$(,)?
$($k:ident: $v:expr),*$(,)?
} => {{
let mut opts = $crate::testing::InstaOptions::new();
$(
opts.$k = Some($v);
)+
opts.$k($v);
)*
insta_assert! {
name: $name,
input_path: $input,
Expand All @@ -192,3 +184,18 @@ macro_rules! insta_assert {
}};
}
pub use insta_assert;

pub fn read_insta_snapshot(path: impl AsRef<Path>) -> anyhow::Result<String> {
let path = path.as_ref();
match insta::Snapshot::from_file(path)
.map_err(|_| anyhow::anyhow!("Failed to load snapshot: {}", path.display()))?
.contents()
{
insta::internals::SnapshotContents::Text(text_snapshot_contents) => {
Ok(format!("{text_snapshot_contents}"))
}
insta::internals::SnapshotContents::Binary(_) => {
anyhow::bail!("Unexpected binary snapshot for path: {}", path.display())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,10 @@ pub fn run_test(path: &Path) -> datatest_stable::Result<()> {
}

let mut options = InstaOptions::new();
options.info = Some(test_info);
options.suffix = suffix;
options.info(test_info);
if let Some(suffix) = suffix {
options.suffix(suffix);
}
insta_assert! {
name: test_name,
input_path: move_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use std::{
collections::{BTreeMap, BTreeSet},
fs::{self, File},
io::Write,
path::PathBuf,
path::{Path, PathBuf},
};

use move_command_line_common::testing::read_insta_snapshot;
use move_package::{
lock_file::LockFile,
resolution::dependency_graph::{
Expand All @@ -28,6 +29,10 @@ macro_rules! assert_error_contains {
};
}

fn snapshot_path(pkg: &Path, kind: &str) -> PathBuf {
pkg.join(format!("Move@{kind}.snap"))
}

#[test]
fn no_dep_graph() {
let pkg = no_dep_test_package();
Expand Down Expand Up @@ -60,12 +65,13 @@ fn no_dep_graph() {
fn no_dep_graph_from_lock() {
let pkg = no_dep_test_package();

let snapshot = pkg.join("Move.locked");
let snapshot = snapshot_path(&pkg, "locked");
let contents = read_insta_snapshot(snapshot).unwrap();
let graph = DependencyGraph::read_from_lock(
pkg,
Symbol::from("Root"),
Symbol::from("Root"),
&mut File::open(snapshot).expect("Opening snapshot"),
&mut contents.as_bytes(),
None,
)
.expect("Reading DependencyGraph");
Expand All @@ -83,14 +89,15 @@ fn lock_file_roundtrip() {
let tmp = tempfile::tempdir().unwrap();
let pkg = one_dep_test_package();

let snapshot = pkg.join("Move.locked");
let snapshot = snapshot_path(&pkg, "locked");
let contents = read_insta_snapshot(snapshot).unwrap();
let commit = tmp.path().join("Move.lock");

let graph = DependencyGraph::read_from_lock(
pkg,
Symbol::from("Root"),
Symbol::from("Root"),
&mut File::open(&snapshot).expect("Opening snapshot"),
&mut contents.as_bytes(),
None,
)
.expect("Reading DependencyGraph");
Expand All @@ -101,11 +108,11 @@ fn lock_file_roundtrip() {

lock.commit(&commit).expect("Committing lock file");

let expect = fs::read_to_string(&snapshot).expect("Reading snapshot");
let actual = fs::read_to_string(commit).expect("Reading committed lock");

assert_eq!(
expect, actual,
contents,
actual.trim(),
"LockFile -> DependencyGraph -> LockFile roundtrip"
);
}
Expand Down Expand Up @@ -182,13 +189,14 @@ fn always_deps() {
#[test]
fn always_deps_from_lock() {
let pkg = dev_dep_test_package();
let snapshot = pkg.join("Move.locked");
let snapshot = snapshot_path(&pkg, "locked");
let contents = read_insta_snapshot(snapshot).unwrap();

let graph = DependencyGraph::read_from_lock(
pkg,
Symbol::from("Root"),
Symbol::from("Root"),
&mut File::open(snapshot).expect("Opening snapshot"),
&mut contents.as_bytes(),
None,
)
.expect("Creating DependencyGraph");
Expand Down
Loading
Loading