Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit 7affdff

Browse files
committed
Fix SonarQube client initialization tests to avoid state interference
1 parent 3d33491 commit 7affdff

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# SonarQube MCP Server
22

3+
[![CI](https://github.com/sapientpants/sonarqube-mcp-server/actions/workflows/ci.yml/badge.svg)](https://github.com/sapientpants/sonarqube-mcp-server/actions/workflows/ci.yml)
4+
35
A Rust implementation of a Model Context Protocol (MCP) server that integrates with SonarQube to provide AI assistants with access to code quality metrics, issues, and quality gate statuses.
46

57
## Overview

tests/sonarqube_tools_tests.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,63 +9,50 @@ use sonarqube_mcp_server::mcp::types::CallToolResultContent;
99
use std::env;
1010

1111
#[test]
12-
fn test_init_sonarqube_client_missing_url() {
12+
fn test_init_sonarqube_client_error_conditions() {
1313
// Save original environment variables
1414
let original_url = env::var("SONARQUBE_URL").ok();
1515
let original_token = env::var("SONARQUBE_TOKEN").ok();
1616

17+
// Test 1: Missing URL
18+
// ------------------
1719
// Unset environment variables
1820
env::remove_var("SONARQUBE_URL");
1921
env::remove_var("SONARQUBE_TOKEN");
2022

2123
// Try to initialize client
2224
let result = sonarqube_mcp_server::mcp::sonarqube::tools::init_sonarqube_client();
2325

24-
// Verify error
26+
// Verify error when URL is missing
2527
assert!(result.is_err());
2628
match result {
2729
Err(SonarError::Config(msg)) => {
28-
assert!(msg.contains("SONARQUBE_URL"));
30+
assert_eq!(msg, "SONARQUBE_URL environment variable not set");
2931
}
30-
_ => panic!("Expected Config error"),
32+
_ => panic!("Expected Config error for missing URL"),
3133
}
3234

33-
// Restore environment variables
34-
if let Some(url) = original_url {
35-
env::set_var("SONARQUBE_URL", url);
36-
}
37-
if let Some(token) = original_token {
38-
env::set_var("SONARQUBE_TOKEN", token);
39-
}
40-
}
41-
42-
#[test]
43-
fn test_init_sonarqube_client_missing_token() {
44-
// Save original environment variables
45-
let original_url = env::var("SONARQUBE_URL").ok();
46-
let original_token = env::var("SONARQUBE_TOKEN").ok();
47-
48-
// Set URL but unset token
35+
// Test 2: Missing Token
36+
// -------------------
37+
// Set the URL but not the token
4938
env::set_var("SONARQUBE_URL", "https://sonarqube.example.com");
50-
env::remove_var("SONARQUBE_TOKEN");
5139

52-
// Try to initialize client
40+
// Try to initialize client again
5341
let result = sonarqube_mcp_server::mcp::sonarqube::tools::init_sonarqube_client();
5442

55-
// Verify error
43+
// Verify error when token is missing
5644
assert!(result.is_err());
5745
match result {
5846
Err(SonarError::Config(msg)) => {
59-
assert!(msg.contains("SONARQUBE_TOKEN"));
47+
assert_eq!(msg, "SONARQUBE_TOKEN environment variable not set");
6048
}
61-
_ => panic!("Expected Config error"),
49+
_ => panic!("Expected Config error for missing token"),
6250
}
6351

6452
// Restore environment variables
53+
env::remove_var("SONARQUBE_URL");
6554
if let Some(url) = original_url {
6655
env::set_var("SONARQUBE_URL", url);
67-
} else {
68-
env::remove_var("SONARQUBE_URL");
6956
}
7057
if let Some(token) = original_token {
7158
env::set_var("SONARQUBE_TOKEN", token);

0 commit comments

Comments
 (0)