From e0872d206286fa3ca3a9cda6a74122c6fdf58f32 Mon Sep 17 00:00:00 2001 From: dotandev Date: Wed, 28 Jan 2026 23:10:54 +0100 Subject: [PATCH] Add comprehensive integration tests Fixes #10 --- tests/integration_tests.rs | 94 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 6b41c88..e5db6b4 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -67,3 +67,97 @@ ports: .success() .stdout(predicate::str::contains("Port 9999 is available")); } + +#[test] +fn test_cli_init() { + let temp_dir = tempfile::tempdir().unwrap(); + let config_path = temp_dir.path().join(".envcheck.yaml"); + + let mut cmd = Command::cargo_bin("envcheck").unwrap(); + cmd.current_dir(temp_dir.path()).arg("init"); + cmd.assert().success(); + + assert!(config_path.exists()); + let content = std::fs::read_to_string(config_path).unwrap(); + assert!(content.contains("version: \"1\"")); + assert!(content.contains("tools:")); +} + +#[test] +fn test_cli_json_output() { + let mut file = NamedTempFile::new().unwrap(); + writeln!( + file, + r#"version: "1" +env_vars: + - name: PATH + required: true +"# + ).unwrap(); + + let mut cmd = Command::cargo_bin("envcheck").unwrap(); + cmd.arg("--config").arg(file.path()).arg("--json"); + + let output = cmd.assert().success().get_output().stdout.clone(); + let stdout_str = String::from_utf8(output).unwrap(); + + // Check if it's valid JSON and has expected fields + let v: serde_json::Value = serde_json::from_str(&stdout_str).unwrap(); + assert!(v["results"].is_array()); + assert!(v["summary"].is_object()); + assert!(v["passed"].is_boolean()); +} + +#[test] +fn test_cli_env_regex() { + let mut file = NamedTempFile::new().unwrap(); + writeln!( + file, + r#"version: "1" +env_vars: + - name: TEST_REGEX_VAR + pattern: "^[0-9]{{3}}$" + required: true +"# + ).unwrap(); + + // Test failure + let mut cmd = Command::cargo_bin("envcheck").unwrap(); + cmd.arg("--config").arg(file.path()) + .env("TEST_REGEX_VAR", "abc"); + cmd.assert() + .failure() + .stdout(predicate::str::contains("TEST_REGEX_VAR is set but does not match pattern")); + + // Test success + let mut cmd = Command::cargo_bin("envcheck").unwrap(); + cmd.arg("--config").arg(file.path()) + .env("TEST_REGEX_VAR", "123"); + cmd.assert() + .success() + .stdout(predicate::str::contains("TEST_REGEX_VAR is set and matches pattern")); +} + +#[test] +fn test_cli_directory_check() { + let temp_dir = tempfile::tempdir().unwrap(); + let dir_path = temp_dir.path().to_str().unwrap(); + + let mut file = NamedTempFile::new().unwrap(); + writeln!( + file, + r#"version: "1" +files: + - path: "{}" + is_directory: true + required: true +"#, + dir_path + ).unwrap(); + + let mut cmd = Command::cargo_bin("envcheck").unwrap(); + cmd.arg("--config").arg(file.path()); + cmd.assert() + .success() + .stdout(predicate::str::contains(format!("Directory {} exists", dir_path))); +}