Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

added http interface to hc run #846

Merged
merged 12 commits into from
Jan 15, 2019
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
### Added
- `hc run` now looks for the --interface flag or `HC_INTERFACE` env var if you want to specify the `http` interface [#846]((https://github.com/holochain/holochain-rust/pull/779)
### Removed

## [0.0.3] - 2019-01-09
Expand Down
4 changes: 2 additions & 2 deletions cmd/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ pub mod tests {
use super::*;
use tempfile::{Builder, TempDir};

const HOLOCHAIN_TEST_PREFIX: &str = "org.holochain.test";
const HOLOCHAIN_TEST_PREFIX: &str = "org_holochain_test";

fn gen_dir() -> TempDir {
pub fn gen_dir() -> TempDir {
Builder::new()
.prefix(HOLOCHAIN_TEST_PREFIX)
.tempdir()
Expand Down
11 changes: 1 addition & 10 deletions cmd/src/cli/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,18 +292,9 @@ fn unpack_recurse(mut obj: Object, to: &PathBuf) -> DefaultResult<()> {
// too slow!
#[cfg(feature = "broken-tests")]
mod tests {
use crate::cli::init::tests::gen_dir;
use assert_cmd::prelude::*;
use std::process::Command;
use tempfile::{Builder, TempDir};

const HOLOCHAIN_TEST_PREFIX: &str = "org.holochain.test";

fn gen_dir() -> TempDir {
Builder::new()
.prefix(HOLOCHAIN_TEST_PREFIX)
.tempdir()
.unwrap()
}

#[test]
fn package_and_unpack_isolated() {
Expand Down
59 changes: 55 additions & 4 deletions cmd/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ const INSTANCE_CONFIG_ID: &str = "test-instance";
const INTERFACE_CONFIG_ID: &str = "websocket-interface";

/// Starts a small container with the current application running
pub fn run(package: bool, port: u16, persist: bool, networked: bool) -> DefaultResult<()> {
pub fn run(
package: bool,
port: u16,
persist: bool,
networked: bool,
interface: String,
) -> DefaultResult<()> {
if package {
cli::package(true, Some(package::DEFAULT_BUNDLE_FILE_NAME.into()))?;
}
Expand Down Expand Up @@ -50,9 +56,18 @@ pub fn run(package: bool, port: u16, persist: bool, networked: bool) -> DefaultR
storage,
};

let interface_type = env::var("HC_INTERFACE").ok().unwrap_or_else(|| interface);
let driver = if interface_type == String::from("websocket") {
InterfaceDriver::Websocket { port }
} else if interface_type == String::from("http") {
InterfaceDriver::Http { port }
} else {
return Err(format_err!("unknown interface type: {}", interface_type));
};

let interface_config = InterfaceConfiguration {
id: INTERFACE_CONFIG_ID.into(),
driver: InterfaceDriver::Websocket { port },
driver,
admin: true,
instances: vec![InstanceReferenceConfiguration {
id: INSTANCE_CONFIG_ID.into(),
Expand Down Expand Up @@ -113,8 +128,8 @@ pub fn run(package: bool, port: u16, persist: bool, networked: bool) -> DefaultR
container.start_all_instances()?;

println!(
"Holochain development container started. Running websocket server on port {}",
port
"Holochain development container started. Running {} server on port {}",
interface_type, port
);
println!("Type 'exit' to stop the container and exit the program");

Expand All @@ -135,3 +150,39 @@ pub fn run(package: bool, port: u16, persist: bool, networked: bool) -> DefaultR

Ok(())
}

#[cfg(test)]
// flagged as broken for:
// 1. taking 60+ seconds
#[cfg(feature = "broken-tests")]
mod tests {
use crate::cli::init::{init, tests::gen_dir};
use assert_cmd::prelude::*;
use std::{env, process::Command};

#[test]
fn test_run() {
let temp_dir = gen_dir();
let temp_dir_path = temp_dir.path();
let temp_dir_path_buf = temp_dir_path.to_path_buf();

let mut run_cmd = Command::main_binary().unwrap();
let mut run2_cmd = Command::main_binary().unwrap();

let _ = init(&temp_dir_path_buf);

assert!(env::set_current_dir(&temp_dir_path).is_ok());

let output = run_cmd
.args(&["run", "--package"])
.output()
.expect("should run");
assert_eq!(format!("{:?}",output),"Output { status: ExitStatus(ExitStatus(256)), stdout: \"\\u{1b}[1;32mCreated\\u{1b}[0m bundle file at \\\"bundle.json\\\"\\nStarting instance \\\"test-instance\\\"...\\nHolochain development container started. Running websocket server on port 8888\\nType \\\'exit\\\' to stop the container and exit the program\\n\", stderr: \"Error: EOF\\n\" }");

let output = run2_cmd
.args(&["run", "--interface", "http"])
.output()
.expect("should run");
assert_eq!(format!("{:?}",output),"Output { status: ExitStatus(ExitStatus(256)), stdout: \"Starting instance \\\"test-instance\\\"...\\nHolochain development container started. Running http server on port 8888\\nType \\\'exit\\\' to stop the container and exit the program\\n\", stderr: \"Error: EOF\\n\" }");
}
}
8 changes: 4 additions & 4 deletions cmd/src/cli/scaffold/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn generate_cargo_toml(name: &str, contents: &str) -> DefaultResult<String> {

let authors_default = Value::from("[\"TODO\"]");
let edition_default = Value::from("\"TODO\"");
let version_default = Value::from("tag = 0.0.3");
let version_default = String::from("branch = \"develop\"");
let maybe_package = config.get("package");

let name = Value::from(name);
Expand All @@ -38,7 +38,7 @@ fn generate_cargo_toml(name: &str, contents: &str) -> DefaultResult<String> {
.and_then(|p| p.get("edition"))
.unwrap_or(&edition_default);

interpolate_cargo_template(&name, authors, edition, &version_default)
interpolate_cargo_template(&name, authors, edition, version_default)
}

/// Use the Cargo.toml.template file and interpolate values into the placeholders
Expand All @@ -47,14 +47,14 @@ fn interpolate_cargo_template(
name: &Value,
authors: &Value,
edition: &Value,
version: &Value,
version: String,
) -> DefaultResult<String> {
let template = include_str!("rust/Cargo.template.toml");
Ok(template
.replace("<<NAME>>", toml::to_string(name)?.as_str())
.replace("<<AUTHORS>>", toml::to_string(authors)?.as_str())
.replace("<<EDITION>>", toml::to_string(edition)?.as_str())
.replace("<<VERSION>>", toml::to_string(version)?.as_str()))
.replace("<<VERSION>>", &version))
}

impl RustScaffold {
Expand Down
62 changes: 19 additions & 43 deletions cmd/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,51 +62,34 @@ pub fn test(
}

#[cfg(test)]
#[cfg(feature = "broken-tests")]
pub mod tests {
use super::*;
use crate::cli::package;
use assert_cmd::prelude::*;
use std::{env, process::Command};
use tempfile::{Builder, TempDir};

#[cfg(feature = "broken-tests")]
const HOLOCHAIN_TEST_PREFIX: &str = "org_holochain_test";

#[cfg(feature = "broken-tests")]
pub fn gen_dir() -> TempDir {
Builder::new()
.prefix(HOLOCHAIN_TEST_PREFIX)
.tempdir()
.unwrap()
}
use crate::cli::init::{init, tests::gen_dir};
// use assert_cmd::prelude::*;
// use std::{env, process::Command};

#[test]
// flagged as broken for:
// 1. taking 60+ seconds
// 2. because `generate_cargo_toml` in cmd/src/scaffold/rust.rs sets the
// branch to master rather than develop and currently there's no way to
// branch to a fixed value rather than develop and currently there's no way to
// adjust that on the fly.
// 3. the call to generate my_zome function doesn't quite work
// 3. because holochain-nodejs version doesn't exist yet
#[cfg(feature = "broken-tests")]
fn test_command_basic_test() {
let temp_space = gen_dir();
let temp_dir_path = temp_space.path();
let temp_dir_path_buf = temp_space.path().to_path_buf();

// do init first, so there's a project
Command::main_binary()
.unwrap()
.args(&["init", temp_dir_path.to_str().unwrap()])
.assert()
.success();
let temp_dir = gen_dir();
let temp_dir_path = temp_dir.path();
let temp_dir_path_buf = temp_dir_path.to_path_buf();

let mut gen_cmd = Command::main_binary().unwrap();

let _ = init(&temp_dir_path_buf);

assert!(env::set_current_dir(&temp_dir_path).is_ok());

// do gen my_zome first, so there's a zome
Command::main_binary()
.unwrap()
.args(&["generate", "my_zome"])
gen_cmd
.args(&["generate", "zomes/my_zome"])
.assert()
.success();

Expand All @@ -127,19 +110,12 @@ pub mod tests {
}

#[test]
// flagged broken for taking 60+ seconds to run
#[cfg(feature = "broken-tests")]
fn test_command_no_test_folder() {
let temp_space = gen_dir();
let temp_dir_path = temp_space.path();
let temp_dir_path_buf = temp_space.path().to_path_buf();

// do init first, so there's a project
Command::main_binary()
.unwrap()
.args(&["init", temp_dir_path.to_str().unwrap()])
.assert()
.success();
let temp_dir = gen_dir();
let temp_dir_path = temp_dir.path();
let temp_dir_path_buf = temp_dir_path.to_path_buf();

let _ = init(&temp_dir_path_buf);

let result = test(&temp_dir_path_buf, "west", "test/index.js", false);

Expand Down
13 changes: 11 additions & 2 deletions cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum Cli {
#[structopt(
name = "run",
alias = "r",
about = "Starts a development container with an open websocket"
about = "Starts a development container with a websocket or http interface"
)]
Run {
#[structopt(
Expand All @@ -111,6 +111,13 @@ enum Cli {
persist: bool,
#[structopt(long, help = "Use real networking")]
networked: bool,
#[structopt(
long,
short,
help = "Specify interface type to use: websocket/http",
default_value = "websocket"
)]
interface: String,
},
#[structopt(
name = "test",
Expand Down Expand Up @@ -163,7 +170,9 @@ fn run() -> HolochainResult<()> {
port,
persist,
networked,
} => cli::run(package, port, persist, networked).map_err(HolochainError::Default)?,
interface,
} => cli::run(package, port, persist, networked, interface)
.map_err(HolochainError::Default)?,
Cli::Test {
dir,
testfile,
Expand Down