Skip to content

Commit

Permalink
pull error rendering out of error type, snapshot tests, only return an
Browse files Browse the repository at this point in the history
error don't print, use named params in fomrat, remove mut from finish()
cache files, capitalization show be lower
  • Loading branch information
jordanjennings-mysten committed Oct 15, 2024
1 parent 90b5cd2 commit 6e9aaa9
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 234 deletions.
9 changes: 1 addition & 8 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,14 +929,7 @@ impl SuiClientCommands {
upgrade_package,
) = upgrade_result?;

check_compatibility(
&client,
package_id,
&compiled_modules,
upgrade_package,
protocol_config,
)
.await?;
check_compatibility(&client, package_id, upgrade_package, protocol_config).await?;

let tx_kind = client
.transaction_builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,14 @@ module upgrades::upgrades {
public struct StructToBeRemoved {
b: u64
}

public enum EnumToBeRemoved {
A,
B
}

public fun fun_to_be_removed(): u64 {
0
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#[allow(unused_field)]
module upgrades::upgrades {
// struct missing
// removed declarations
// public struct StructToBeRemoved {}
// public enum EnumToBeRemoved {}
// public fun fun_to_be_removed() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/sui/src/unit_tests/upgrade_compatibility_tests.rs
expression: normalize_path(err.to_string())
---
Upgrade compatibility check failed:
error: struct is missing
┌─ crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7module upgrades::upgrades {
^^^^^^^^ Module 'upgrades' expected struct 'StructToBeRemoved', but found none
= The struct is missing in the new module, add the previously defined: 'StructToBeRemoved'

error: enum is missing
┌─ crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7 │ module upgrades::upgrades {
│ ^^^^^^^^ Module 'upgrades' expected enum 'EnumToBeRemoved', but found none
= The enum is missing in the new module, add the previously defined: 'EnumToBeRemoved'

error: public function is missing
┌─ crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7 │ module upgrades::upgrades {
│ ^^^^^^^^ Module 'upgrades' expected public function 'fun_to_be_removed', but found none
= The public function is missing in the new module, add the previously defined: 'fun_to_be_removed'


Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,27 @@
source: crates/sui/src/unit_tests/upgrade_compatibility_tests.rs
expression: err.to_string()
---
Upgrade compatibility check failed with the following errors:
- StructMissing { name: Identifier("StructToBeRemoved"), old_struct: Struct { abilities: [], type_parameters: [], fields: [Field { name: Identifier("b"), type_: U64 }] } }
Upgrade compatibility check failed:
error: struct is missing
┌─ /Users/jordanjennings/code/sui/crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7module upgrades::upgrades {
^^^^^^^^ Module 'upgrades' expected struct 'StructToBeRemoved', but found none
= The struct is missing in the new module, add the previously defined: 'StructToBeRemoved'

error: enum is missing
┌─ /Users/jordanjennings/code/sui/crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7 │ module upgrades::upgrades {
│ ^^^^^^^^ Module 'upgrades' expected enum 'EnumToBeRemoved', but found none
= The enum is missing in the new module, add the previously defined: 'EnumToBeRemoved'

error: public function is missing
┌─ /Users/jordanjennings/code/sui/crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
7 │ module upgrades::upgrades {
│ ^^^^^^^^ Module 'upgrades' expected public function 'fun_to_be_removed', but found none
= The public function is missing in the new module, add the previously defined: 'fun_to_be_removed'
51 changes: 32 additions & 19 deletions crates/sui/src/unit_tests/upgrade_compatibility_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,56 @@ use insta::assert_snapshot;
use move_binary_format::CompiledModule;
use std::path::PathBuf;
use sui_move_build::BuildConfig;
use sui_move_build::CompiledPackage;

#[test]
#[should_panic]
fn test_all_fail() {
let (pkg_v1, pkg_v2) = get_packages("all");
let (mods_v1, pkg_v2) = get_packages("all");

let result = compare_packages(pkg_v1, pkg_v2);
assert!(result.is_err());
let err = result.unwrap_err();

assert_snapshot!(err.to_string());
// panics: Not all errors are implemented yet
compare_packages(mods_v1, pkg_v2, false).unwrap();
}

#[test]
fn test_struct_missing() {
let (pkg_v1, pkg_v2) = get_packages("struct_missing");
let result = compare_packages(pkg_v1, pkg_v2);
fn test_declarations_missing() {
let (pkg_v1, pkg_v2) = get_packages("declarations_missing");
let result = compare_packages(pkg_v1, pkg_v2, false);

// find lines that have ┌─ /Users/jordanjennings/code/sui/crates/sui/src/unit_tests/fixtures/upgrade_errors/declarations_missing_v2/sources/UpgradeErrors.move:7:18
// remove the path before /sui/
let result = result.map_err(|err| {
let err = err.to_string();
let err = err.replace("/Users/jordanjennings/code/sui/", "");
err
});


assert!(result.is_err());
let err = result.unwrap_err();
assert_snapshot!(err.to_string());
assert_snapshot!(normalize_path(err.to_string()));
}

#[test]
fn test_friend_link_ok() {
let (pkg_v1, pkg_v2) = get_packages("friend_linking");
// upgrade compatibility ignores friend linking
assert!(compare_packages(pkg_v1, pkg_v2).is_ok());
assert!(compare_packages(pkg_v1, pkg_v2, false).is_ok());
}

#[test]
fn test_entry_linking_ok() {
let (pkg_v1, pkg_v2) = get_packages("entry_linking");
// upgrade compatibility ignores entry linking
assert!(compare_packages(pkg_v1, pkg_v2).is_ok());
assert!(compare_packages(pkg_v1, pkg_v2, false).is_ok());
}

fn get_packages(name: &str) -> (Vec<CompiledModule>, Vec<CompiledModule>) {
fn get_packages(name: &str) -> (Vec<CompiledModule>, CompiledPackage) {
let mut path: PathBuf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("src/unit_tests/fixtures/upgrade_errors/");
path.push(format!("{}_v1", name));

let pkg_v1 = BuildConfig::new_for_testing()
let mods_v1 = BuildConfig::new_for_testing()
.build(&path)
.unwrap()
.into_modules();
Expand All @@ -56,10 +64,15 @@ fn get_packages(name: &str) -> (Vec<CompiledModule>, Vec<CompiledModule>) {
path.push("src/unit_tests/fixtures/upgrade_errors/");
path.push(format!("{}_v2", name));

let pkg_v2 = BuildConfig::new_for_testing()
.build(&path)
.unwrap()
.into_modules();
let pkg_v2 = BuildConfig::new_for_testing().build(&path).unwrap();

(pkg_v1, pkg_v2)
(mods_v1, pkg_v2)
}

/// Snapshots will differ on each machine, normalize to prevent test failures
fn normalize_path(err_string: String) -> String {
// match the part of the path that is before /sui/ could be any home directory
let re = regex::Regex::new(r"(^ ┌─ /[-a-zA-Z0-9_/ .+()]+/sui/)").unwrap();
let path = re.replace(&err_string, " ┌─ /sui/");
path.to_string()
}
Loading

0 comments on commit 6e9aaa9

Please sign in to comment.