diff --git a/.gitignore b/.gitignore index 0251ae90..7f623520 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Build files /target - +/integration-tests/docker-build-template/target # Code Editor related files .idea .vscode diff --git a/Cargo.lock b/Cargo.lock index 23578077..0f051d70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -868,8 +868,10 @@ dependencies = [ "cargo-near 0.6.2", "color-eyre", "const_format", + "env_logger 0.11.3", "function_name", "git2", + "log", "minifier", "near-abi", "near-workspaces", diff --git a/cargo-near/src/commands/build_command/docker.rs b/cargo-near/src/commands/build_command/docker.rs index 651f74b9..1849ff0f 100644 --- a/cargo-near/src/commands/build_command/docker.rs +++ b/cargo-near/src/commands/build_command/docker.rs @@ -129,7 +129,10 @@ impl super::BuildCommand { &container_paths.crate_path, ]; - if std::env::var(SERVER_DISABLE_INTERACTIVE).is_err() { + log::debug!("input device is a tty: {}", atty::is(atty::Stream::Stdin)); + if atty::is(atty::Stream::Stdin) + && std::env::var(SERVER_DISABLE_INTERACTIVE).is_err() + { docker_args.push("-it"); } diff --git a/integration-tests/Cargo.toml b/integration-tests/Cargo.toml index 8ee5cbbd..a109829f 100644 --- a/integration-tests/Cargo.toml +++ b/integration-tests/Cargo.toml @@ -25,3 +25,5 @@ tokio = "1.0" quote = "1.0" near-workspaces = "0.10.1" zstd = "0.13" +env_logger = "0.11" +log = "0.4" diff --git a/integration-tests/docker-build-template/Cargo.lock b/integration-tests/docker-build-template/Cargo.lock new file mode 100644 index 00000000..5bb523c3 --- /dev/null +++ b/integration-tests/docker-build-template/Cargo.lock @@ -0,0 +1,463 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "Inflector" +version = "0.11.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe438c63458706e03479442743baae6c88256498e6431708f6dfc520a26515d3" + +[[package]] +name = "base64" +version = "0.21.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" + +[[package]] +name = "borsh" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn", + "syn_derive", +] + +[[package]] +name = "bs58" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "cargo-near-docker-build-integration-test" +version = "0.1.0" +dependencies = [ + "near-sdk", +] + +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "darling" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83b2eb4d90d12bdda5ed17de686c2acb4c57914f8f921b8da7e112b5a36f3fe1" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622687fe0bac72a04e5599029151f5796111b90f1baaa9b544d807a5e31cd120" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "733cabb43482b1a1b53eee8583c2b9e8684d592215ea83efd305dd31bc2f0178" +dependencies = [ + "darling_core", + "quote", + "syn", +] + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "memory_units" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3" + +[[package]] +name = "near-account-id" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35cbb989542587b47205e608324ddd391f0cee1c22b4b64ae49f458334b95907" +dependencies = [ + "borsh", + "serde", +] + +[[package]] +name = "near-gas" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14e75c875026229902d065e4435804497337b631ec69ba746b102954273e9ad1" +dependencies = [ + "borsh", + "serde", +] + +[[package]] +name = "near-sdk" +version = "5.1.0" +source = "git+https://github.com/near/near-sdk-rs.git?rev=23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528#23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528" +dependencies = [ + "base64", + "borsh", + "bs58", + "near-account-id", + "near-gas", + "near-sdk-macros", + "near-sys", + "near-token", + "once_cell", + "serde", + "serde_json", + "wee_alloc", +] + +[[package]] +name = "near-sdk-macros" +version = "5.1.0" +source = "git+https://github.com/near/near-sdk-rs.git?rev=23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528#23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528" +dependencies = [ + "Inflector", + "darling", + "proc-macro2", + "quote", + "serde", + "serde_json", + "strum", + "strum_macros", + "syn", +] + +[[package]] +name = "near-sys" +version = "0.2.1" +source = "git+https://github.com/near/near-sdk-rs.git?rev=23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528#23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528" + +[[package]] +name = "near-token" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b68f3f8a2409f72b43efdbeff8e820b81e70824c49fee8572979d789d1683fb" +dependencies = [ + "borsh", + "serde", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro-crate" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" +dependencies = [ + "toml_edit", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.203" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.120" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "strum" +version = "0.26.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06" + +[[package]] +name = "strum_macros" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + +[[package]] +name = "syn" +version = "2.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1329189c02ff984e9736652b1631330da25eaa6bc639089ed4915d25446cbe7b" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinyvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c55115c6fbe2d2bef26eb09ad74bde02d8255476fc0c7b515ef09fbb35742d82" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "toml_datetime" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" + +[[package]] +name = "toml_edit" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wee_alloc" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e" +dependencies = [ + "cfg-if", + "libc", + "memory_units", + "winapi", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] diff --git a/integration-tests/docker-build-template/Cargo.toml b/integration-tests/docker-build-template/Cargo.toml new file mode 100644 index 00000000..893631be --- /dev/null +++ b/integration-tests/docker-build-template/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "cargo-near-docker-build-integration-test" +description = "cargo-near-docker-build-integration-test" +version = "0.1.0" +edition = "2021" +# TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project. +# NEP-0330 is automatically implemented for all contracts built with https://github.com/near/cargo-near. +# Link to the repository will be available via `contract_source_metadata` view-function. +#repository = "https://github.com/xxx/xxx" + +[lib] +crate-type = ["cdylib", "rlib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +near-sdk = { version = "5.1.0", git= "https://github.com/near/near-sdk-rs.git", rev = "23f0545e5d2ca9f0fe2fa12ec6b9b5f00de4b528"} + +[package.metadata.near.reproducible_build] +image = "dj8yfo/sourcescan:0.x.x-dev-cargo-near-finalization" +image_digest = "sha256:f86a095cb3daed21d0b5f86a4b5d9da0c0e26835f85c42678e1d460c9caa2c12" +container_build_command = ["cargo", "near", "build"] +source_code_git_url = "https://github.com/xxx/xxx.git" + +[profile.release] +codegen-units = 1 +# Tell `rustc` to optimize for small code size. +opt-level = "z" +lto = true +debug = false +panic = "abort" +# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 +overflow-checks = true + +[workspace] diff --git a/integration-tests/docker-build-template/src/lib.rs b/integration-tests/docker-build-template/src/lib.rs new file mode 100644 index 00000000..af8a6b37 --- /dev/null +++ b/integration-tests/docker-build-template/src/lib.rs @@ -0,0 +1,55 @@ +// Find all our documentation at https://docs.near.org +use near_sdk::{log, near}; + +// Define the contract structure +#[near(contract_state)] +pub struct Contract { + greeting: String, +} + +// Define the default, which automatically initializes the contract +impl Default for Contract { + fn default() -> Self { + Self { + greeting: "Hello".to_string(), + } + } +} + +// Implement the contract structure +#[near] +impl Contract { + // Public method - returns the greeting saved, defaulting to DEFAULT_GREETING + pub fn get_greeting(&self) -> String { + self.greeting.clone() + } + + // Public method - accepts a greeting, such as "howdy", and records it + pub fn set_greeting(&mut self, greeting: String) { + log!("Saving greeting: {greeting}"); + self.greeting = greeting; + } +} + +/* + * The rest of this file holds the inline tests for the code above + * Learn more about Rust tests: https://doc.rust-lang.org/book/ch11-01-writing-tests.html + */ +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn get_default_greeting() { + let contract = Contract::default(); + // this test did not call set_greeting so should return the default "Hello" greeting + assert_eq!(contract.get_greeting(), "Hello"); + } + + #[test] + fn set_then_get_greeting() { + let mut contract = Contract::default(); + contract.set_greeting("howdy".to_string()); + assert_eq!(contract.get_greeting(), "howdy"); + } +} diff --git a/integration-tests/tests/abi/borsh_schema.rs b/integration-tests/tests/abi/borsh_schema.rs index 1faa3642..5c8522da 100644 --- a/integration-tests/tests/abi/borsh_schema.rs +++ b/integration-tests/tests/abi/borsh_schema.rs @@ -7,6 +7,7 @@ use std::collections::BTreeMap; #[test] #[named] fn test_borsh_schema_numeric_primitives_signed() -> cargo_near::CliResult { + let _e = env_logger::Builder::from_default_env().try_init(); let abi_root = generate_abi_fn! { pub fn foo( &self, diff --git a/integration-tests/tests/docker.rs b/integration-tests/tests/docker.rs new file mode 100644 index 00000000..95c4b276 --- /dev/null +++ b/integration-tests/tests/docker.rs @@ -0,0 +1,29 @@ +#[cfg(target_os = "linux")] +#[test] +fn test_docker_build() -> cargo_near::CliResult { + let _e = env_logger::Builder::from_default_env().try_init(); + let manifest_dir: camino::Utf8PathBuf = env!("CARGO_MANIFEST_DIR").into(); + + let cargo_near::CliOpts::Near(cli_args) = cargo_near::Opts::try_parse_from("cargo near build")?; + + let cargo_path_parent = manifest_dir.join("docker-build-template"); + let cargo_path = cargo_path_parent.join("Cargo.toml"); + match cli_args.cmd { + Some(cargo_near::commands::CliNearCommand::Abi(_cmd)) => { + unreachable!("another cmd is set by `cargo near build`"); + } + Some(cargo_near::commands::CliNearCommand::Build(cmd)) => { + let args = { + let mut args = cargo_near::commands::build_command::BuildCommand::from(cmd); + args.manifest_path = Some(cargo_path.into()); + args + }; + args.run(cargo_near::commands::build_command::BuildContext::Build)?; + } + _ => { + unreachable!("another cmd is set by `cargo near build`"); + } + } + + Ok(()) +} diff --git a/integration-tests/tests/regular_tests.rs b/integration-tests/tests/regular_tests.rs new file mode 100644 index 00000000..9a988669 --- /dev/null +++ b/integration-tests/tests/regular_tests.rs @@ -0,0 +1,4 @@ +mod abi; +mod build; +mod cargo; +mod util; diff --git a/integration-tests/tests/util.rs b/integration-tests/tests/util/mod.rs similarity index 100% rename from integration-tests/tests/util.rs rename to integration-tests/tests/util/mod.rs