feat(keyring): 集成系统密钥环安全存储 API key
This commit is contained in:
@@ -47,6 +47,24 @@ fn create_commit(dir: &PathBuf, message: &str) {
|
||||
.expect("Failed to create commit");
|
||||
}
|
||||
|
||||
fn setup_git_repo(dir: &PathBuf) {
|
||||
create_git_repo(dir);
|
||||
configure_git_user(dir);
|
||||
}
|
||||
|
||||
fn setup_test_repo_with_file(dir: &PathBuf, file_name: &str, file_content: &str) {
|
||||
setup_git_repo(dir);
|
||||
create_test_file(dir, file_name, file_content);
|
||||
stage_file(dir, file_name);
|
||||
}
|
||||
|
||||
fn init_quicommit(dir: &PathBuf, config_path: &PathBuf) {
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(dir);
|
||||
cmd.assert().success();
|
||||
}
|
||||
|
||||
mod cli_basic {
|
||||
use super::*;
|
||||
|
||||
@@ -57,7 +75,10 @@ mod cli_basic {
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("QuiCommit"))
|
||||
.stdout(predicate::str::contains("AI-powered Git assistant"));
|
||||
.stdout(predicate::str::contains("AI-powered Git assistant"))
|
||||
.stdout(predicate::str::contains("Usage:"))
|
||||
.stdout(predicate::str::contains("Commands:"))
|
||||
.stdout(predicate::str::contains("Options:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -252,15 +273,10 @@ mod commit_command {
|
||||
fn test_commit_no_changes() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "test: empty", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -275,18 +291,10 @@ mod commit_command {
|
||||
fn test_commit_with_staged_changes() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "Hello, World!");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "Hello, World!");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "test: add test file", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -301,18 +309,10 @@ mod commit_command {
|
||||
fn test_commit_date_mode() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "daily.txt", "Daily update");
|
||||
stage_file(&repo_path, "daily.txt");
|
||||
setup_test_repo_with_file(&repo_path, "daily.txt", "Daily update");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--date", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -349,19 +349,14 @@ mod tag_command {
|
||||
fn test_tag_list_empty() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
create_commit(&repo_path, "feat: initial commit");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["tag", "--name", "v0.1.0", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -380,16 +375,12 @@ mod changelog_command {
|
||||
fn test_changelog_init() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
let changelog_path = repo_path.join("CHANGELOG.md");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["changelog", "--init", "--output", changelog_path.to_str().unwrap(), "--config", config_path.to_str().unwrap()])
|
||||
@@ -404,19 +395,14 @@ mod changelog_command {
|
||||
fn test_changelog_dry_run() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
setup_git_repo(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
create_commit(&repo_path, "feat: add feature");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["changelog", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -532,18 +518,10 @@ mod validators {
|
||||
fn test_commit_message_validation() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "invalid commit message without type", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -558,18 +536,10 @@ mod validators {
|
||||
fn test_valid_conventional_commit() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "feat: add new feature", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -588,18 +558,10 @@ mod subcommands {
|
||||
fn test_commit_alias() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
create_git_repo(&repo_path);
|
||||
configure_git_user(&repo_path);
|
||||
|
||||
create_test_file(&repo_path, "test.txt", "content");
|
||||
stage_file(&repo_path, "test.txt");
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
cmd.assert().success();
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["c", "--manual", "-m", "fix: test", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
@@ -640,3 +602,59 @@ mod subcommands {
|
||||
.stdout(predicate::str::contains("default"));
|
||||
}
|
||||
}
|
||||
|
||||
mod edge_cases {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_file_not_found() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let non_existent_config = temp_dir.path().join("non_existent_config.toml");
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["config", "show", "--config", non_existent_config.to_str().unwrap()]);
|
||||
|
||||
cmd.assert()
|
||||
.success()
|
||||
.stdout(predicate::str::contains("QuiCommit Configuration"))
|
||||
.stdout(predicate::str::contains("Default profile: (none)"))
|
||||
.stdout(predicate::str::contains("Profiles: 0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_git_repo() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["init", "--yes", "--config", config_path.to_str().unwrap()]);
|
||||
cmd.assert().success();
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("git").or(predicate::str::contains("repository")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_empty_commit_message() {
|
||||
let temp_dir = TempDir::new().unwrap();
|
||||
let repo_path = temp_dir.path().to_path_buf();
|
||||
setup_test_repo_with_file(&repo_path, "test.txt", "content");
|
||||
|
||||
let config_path = repo_path.join("config.toml");
|
||||
init_quicommit(&repo_path, &config_path);
|
||||
|
||||
let mut cmd = Command::cargo_bin("quicommit").unwrap();
|
||||
cmd.args(&["commit", "--manual", "-m", "", "--dry-run", "--yes", "--config", config_path.to_str().unwrap()])
|
||||
.current_dir(&repo_path);
|
||||
|
||||
cmd.assert()
|
||||
.failure()
|
||||
.stderr(predicate::str::contains("Invalid conventional commit format"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user