Made the tests pass on Windows & fixed special characters in environment variable names (#1065)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-10-15 19:13:44 +02:00
1 parent d8071910d9
commit 45c3500623
5 files changed
+74 -31

No files matched your search

+42 -6
View File
@@ -859,11 +859,18 @@ fn enterprise_env_key_equals(env_name: &str, expected: &str) -> bool {
#[cfg(target_os = "windows")]
fn enterprise_env_key_suffix<'a>(env_name: &'a str, prefix: &str) -> Option<&'a str> {
if env_name.len() < prefix.len() {
return None;
}
enterprise_env_key_suffix_ignoring_case(env_name, prefix)
}
let (raw_prefix, suffix) = env_name.split_at(prefix.len());
/// Returns the slot suffix of an environment variable name, comparing the prefix without
/// regard to case, the way Windows compares the names of environment variables.
///
/// Every environment variable passes through here, whatever characters its name contains.
/// Where the length of the prefix ends inside one of them, the name cannot start with the
/// prefix, and splitting it there would panic.
#[cfg(any(target_os = "windows", test))]
fn enterprise_env_key_suffix_ignoring_case<'a>(env_name: &'a str, prefix: &str) -> Option<&'a str> {
let (raw_prefix, suffix) = env_name.split_at_checked(prefix.len())?;
if raw_prefix.eq_ignore_ascii_case(prefix) {
normalize_enterprise_slot_suffix(suffix)
} else {
@@ -1368,8 +1375,9 @@ fn normalize_enterprise_config_id(value: &str) -> Option<String> {
#[cfg(test)]
mod tests {
use super::{
directory_is_writable, enterprise_environment_key_name,
enterprise_policy_file_slot_suffix, has_managed_installation_marker,
directory_is_writable, enterprise_env_key_suffix_ignoring_case,
enterprise_environment_key_name, enterprise_policy_file_slot_suffix,
has_managed_installation_marker,
is_windows_machine_wide_installation, is_windows_per_user_installation,
load_external_http_custom_root_certificate_policy_from_directories,
linux_policy_directories_from_xdg, load_policy_values_from_directories,
@@ -1378,6 +1386,7 @@ mod tests {
select_effective_enterprise_secret_source, update_target_installation_kind,
EnterpriseConfig, EnterpriseSourceData, EnterpriseSourceValue, EnterpriseSourceValues,
ExternalHttpCustomRootCertificatePolicy, InstallationKind, LinuxPackageType,
ENTERPRISE_ENV_CONFIG_ID_PREFIX, ENTERPRISE_ENV_CONFIG_SERVER_URL_PREFIX,
MANAGED_INSTALLATION_MARKER_FILE_NAME,
};
use std::collections::HashMap;
@@ -1626,6 +1635,33 @@ mod tests {
);
}
#[test]
fn enterprise_env_key_suffix_ignoring_case_accepts_any_case() {
assert_eq!(
enterprise_env_key_suffix_ignoring_case("mindwork_ai_studio_enterprise_config_id_10503", ENTERPRISE_ENV_CONFIG_ID_PREFIX),
Some("10503")
);
assert_eq!(
enterprise_env_key_suffix_ignoring_case("Mindwork_AI_Studio_Enterprise_Config_Server_URL_00000", ENTERPRISE_ENV_CONFIG_SERVER_URL_PREFIX),
Some("00000")
);
assert_eq!(
enterprise_env_key_suffix_ignoring_case("MINDWORK_AI_STUDIO", ENTERPRISE_ENV_CONFIG_ID_PREFIX),
None
);
}
#[test]
fn enterprise_env_key_suffix_ignoring_case_skips_names_where_the_prefix_ends_inside_a_character() {
for prefix in [ENTERPRISE_ENV_CONFIG_ID_PREFIX, ENTERPRISE_ENV_CONFIG_SERVER_URL_PREFIX] {
// Every "Ä" takes two bytes, and both prefixes are an odd number of bytes long:
let name = "Ä".repeat(prefix.len());
assert!(!name.is_char_boundary(prefix.len()));
assert_eq!(enterprise_env_key_suffix_ignoring_case(&name, prefix), None);
}
}
#[test]
fn parse_enterprise_source_values_keeps_environment_id_variable_as_source_detail() {
let mut values = EnterpriseSourceValues::new();
+18 -22
View File
@@ -17,6 +17,14 @@ use crate::environment::{is_dev, is_flatpak};
const FLATPAK_PERSISTENT_DATA_DIRECTORY: &str = "/var/data";
/// The name of the log file, without its extension. On Unix systems,
/// the file starts with a dot to be hidden.
const LOG_BASENAME: &str = if cfg!(unix) {
".AI Studio Events"
} else {
"AI Studio Events"
};
/// The key under which a log record carries the time its event happened, in microseconds
/// since the Unix epoch. A record without it happened when it reached the logger. The
/// formatters start the line with this time and never write the key itself.
@@ -77,18 +85,10 @@ pub fn init_logging(bundle_identifier: &str) {
log_config.push_str("qdrant_edge::common::mmap::ops=error, ");
log_config.push_str("qdrant_edge::common::mmap::advice=info");
// Configure the initial filename. On Unix systems, the file should start
// with a dot to be hidden.
let log_basename = match cfg!(unix)
{
true => ".AI Studio Events",
false => "AI Studio Events",
};
let (startup_log_directory, fallback_warning) = get_startup_log_path(bundle_identifier);
let log_path = FileSpec::default()
.directory(startup_log_directory)
.basename(log_basename)
.basename(LOG_BASENAME)
.suppress_timestamp()
.suffix("log");
@@ -243,12 +243,8 @@ where
fn ensure_log_directory_is_writable(directory: &Path) -> Result<(), String> {
create_dir_all(directory).map_err(|error| format!("could not create '{}': {error}", directory.display()))?;
let log_file_path = directory.join(if cfg!(unix) {
".AI Studio Events.log"
} else {
"AI Studio Events.log"
});
let log_file_path = directory.join(format!("{LOG_BASENAME}.log"));
OpenOptions::new()
.create(true)
.append(true)
@@ -773,12 +769,14 @@ mod tests {
#[test]
fn startup_log_path_storage_uses_selected_fallback_path() {
let temporary = PathBuf::from("/tmp").join(BUNDLE_IDENTIFIER).join("data");
// The temporary directory is absolute on every OS, while "/tmp" is not on Windows,
// where the stored path would then gain a drive letter and backslashes:
let temporary = temp_dir().join(BUNDLE_IDENTIFIER).join("data");
let (selected, _) = select_flatpak_startup_log_path(
BUNDLE_IDENTIFIER,
None,
PathBuf::from("/var/data"),
PathBuf::from("/tmp"),
temp_dir(),
|candidate| {
if candidate == temporary {
Ok(())
@@ -787,18 +785,16 @@ mod tests {
}
},
).unwrap();
let expected = selected.join(format!("{LOG_BASENAME}.log"));
let log_path = FileSpec::default()
.directory(selected)
.basename(".AI Studio Events")
.basename(LOG_BASENAME)
.suppress_timestamp()
.suffix("log");
let storage = OnceLock::new();
store_startup_log_path(&storage, &log_path);
assert_eq!(
storage.get().unwrap(),
"/tmp/org.mindworkai.AIStudio/data/.AI Studio Events.log",
);
assert_eq!(Path::new(storage.get().unwrap()), expected);
}
}