Skip to content

Commit

Permalink
docs: add docs for the ganachecli module (#190)
Browse files Browse the repository at this point in the history
this adds docs for the ganachecli
Part of the #2 initative

---------

Co-authored-by: Artem Medvedev <[email protected]>
  • Loading branch information
CommanderStorm and DDtKey authored Aug 31, 2024
1 parent 480d5ed commit b09e026
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions src/trufflesuite_ganachecli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
use std::borrow::Cow;

use testcontainers::{core::WaitFor, Image};
use testcontainers::{
core::{ContainerPort, WaitFor},
Image,
};

const NAME: &str = "trufflesuite/ganache-cli";
const TAG: &str = "v6.1.3";

/// Port that the [`Ganache CLI`] container has internally.
/// Can be rebound externally via [`testcontainers::core::ImageExt::with_mapped_port`]
///
/// [Ganache CLI]: https://github.com/trufflesuite/ganache
pub const GANACHE_CLI_PORT: ContainerPort = ContainerPort::Tcp(8545);

/// # Module to work with the [`Ganache CLI`] inside of tests.
///
/// Starts an instance of Meilisearch.
/// This module is based on the official [`trufflesuite/ganache-cli` docker image] documented in the [documentation].
///
/// # Example
/// ```
/// use testcontainers_modules::{
/// testcontainers::runners::SyncRunner, trufflesuite_ganachecli,
/// trufflesuite_ganachecli::GANACHE_CLI_PORT,
/// };
///
/// let instance = trufflesuite_ganachecli::GanacheCli::default()
/// .start()
/// .unwrap();
/// let url = format!(
/// "http://{host_ip}:{host_port}",
/// host_ip = instance.get_host().unwrap(),
/// host_port = instance.get_host_port_ipv4(GANACHE_CLI_PORT).unwrap()
/// );
/// // do something with the started GanacheCli instance..
/// ```
///
/// [Ganache CLI]: https://github.com/trufflesuite/ganache
/// [documentation]: https://github.com/trufflesuite/ganache?tab=readme-ov-file#documentation
/// [`trufflesuite/ganache-cli` docker image]: https://hub.docker.com/r/trufflesuite/ganache-cli/
#[derive(Debug, Default, Clone)]
pub struct GanacheCli {
cmd: GanacheCliCmd,
}

/// Options to pass to the `ganache-cli` command
#[derive(Debug, Clone)]
pub struct GanacheCliCmd {
/// Specify the network id ganache-core will use to identify itself (defaults to the current time or the network id of the forked blockchain if configured)
pub network_id: u32,
/// Specify the number of accounts to generate at startup
pub number_of_accounts: u32,
/// Use a bip39 mnemonic phrase for generating a PRNG seed, which is in turn used for hierarchical deterministic (HD) account generation.
pub mnemonic: String,
}

Expand Down Expand Up @@ -57,6 +96,10 @@ impl Image for GanacheCli {
TAG
}

fn expose_ports(&self) -> &[ContainerPort] {
&[GANACHE_CLI_PORT]
}

fn ready_conditions(&self) -> Vec<WaitFor> {
vec![WaitFor::message_on_stdout("Listening on localhost:")]
}
Expand All @@ -70,14 +113,14 @@ impl Image for GanacheCli {
mod tests {
use testcontainers::runners::SyncRunner;

use crate::trufflesuite_ganachecli;
use super::*;

#[test]
fn trufflesuite_ganachecli_listaccounts() -> Result<(), Box<dyn std::error::Error + 'static>> {
let _ = pretty_env_logger::try_init();
let node = trufflesuite_ganachecli::GanacheCli::default().start()?;
let node = GanacheCli::default().start()?;
let host_ip = node.get_host()?;
let host_port = node.get_host_port_ipv4(8545)?;
let host_port = node.get_host_port_ipv4(GANACHE_CLI_PORT)?;

let response = reqwest::blocking::Client::new()
.post(format!("http://{host_ip}:{host_port}"))
Expand Down

0 comments on commit b09e026

Please sign in to comment.