Skip to content

Commit

Permalink
[CLI] Adds initial package layout on sui move new (#16255)
Browse files Browse the repository at this point in the history
## Description 

Following the discussion on scaffolding the app:

- adds `sources/{name}.move`
- adds `tests/{name}_tests.move`

```sh
sources/
    donuts.move
tests/
    donuts_tests.move
Move.toml
```

File: sources/name.move; Commented out contents:
```move
/// Module: donuts
module donuts::donuts {

}
```

File: tests/name_tests.move; Commented out contents:
```move
#[test_only]
module donuts::donuts_tests {
    // uncomment this line to import the module
    // use donuts::donuts;

    const ENotImplemented: u64 = 0;

    #[test]
    fun test_donuts() {
        // pass
    }

    #[test, expected_failure(abort_code = donuts::donuts_tests::ENotImplemented)]
    fun test_donuts_fail() {
        abort ENotImplemented
    }
}
```

## Test Plan 

Tests pass!

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
damirka authored Mar 17, 2024
1 parent 8df080f commit 1cedbb8
Showing 1 changed file with 62 additions and 3 deletions.
65 changes: 62 additions & 3 deletions crates/sui-move/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -20,8 +25,62 @@ pub struct New {
impl New {
pub fn execute(self, path: Option<PathBuf>) -> 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(())
}
}

0 comments on commit 1cedbb8

Please sign in to comment.