Skip to content

Commit

Permalink
Fixes completion tests waiting for user input
Browse files Browse the repository at this point in the history
  • Loading branch information
954alberto committed Oct 24, 2024
1 parent c445566 commit d5227b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
26 changes: 16 additions & 10 deletions src/lib/completion.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io::BufRead;
use std::path::Path;
use std::{fs, io};


/// # Completions Module
///
/// This module handles the generation of shell completion scripts for the `cargo-make` tool.
Expand All @@ -26,7 +26,7 @@ mod completion_test;
pub fn generate_completions(shell: &str) {
match shell {
"zsh" => {
if let Err(e) = generate_completion_zsh() {
if let Err(e) = generate_completion_zsh(None) {
eprintln!("Error generating Zsh completions: {}", e);
}
}
Expand All @@ -36,7 +36,8 @@ pub fn generate_completions(shell: &str) {
}
}

fn generate_completion_zsh() -> Result<(), Box<dyn std::error::Error>> {
// Modify the function to accept an optional input stream
fn generate_completion_zsh(input: Option<&mut dyn io::Read>) -> Result<(), Box<dyn std::error::Error>> {
let home_dir = std::env::var("HOME")?;
let zfunc_dir = format!("{}/.zfunc", home_dir);
let completion_file = format!("{}/_cargo-make", zfunc_dir);
Expand All @@ -50,13 +51,18 @@ fn generate_completion_zsh() -> Result<(), Box<dyn std::error::Error>> {
}

if Path::new(&completion_file).exists() {
let mut input = String::new();
println!(
"File {} already exists. Overwrite? (y/n): ",
completion_file
);
io::stdin().read_line(&mut input)?;
if input.trim().to_lowercase() != "y" {
let mut input_str = String::new();
let reader: Box<dyn io::Read> = match input {
Some(input) => Box::new(input),
None => Box::new(io::stdin()),
};

// Create a BufReader to read from the provided input or stdin
let mut buf_reader = io::BufReader::new(reader);
println!("File {} already exists. Overwrite? (y/n): ", completion_file);
buf_reader.read_line(&mut input_str)?;

if input_str.trim().to_lowercase() != "y" {
println!("Aborted overwriting the file.");
return Ok(());
}
Expand Down
51 changes: 40 additions & 11 deletions src/lib/completion_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use super::*;
use std::fs;
use std::path::Path;
use std::io::Cursor;

#[cfg(test)]
mod tests {
use super::*;
use crate::completion::generate_completion_zsh;

use super::*;

// Function to clean up test environment by removing the completion file
fn cleanup() {
let home_dir = std::env::var("HOME").expect("Failed to get HOME");
Expand All @@ -15,11 +17,34 @@ mod tests {
}
}

#[test]
fn test_generate_completion_zsh_overwrite_prompt_yes() {
cleanup(); // Clean up before the test
let input = b"y\n"; // Simulate user input of 'y'
let mut reader = Cursor::new(input);

let result = generate_completion_zsh(Some(&mut reader));
assert!(result.is_ok());
}

#[test]
fn test_generate_completion_zsh_overwrite_prompt_no() {
cleanup(); // Clean up before the test
let input = b"n\n"; // Simulate user input of 'n'
let mut reader = Cursor::new(input);

let result = generate_completion_zsh(Some(&mut reader));
assert!(result.is_ok());
}

#[test]
fn test_generate_completion_zsh_creates_directory() {
cleanup(); // Clean up before the test

let result = generate_completion_zsh();
let input = b"y\n"; // Simulate user input of 'y'
let mut reader = Cursor::new(input);

let result = generate_completion_zsh(Some(&mut reader));
assert!(result.is_ok(), "Should succeed in generating completions");

// Check if the directory was created
Expand All @@ -32,7 +57,10 @@ mod tests {
fn test_generate_completion_zsh_creates_file() {
cleanup(); // Clean up before the test

let result = generate_completion_zsh();
let input = b"y\n"; // Simulate user input of 'y'
let mut reader = Cursor::new(input);

let result = generate_completion_zsh(Some(&mut reader));
assert!(result.is_ok(), "Should succeed in generating completions");

// Check if the completion file was created
Expand All @@ -46,16 +74,17 @@ mod tests {
cleanup(); // Clean up before the test

// Create the directory and file first
generate_completion_zsh().expect("Should succeed in generating completions");
let input = b"y\n"; // Simulate user input of 'y'
let mut reader = Cursor::new(input);

generate_completion_zsh(Some(&mut reader)).expect("Should succeed in generating completions");

// Simulate user input for overwrite.
// You might want to refactor the `generate_completion_zsh` function to take an input parameter
// for easier testing.

// For now, let's just check that we can call it again
let result = generate_completion_zsh(); // This will prompt in the real environment
let input = b"y\n"; // Simulate user input of 'y' again
let mut reader = Cursor::new(input);

let result = generate_completion_zsh(Some(&mut reader));
assert!(result.is_ok(), "Should handle overwrite prompt gracefully");
}

// Additional tests can be added here for other scenarios
}

0 comments on commit d5227b9

Please sign in to comment.