diff --git a/crates/sui-move/src/new.rs b/crates/sui-move/src/new.rs index f9ce75361b1bd..ac8d13e4daa73 100644 --- a/crates/sui-move/src/new.rs +++ b/crates/sui-move/src/new.rs @@ -3,7 +3,12 @@ use clap::Parser; use move_cli::base::new; -use std::path::PathBuf; +use move_package::source_package::layout::SourcePackageLayout; +use std::{ + fs::create_dir_all, + io::Write, + path::{Path, PathBuf}, +}; const SUI_PKG_NAME: &str = "Sui"; @@ -20,8 +25,62 @@ pub struct New { impl New { pub fn execute(self, path: Option) -> anyhow::Result<()> { let name = &self.new.name.to_lowercase(); - self.new - .execute(path, [(SUI_PKG_NAME, SUI_PKG_PATH)], [(name, "0x0")], "")?; + let p = match &path { + Some(path) => path, + None => Path::new(&name), + }; + + self.new.execute( + path.clone(), + [(SUI_PKG_NAME, SUI_PKG_PATH)], + [(name, "0x0")], + "", + )?; + + let mut w = std::fs::File::create( + p.join(SourcePackageLayout::Sources.path()) + .join(format!("{name}.move")), + )?; + writeln!( + w, + r#"/* +/// Module: {name} +module {name}::{name} {{ + +}} +*/"#, + name = name + )?; + + create_dir_all(p.join(SourcePackageLayout::Tests.path()))?; + let mut w = std::fs::File::create( + p.join(SourcePackageLayout::Tests.path()) + .join(format!("{name}_tests.move")), + )?; + writeln!( + w, + r#"/* +#[test_only] +module {name}::{name}_tests {{ + // uncomment this line to import the module + // use {name}::{name}; + + const ENotImplemented: u64 = 0; + + #[test] + fun test_{name}() {{ + // pass + }} + + #[test, expected_failure(abort_code = {name}::{name}_tests::ENotImplemented)] + fun test_{name}_fail() {{ + abort ENotImplemented + }} +}} +*/"#, + name = name + )?; + Ok(()) } }