Skip to content

Commit

Permalink
cln_plugin : Test default values for ConfigOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikDeSmedt committed Feb 6, 2024
1 parent 320dd93 commit 64ddef2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
31 changes: 20 additions & 11 deletions plugins/examples/cln-plugin-startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@
//! plugins using the Rust API against Core Lightning.
#[macro_use]
extern crate serde_json;
use cln_plugin::{messages, options, Builder, Error, Plugin};
use cln_plugin::options::{DefaultIntegerConfigOption, IntegerConfigOption};
use cln_plugin::{messages, Builder, Error, Plugin};
use tokio;

const TEST_NOTIF_TAG: &str = "test_custom_notification";

const TEST_OPTION: DefaultIntegerConfigOption = DefaultIntegerConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
);

const TEST_OPTION_NO_DEFAULT: IntegerConfigOption =
IntegerConfigOption::new_i64_no_default("opt-option", "An option without a default");

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let state = ();

if let Some(plugin) = Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new_i64_with_default(
"test-option",
42,
"a test-option with default 42",
))
.option(options::ConfigOption::new_i64_no_default(
"opt-option",
"An optional option",
))
.option(TEST_OPTION)
.option(TEST_OPTION_NO_DEFAULT)
.rpcmethod("testmethod", "This is a test", testmethod)
.rpcmethod(
"testoptions",
Expand All @@ -46,8 +49,14 @@ async fn main() -> Result<(), anyhow::Error> {
}

async fn testoptions(p: Plugin<()>, _v: serde_json::Value) -> Result<serde_json::Value, Error> {
let test_option = p.option(&TEST_OPTION).expect("Option was configured");
let test_option_no_default = p
.option(&TEST_OPTION_NO_DEFAULT)
.expect("option was configured");

Ok(json!({
"opt-option": format!("{:?}", p.option_str("opt-option").unwrap())
"test-option": test_option,
"opt-option" : test_option_no_default
}))
}

Expand Down
5 changes: 1 addition & 4 deletions plugins/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ pub mod config_type {
pub struct Flag;
}


/// Config values are represented as an i64. No default is used
pub type IntegerConfigOption<'a> = ConfigOption<'a, config_type::Integer>;
/// Config values are represented as a String. No default is used.
Expand All @@ -160,7 +159,6 @@ pub type DefaultBooleanConfigOption<'a> = ConfigOption<'a, config_type::DefaultB
/// Config value is represented as a flag
pub type FlagConfigOption<'a> = ConfigOption<'a, config_type::Flag>;


pub trait ConfigOptionType {
type OutputValue;
type DefaultValue;
Expand Down Expand Up @@ -626,8 +624,7 @@ mod test {
// The main goal of this test is to test compilation

// Initiate every type as a const
const _: FlagConfigOption =
ConfigOption::new_flag("flag-option", "A flag option");
const _: FlagConfigOption = ConfigOption::new_flag("flag-option", "A flag option");
const _: DefaultBooleanConfigOption =
ConfigOption::new_bool_with_default("bool-option", false, "A boolean option");
const _: BooleanConfigOption =
Expand Down
10 changes: 6 additions & 4 deletions tests/test_cln_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,20 @@ def test_plugin_start(node_factory):
l1.daemon.wait_for_log(r'Got a connect notification')


def test_plugin_optional_opts(node_factory):
def test_plugin_options_handle_defaults(node_factory):
"""Start a minimal plugin and ensure it is well-behaved
"""
bin_path = Path.cwd() / "target" / RUST_PROFILE / "examples" / "cln-plugin-startup"
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'opt-option': 31337})
l1 = node_factory.get_node(options={"plugin": str(bin_path), 'opt-option': 31337, "test-option" : 31338})
opts = l1.rpc.testoptions()
print(opts)
assert opts["opt-option"] == 31337
assert opts["test-option"] == 31338

# Do not set any value, should be None now
l1 = node_factory.get_node(options={"plugin": str(bin_path)})
opts = l1.rpc.testoptions()
print(opts)
assert opts["opt-option"] is None, "opt-option has no default"
assert opts["test-option"] == 42, "test-option has a default of 42"


def test_grpc_connect(node_factory):
Expand Down

0 comments on commit 64ddef2

Please sign in to comment.