Skip to content

Commit

Permalink
Merge pull request #12 from Kobzol/profile-name-validation
Browse files Browse the repository at this point in the history
Profile name validation
  • Loading branch information
Kobzol authored Mar 11, 2024
2 parents 88ce5ea + 131185b commit 4aaea4b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dev

- Detect if a linker is installed and don't display warning if it is (https://github.com/Kobzol/cargo-wizard/issues/5).
- Validate profile name (https://github.com/Kobzol/cargo-wizard/issues/7).

# 0.2.1 (10. 3. 2024)

Expand Down
30 changes: 25 additions & 5 deletions src/dialog/prompts/select_profile.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::fmt::{Display, Formatter};

use inquire::ui::{Color, RenderConfig};
use inquire::validator::{ErrorMessage, Validation};
use inquire::{Select, Text};

use cargo_wizard::Profile;

use crate::cli::CliConfig;
use crate::dialog::utils::{colorize_render_config, create_render_config};
use crate::dialog::PromptResult;
use cargo_wizard::Profile;
use inquire::ui::{Color, RenderConfig};
use inquire::{min_length, Select, Text};
use std::fmt::{Display, Formatter};

pub fn prompt_select_profile(
cli_config: &CliConfig,
Expand Down Expand Up @@ -58,7 +62,23 @@ pub fn prompt_select_profile(

fn prompt_enter_profile_name(cli_config: &CliConfig) -> PromptResult<Profile> {
let profile = Text::new("Select profile name:")
.with_validator(min_length!(1))
.with_validator(|input: &str| {
if input.is_empty() {
return Ok(Validation::Invalid(ErrorMessage::Custom(
"Profile name must not be empty".to_string(),
)));
} else if !input
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == '-')
{
// Yes, Cargo does allow any unicode alphabet char or digit :)
return Ok(Validation::Invalid(ErrorMessage::Custom(
"Profile name may contain only letters, numbers, underscore and hyphen"
.to_string(),
)));
}
Ok(Validation::Valid)
})
.with_render_config(profile_render_config(cli_config))
.prompt()?;
let profile = match profile.as_str() {
Expand Down
30 changes: 28 additions & 2 deletions tests/integration/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::terminal::Terminal;
use crate::utils::{init_cargo_project, CargoProject};

#[test]
Expand Down Expand Up @@ -154,6 +155,26 @@ fn dialog_fast_compile_to_new_profile() -> anyhow::Result<()> {
Ok(())
}

#[test]
fn dialog_empty_profile_name() -> anyhow::Result<()> {
let project = init_cargo_project()?;

let mut terminal = DialogBuilder::default().start(&project)?;
terminal.select_line("<Create a new profile>")?;
terminal.line("")?;
terminal.expect("Profile name must not be empty")
}

#[test]
fn dialog_invalid_profile_name() -> anyhow::Result<()> {
let project = init_cargo_project()?;

let mut terminal = DialogBuilder::default().start(&project)?;
terminal.select_line("<Create a new profile>")?;
terminal.line("#")?;
terminal.expect("Profile name may contain only letters, numbers, underscore and hyphen")
}

#[test]
fn dialog_create_config() -> anyhow::Result<()> {
let project = init_cargo_project()?;
Expand Down Expand Up @@ -421,14 +442,19 @@ impl DialogBuilder {
self
}

fn run(self, project: &CargoProject) -> anyhow::Result<()> {
fn start(&self, project: &CargoProject) -> anyhow::Result<Terminal> {
let nightly = match self.nightly {
true => "on",
false => "off",
};
let mut terminal = project
let terminal = project
.cmd(&[&format!("--nightly={nightly}")])
.start_terminal()?;
Ok(terminal)
}

fn run(self, project: &CargoProject) -> anyhow::Result<()> {
let mut terminal = self.start(project)?;
// Select profile
terminal.expect("Select the profile that you want to update/create")?;
terminal.select_line(&self.profile)?;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::process::{Command, Output, Stdio};
use tempfile::TempDir;

#[cfg(target_os = "linux")]
mod terminal;
pub mod terminal;

pub struct CargoProject {
_name: String,
Expand Down

0 comments on commit 4aaea4b

Please sign in to comment.