Skip to content

Commit

Permalink
fix v2 for vm integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahxephon89 committed Jan 13, 2025
1 parent fd726b2 commit e2a7d60
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions devtools/aptos-cargo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ const RELEVANT_FILE_PATHS_FOR_COMPILER_V2: [&str; 5] = [
"aptos-move/move-examples",
"third_party/move",
];
const NO_MVC_BLOCK_V1_PACKAGES: [&str; 11] = [
const NO_MVC_BLOCK_V1_PACKAGES: [&str; 10] = [
"e2e-move-tests", // no block v1 because the meta data test requires using v1
"move-prover",
"move-prover-bytecode-pipeline",
"move-compiler",
"move-compiler-transactional-tests",
"move-compiler-v2-transactional-tests",
"move-to-yul",
"move-vm-integration-tests",
"move-model",
"move-stackless-bytecode-test-utils",
"move-stackless-bytecode",
Expand Down
3 changes: 3 additions & 0 deletions third_party/move/move-vm/integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ edition = "2021"
[dependencies]
anyhow = { workspace = true }
bytes = { workspace = true }
codespan-reporting = { workspace = true }
memory-stats = { workspace = true }
move-binary-format = { workspace = true, features = ["testing"] }
move-bytecode-verifier = { workspace = true }
move-compiler = { workspace = true }
move-compiler-v2 = { workspace = true }
move-core-types = { workspace = true }
move-model = { workspace = true }
move-stdlib = { path = "../../move-stdlib" }
move-vm-runtime = { workspace = true, features = ["testing"] }
move-vm-test-utils = { workspace = true }
Expand Down
96 changes: 49 additions & 47 deletions third_party/move/move-vm/integration-tests/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,69 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Result};
use codespan_reporting::term::termcolor::Buffer;
use move_binary_format::file_format::{CompiledModule, CompiledScript};
use move_compiler::{
compiled_unit::AnnotatedCompiledUnit,
shared::{known_attributes::KnownAttribute, Flags},
Compiler as MoveCompiler,
shared::{known_attributes::KnownAttribute, NumericalAddress},
};
use move_model::metadata::LanguageVersion;
use std::{
collections::BTreeMap,
fs::File,
io::Write,
path::{Path, PathBuf},
};
use std::{fs::File, io::Write, path::Path};
use tempfile::tempdir;

pub fn compile_units(s: &str) -> Result<Vec<AnnotatedCompiledUnit>> {
fn compile_source_unit_v2(
s: &str,
file_option: Option<PathBuf>,
named_address_mapping: BTreeMap<String, NumericalAddress>,
deps: Vec<String>,
) -> Result<Vec<AnnotatedCompiledUnit>> {
let dir = tempdir()?;

let file_path = dir.path().join("modules.move");
{
let mut file = File::create(&file_path)?;
let file_path = if let Some(file) = file_option {
file
} else {
let path = dir.path().join("modules.move");
let mut file = File::create(&path)?;
writeln!(file, "{}", s)?;
}

let (_, units) = MoveCompiler::from_files(
vec![file_path.to_str().unwrap().to_string()],
vec![],
move_stdlib::move_stdlib_named_addresses(),
Flags::empty().set_skip_attribute_checks(false),
KnownAttribute::get_all_attribute_names(),
)
.build_and_report()?;

path
};
let options = move_compiler_v2::Options {
sources: vec![file_path.to_str().unwrap().to_string()],
dependencies: deps,
named_address_mapping: named_address_mapping
.into_iter()
.map(|(alias, addr)| format!("{}={}", alias, addr))
.collect(),
known_attributes: KnownAttribute::get_all_attribute_names().clone(),
language_version: Some(LanguageVersion::latest_stable()),
..move_compiler_v2::Options::default()
};
let mut error_writer = Buffer::no_color();
let result = {
let mut emitter = options.error_emitter(&mut error_writer);
move_compiler_v2::run_move_compiler(emitter.as_mut(), options)
};
let error_str = String::from_utf8_lossy(&error_writer.into_inner()).to_string();
let (_, units) = result.map_err(|_| anyhow::anyhow!("compilation errors:\n {}", error_str))?;
dir.close()?;

Ok(units)
}

pub fn compile_units_with_stdlib(s: &str) -> Result<Vec<AnnotatedCompiledUnit>> {
let dir = tempdir()?;

let file_path = dir.path().join("modules.move");
{
let mut file = File::create(&file_path)?;
writeln!(file, "{}", s)?;
}
pub fn compile_units(s: &str) -> Result<Vec<AnnotatedCompiledUnit>> {
compile_source_unit_v2(s, None, move_stdlib::move_stdlib_named_addresses(), vec![])
}

let (_, units) = MoveCompiler::from_files(
vec![file_path.to_str().unwrap().to_string()],
move_stdlib::move_stdlib_files(),
pub fn compile_units_with_stdlib(s: &str) -> Result<Vec<AnnotatedCompiledUnit>> {
compile_source_unit_v2(
s,
None,
move_stdlib::move_stdlib_named_addresses(),
Flags::empty().set_skip_attribute_checks(false),
KnownAttribute::get_all_attribute_names(),
move_stdlib::move_stdlib_files(),
)
.build_and_report()?;

dir.close()?;

Ok(units)
}

fn expect_modules(
Expand All @@ -68,15 +78,7 @@ fn expect_modules(
}

pub fn compile_modules_in_file(path: &Path) -> Result<Vec<CompiledModule>> {
let (_, units) = MoveCompiler::from_files(
vec![path.to_str().unwrap().to_string()],
vec![],
std::collections::BTreeMap::<String, _>::new(),
Flags::empty().set_skip_attribute_checks(false),
KnownAttribute::get_all_attribute_names(),
)
.build_and_report()?;

let units = compile_source_unit_v2("", Some(path.to_path_buf()), BTreeMap::new(), vec![])?;
expect_modules(units).collect()
}

Expand Down

0 comments on commit e2a7d60

Please sign in to comment.