-
Notifications
You must be signed in to change notification settings - Fork 1
Add dela allow
#166
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
Merged
Add dela allow
#166
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| use crate::allowlist; | ||
| use crate::task_discovery; | ||
| use crate::types::AllowScope; | ||
| use std::env; | ||
|
|
||
| /// Executes the 'dela allow' command to add a specific task to the allowlist. | ||
| pub fn execute(task_name: &str) -> anyhow::Result<()> { | ||
| let current_dir = env::current_dir() | ||
| .map_err(|e| anyhow::anyhow!("Failed to get current directory: {}", e))?; | ||
| let discovered = task_discovery::discover_tasks(¤t_dir); | ||
|
|
||
| // Find all tasks with the given name (both original and disambiguated) | ||
| let matching_tasks = task_discovery::get_matching_tasks(&discovered, task_name); | ||
|
|
||
| match matching_tasks.len() { | ||
| 0 => Err(anyhow::anyhow!( | ||
| "dela: command or task not found: {}", | ||
| task_name | ||
| )), | ||
| 1 => { | ||
| let task = matching_tasks[0]; | ||
| allowlist::check_task_allowed_with_scope(task, AllowScope::Task)?; | ||
| println!( | ||
| "Added task '{}' ({}) to allowlist.", | ||
| task.name, | ||
| task.runner.short_name() | ||
| ); | ||
| Ok(()) | ||
| } | ||
| _ => { | ||
| let error_msg = task_discovery::format_ambiguous_task_error(task_name, &matching_tasks); | ||
| eprintln!("{}", error_msg); | ||
| Err(anyhow::anyhow!( | ||
| "Multiple tasks named '{}' found", | ||
| task_name | ||
| )) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::config::{preferred_allowlist_path_for, preferred_config_dir_path_for}; | ||
| use crate::environment::{TestEnvironment, reset_to_real_environment, set_test_environment}; | ||
| use serial_test::serial; | ||
| use std::env; | ||
| use std::fs::{self, File}; | ||
| use std::io::Write; | ||
| use tempfile::TempDir; | ||
|
|
||
| struct TestEnvGuard { | ||
| project_dir: TempDir, | ||
| home_dir: TempDir, | ||
| } | ||
|
|
||
| impl TestEnvGuard { | ||
| fn new() -> Self { | ||
| let (project_dir, home_dir) = setup_test_env(); | ||
| Self { | ||
| project_dir, | ||
| home_dir, | ||
| } | ||
| } | ||
|
|
||
| fn new_uninitialized() -> Self { | ||
| // Create a temp dir for HOME but don't create the dela config directory | ||
| let home_dir = TempDir::new().expect("Failed to create temp HOME directory"); | ||
|
|
||
| // Set up test environment with the temp directory as HOME | ||
| let test_env = TestEnvironment::new().with_home(home_dir.path().to_string_lossy()); | ||
| set_test_environment(test_env); | ||
|
|
||
| Self { | ||
| project_dir: TempDir::new().expect("Failed to create temp directory"), | ||
| home_dir, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for TestEnvGuard { | ||
| fn drop(&mut self) { | ||
| reset_to_real_environment(); | ||
| } | ||
| } | ||
|
|
||
| fn setup_test_env() -> (TempDir, TempDir) { | ||
| // Create a temp dir for the project | ||
| let project_dir = TempDir::new().expect("Failed to create temp directory"); | ||
|
|
||
| // Create a test Makefile | ||
| let makefile_content = " | ||
| build: ## Building the project | ||
| \t@echo Building... | ||
|
|
||
| test: ## Running tests | ||
| \t@echo Testing... | ||
| "; | ||
| let mut makefile = | ||
| File::create(project_dir.path().join("Makefile")).expect("Failed to create Makefile"); | ||
| makefile | ||
| .write_all(makefile_content.as_bytes()) | ||
| .expect("Failed to write Makefile"); | ||
|
|
||
| // Create a temp dir for HOME and set it up | ||
| let home_dir = TempDir::new().expect("Failed to create temp HOME directory"); | ||
|
|
||
| // Set up test environment with the temp directory as HOME | ||
| let test_env = TestEnvironment::new().with_home(home_dir.path().to_string_lossy()); | ||
| set_test_environment(test_env); | ||
|
|
||
| // Create ~/.config/dela directory | ||
| fs::create_dir_all(preferred_config_dir_path_for(home_dir.path())) | ||
| .expect("Failed to create dela config directory"); | ||
|
|
||
| (project_dir, home_dir) | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_execute_allow_single_task() { | ||
| let guard = TestEnvGuard::new(); | ||
| env::set_current_dir(&guard.project_dir).expect("Failed to change directory"); | ||
|
|
||
| let result = execute("test"); | ||
| assert!(result.is_ok(), "Should succeed for a single task"); | ||
|
|
||
| // Verify it was added to the allowlist | ||
| let allowlist_content = | ||
| fs::read_to_string(preferred_allowlist_path_for(guard.home_dir.path())).unwrap(); | ||
| assert!(allowlist_content.contains("test")); | ||
| assert!(allowlist_content.contains("scope = \"Task\"")); | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_execute_allow_no_task() { | ||
| let guard = TestEnvGuard::new(); | ||
| env::set_current_dir(&guard.project_dir).expect("Failed to change directory"); | ||
|
|
||
| let result = execute("nonexistent"); | ||
| assert!(result.is_err(), "Should fail for nonexistent task"); | ||
| assert_eq!( | ||
| result.unwrap_err().to_string(), | ||
| "dela: command or task not found: nonexistent" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_execute_allow_uninitialized() { | ||
| let guard = TestEnvGuard::new_uninitialized(); | ||
| env::set_current_dir(&guard.project_dir).expect("Failed to change directory"); | ||
|
|
||
| // Create a test Makefile | ||
| let makefile_content = " | ||
| test: ## Running tests | ||
| \t@echo Testing... | ||
| "; | ||
| let mut makefile = File::create(guard.project_dir.path().join("Makefile")) | ||
| .expect("Failed to create Makefile"); | ||
| makefile | ||
| .write_all(makefile_content.as_bytes()) | ||
| .expect("Failed to write Makefile"); | ||
|
|
||
| let result = execute("test"); | ||
| assert!(result.is_err(), "Should fail when dela is not initialized"); | ||
| assert_eq!( | ||
| result.unwrap_err().to_string(), | ||
| "Dela is not initialized. Please run 'dela init' first." | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| #[serial] | ||
| fn test_execute_allow_disambiguated() { | ||
| let guard = TestEnvGuard::new(); | ||
| env::set_current_dir(&guard.project_dir).expect("Failed to change directory"); | ||
|
|
||
| // Create a package.json with the same task name | ||
| let package_json_content = r#"{ | ||
| "name": "test-package", | ||
| "scripts": { | ||
| "test": "jest" | ||
| } | ||
| }"#; | ||
|
|
||
| File::create(guard.project_dir.path().join("package.json")) | ||
| .unwrap() | ||
| .write_all(package_json_content.as_bytes()) | ||
| .unwrap(); | ||
|
|
||
| // Create package-lock.json to ensure npm is detected | ||
| File::create(guard.project_dir.path().join("package-lock.json")) | ||
| .unwrap() | ||
| .write_all(b"{}") | ||
| .unwrap(); | ||
|
|
||
| // Ambiguous task name 'test' should fail | ||
| let result = execute("test"); | ||
| assert!(result.is_err(), "Should fail for ambiguous task name"); | ||
| assert!( | ||
| result | ||
| .unwrap_err() | ||
| .to_string() | ||
| .contains("Multiple tasks named 'test' found") | ||
| ); | ||
|
|
||
| // Suffixed/disambiguated task name should succeed | ||
| let result = execute("test-m"); | ||
| assert!(result.is_ok(), "Should succeed with disambiguated name"); | ||
|
|
||
| let allowlist_content = | ||
| fs::read_to_string(preferred_allowlist_path_for(guard.home_dir.path())).unwrap(); | ||
| assert!(allowlist_content.contains("test")); // Note: allowlist uses original task name | ||
| assert!(allowlist_content.contains("scope = \"Task\"")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| pub mod allow; | ||
| pub mod allow_command; | ||
| pub mod configure_shell; | ||
| pub mod get_command; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.