Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#43): fakehub start, --port #60

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,28 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use clap::Parser;
use clap::{Parser, Subcommand};

// @todo #13:15min Handle port argument
// We should process the port argument and
// pass it to the server on `start` command.
// Start command should be added also with clap
// @todo #41:15min Add --report argument.
// Let's add --report option for generating reports in desired formats:
// We should support following formats: xml, tex, and txt. User should have
// ability to generate report in multiple formats as well: --report tex,xml,txt.
#[derive(Parser, Debug)]
#[command(name = "fakehub")]
pub(crate) struct Args {
#[command(subcommand)]
pub(crate) command: Command,
}

#[derive(Subcommand, Debug)]
pub(crate) enum Command {
// Start the server
h1alexbel marked this conversation as resolved.
Show resolved Hide resolved
#[command(about = "Start the server")]
Start(StartArgs),
}

#[derive(Parser, Debug)]
pub(crate) struct StartArgs {
/// The port to run
#[arg(short, long, default_value_t = 3000)]
pub(crate) port: usize,
Expand Down
23 changes: 20 additions & 3 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use clap::Parser;
use log::info;

use crate::args::Args;
use server::Server;

use crate::args::{Args, Command};

mod args;

fn main() {
let _ = Args::parse();
#[tokio::main]
async fn main() {
let args = Args::parse();
match args.command {
Command::Start(start_args) => {
info!("Starting server on port {}", start_args.port);
let server = Server::new(start_args.port);
match server.start().await {
Ok(_) => info!("Server started successfully on port {}", start_args.port),
Err(e) => panic!(

Check warning on line 40 in cli/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/main.rs#L31-L40

Added lines #L31 - L40 were not covered by tests
"{}",
format!("Failed to start server on port {}: {}", start_args.port, e)

Check warning on line 42 in cli/src/main.rs

View check run for this annotation

Codecov / codecov/patch

cli/src/main.rs#L42

Added line #L42 was not covered by tests
),
}
}
}
}
41 changes: 38 additions & 3 deletions cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,47 @@ use anyhow::Result;
use assert_cmd::Command;

#[test]
fn should_output_help() -> Result<()> {
fn outputs_help() -> Result<()> {
env!("CARGO_BIN_EXE_cli");
h1alexbel marked this conversation as resolved.
Show resolved Hide resolved
let assertion = Command::cargo_bin("cli")?.arg("--help").assert();
let bytes = assertion.get_output().stdout.as_slice();
let output = str::from_utf8(bytes)?;
assert!(output.contains("The port to run"));
assert!(output.contains("3000"));
assert!(output.contains("help"));
assert!(output.contains("start"));
assert!(output.contains("Start the server"));
assert!(output.contains("--help"));
assert!(output.contains("Print help"));
Ok(())
}

#[test]
fn outputs_start_opts() -> Result<()> {
let assertion = Command::cargo_bin("cli")?
.arg("start")
.arg("--help")
.assert();
let bytes = assertion.get_output().stdout.as_slice();
let output = str::from_utf8(bytes)?;
assert!(output.contains("--port"));
assert!(output.contains("The port to run [default: 3000]"));
Ok(())
}

// @todo #43:30min Enable starts_server integration test.
// We should enable this integration test that checks whether server starts or
// not. This test should output success message in info!. For now it does not
// work. Probably some error with logging configuration. Don't forget to
// remove this puzzle.
#[test]
#[ignore]
fn starts_server() -> Result<()> {
env!("CARGO_BIN_EXE_cli");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant env!

let assertion = Command::cargo_bin("cli")?
.arg("start")
.arg("--port 8080")
.assert();
let bytes = assertion.get_output().stdout.as_slice();
let output = str::from_utf8(bytes)?;
assert!(output.contains("Server started successfully on port 8080"));
Ok(())
}
Loading