sui_move/
new.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use clap::Parser;
5use indoc::formatdoc;
6use move_cli::base::new;
7use move_package_alt::package::layout::SourcePackageLayout;
8use std::{
9    fs::create_dir_all,
10    path::{Path, PathBuf},
11};
12
13#[derive(Parser)]
14#[group(id = "sui-move-new")]
15pub struct New {
16    #[clap(flatten)]
17    pub new: new::New,
18}
19
20impl New {
21    pub fn execute(self, path: Option<&Path>) -> anyhow::Result<()> {
22        let name = self.new.name_var()?;
23        self.new.execute(path)?;
24        std::fs::write(
25            self.new.source_file_path(&path)?,
26            formatdoc!(
27                r#"
28                /*
29                /// Module: {name}
30                module {name}::{name};
31                */
32
33                // For Move coding conventions, see
34                // https://docs.sui.io/concepts/sui-move-concepts/conventions
35
36                "#,
37            ),
38        )?;
39
40        std::fs::write(
41            self.test_file_path(&path)?,
42            formatdoc!(
43                r#"
44                /*
45                #[test_only]
46                module {name}::{name}_tests;
47                // uncomment this line to import the module
48                // use {name}::{name};
49
50                #[error(code = 0)]
51                const ENotImplemented: vector<u8> = b"Not Implemented";
52
53                #[test]
54                fun test_{name}() {{
55                    // pass
56                }}
57
58                #[test, expected_failure(abort_code = ::{name}::{name}_tests::ENotImplemented)]
59                fun test_{name}_fail() {{
60                    abort ENotImplemented
61                }}
62                */
63                "#,
64            ),
65        )?;
66
67        Ok(())
68    }
69
70    pub fn test_file_path(&self, path: &Option<&Path>) -> anyhow::Result<PathBuf> {
71        let dir = self
72            .new
73            .root_dir(path)?
74            .join(SourcePackageLayout::Tests.path());
75
76        create_dir_all(&dir)?;
77
78        Ok(dir.join(format!("{}_tests.move", self.new.name_var()?)))
79    }
80}