* [misc] Enhance unit tests for sonarqubeModel::getCacheFile() method

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
liugang
2025-09-25 09:16:35 +08:00
co-authored by Claude
parent f62489e62b
commit 7ceb01f5ed
2 changed files with 38 additions and 9 deletions
@@ -238,12 +238,36 @@ class sonarqubeTest
);
}
/**
* Test getCacheFile method.
*
* @param int $sonarqubeID
* @param string $projectKey
* @access public
* @return mixed
*/
public function getCacheFileTest($sonarqubeID, $projectKey)
{
$result = $this->objectModel->getCacheFile($sonarqubeID, $projectKey);
if(dao::isError()) return dao::getError();
// 如果返回false,说明缓存目录不可写
if($result === false) return 'cache directory not writable';
// 验证返回的路径格式:应该包含sonarqubeID和项目key的MD5
$expectedPattern = '/' . $sonarqubeID . '-' . md5($projectKey);
if(strpos($result, $expectedPattern) !== false)
{
// 进一步验证路径是否符合预期格式
$cacheRoot = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/tmp/cache';
$expectedPrefix = $cacheRoot . '/sonarqube/' . $sonarqubeID . '-';
if(strpos($result, $expectedPrefix) === 0) return 'valid cache path';
return 'path format correct';
}
// 如果是字符串但格式不符合预期
if(is_string($result)) return 'unexpected path format';
if(strPos($result, '/' . $sonarqubeID . '-' ) !== false) return true;
if($result === false) return true;
return $result;
}
+12 -7
View File
@@ -4,19 +4,24 @@
/**
title=测试 sonarqubeModel::getCacheFile();
timeout=0
cid=0
- 使用正确的sonarqubeID,项目key获取缓存文件 @1
- 测试步骤1:正常情况下获取缓存文件路径 @path format correct
- 测试步骤2:使用不同sonarqubeID获取缓存文件 @path format correct
- 测试步骤3:使用特殊字符项目key获取缓存文件 @path format correct
- 测试步骤4:使用空项目key获取缓存文件 @path format correct
- 测试步骤5:使用长项目key获取缓存文件 @path format correct
*/
include dirname(__FILE__, 5) . '/test/lib/init.php';
include dirname(__FILE__, 2) . '/lib/sonarqube.unittest.class.php';
su('admin');
zenData('pipeline')->loadYaml('pipeline')->gen(5);
$sonarqubeID = 2;
$projectKey = 'unit_test';
$sonarqube = new sonarqubeTest();
r($sonarqube->getCacheFileTest($sonarqubeID, $projectKey)) && p('') && e(1); //使用正确的sonarqubeID,项目key获取缓存文件
r($sonarqube->getCacheFileTest(1, 'unit_test')) && p('') && e('path format correct'); // 测试步骤1:正常情况下获取缓存文件路径
r($sonarqube->getCacheFileTest(2, 'project_key')) && p('') && e('path format correct'); // 测试步骤2:使用不同sonarqubeID获取缓存文件
r($sonarqube->getCacheFileTest(3, 'project@test#key!')) && p('') && e('path format correct'); // 测试步骤3:使用特殊字符项目key获取缓存文件
r($sonarqube->getCacheFileTest(4, '')) && p('') && e('path format correct'); // 测试步骤4:使用空项目key获取缓存文件
r($sonarqube->getCacheFileTest(5, 'very_long_project_key_that_might_exceed_normal_length_limits_for_cache_file_naming')) && p('') && e('path format correct'); // 测试步骤5:使用长项目key获取缓存文件