1
0

fix: add test for new added env function and fix their bugs

This commit is contained in:
2025-12-12 14:57:08 +08:00
parent 19086f44e2
commit 6c2dba74d1
3 changed files with 41 additions and 14 deletions

View File

@ -187,13 +187,19 @@ namespace yycc::env {
while (*current != L'\0') { while (*current != L'\0') {
// Fetch current wide string // Fetch current wide string
std::wstring_view entry(current); std::wstring_view entry(current);
// Increase the pointer first,
// because in later steps we may enter next loop before reaching the end of this loop syntax.
current += entry.length() + 1;
// Parse "KEY=VALUE" // Parse "KEY=VALUE"
size_t pos = entry.find(L'='); size_t pos = entry.find(L'=');
if (pos != std::string::npos) { if (pos != std::string::npos) {
auto key = entry.substr(0, pos); auto key = entry.substr(0, pos);
auto value = entry.substr(pos + 1); auto value = entry.substr(pos + 1);
if (key.empty()) return std::unexpected(VarError::NullPointer); // However, Idk why there are some shit words like "=::=::" are in this string list.
// This should be excluded and can not be seen as error.
// So we simply skip them and do not report error.
if (key.empty()) continue;
auto u8key = ENC::to_utf8(key); auto u8key = ENC::to_utf8(key);
auto u8value = ENC::to_utf8(value); auto u8value = ENC::to_utf8(value);
@ -205,9 +211,6 @@ namespace yycc::env {
} else { } else {
return std::unexpected(VarError::Others); return std::unexpected(VarError::Others);
} }
// Increase the pointer
current += entry.length() + 1;
} }
env_block.reset(); env_block.reset();
@ -408,7 +411,7 @@ namespace yycc::env {
if (argv == nullptr) return std::unexpected(ArgError::NullPointer); if (argv == nullptr) return std::unexpected(ArgError::NullPointer);
// Analyse it // Analyse it
for (int i = 1; i < argc; ++i) { // starts with 1 to remove first part (executable self) for (int i = 0; i < argc; ++i) { // starts with 1 to remove first part (executable self)
auto arg = argv.get()[i]; auto arg = argv.get()[i];
if (arg == nullptr) return std::unexpected(ArgError::NullPointer); if (arg == nullptr) return std::unexpected(ArgError::NullPointer);

View File

@ -77,6 +77,9 @@ namespace yycc::env {
*/ */
VarResult<std::vector<VarPair>> get_vars(); VarResult<std::vector<VarPair>> get_vars();
// TODO: Add join_paths() and split_paths() for variable
// especially for PATH-like variable.
#pragma endregion #pragma endregion
#pragma region Environment Path #pragma region Environment Path

View File

@ -1,7 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <yycc.hpp> #include <yycc.hpp>
#include <yycc/env.hpp> #include <yycc/env.hpp>
#include <yycc/macro/os_detector.hpp>
#include <filesystem> #include <filesystem>
#define ENV ::yycc::env #define ENV ::yycc::env
@ -44,18 +43,40 @@ namespace yycctest::env {
} }
} }
TEST(Env, EnvVars) {
auto rv = ENV::get_vars();
ASSERT_TRUE(rv.has_value());
EXPECT_FALSE(rv.value().empty());
}
TEST(Env, CurrentDir) {
auto rv = ENV::current_dir();
ASSERT_TRUE(rv.has_value());
EXPECT_TRUE(std::filesystem::is_directory(rv.value()));
}
TEST(Env, CurrentExe) { TEST(Env, CurrentExe) {
auto rv = ENV::current_exe(); auto rv = ENV::current_exe();
ASSERT_TRUE(rv.has_value()); ASSERT_TRUE(rv.has_value());
EXPECT_TRUE(std::filesystem::is_regular_file(rv.value()));
}
auto filename = rv.value().filename().u8string(); TEST(Env, HomeDir) {
#if defined(YYCC_OS_WINDOWS) auto rv = ENV::home_dir();
// Only Windows has special ext. ASSERT_TRUE(rv.has_value());
EXPECT_EQ(filename, u8"YYCCTest.exe"); EXPECT_TRUE(std::filesystem::is_directory(rv.value()));
#else }
// Executable in other system are all in plain name.
EXPECT_EQ(filename, u8"YYCCTest"); TEST(Env, TempDir) {
#endif auto rv = ENV::temp_dir();
ASSERT_TRUE(rv.has_value());
EXPECT_TRUE(std::filesystem::is_directory(rv.value()));
}
TEST(Env, EnvArgs) {
auto rv = ENV::get_args();
ASSERT_TRUE(rv.has_value());
EXPECT_FALSE(rv.value().empty());
} }
} // namespace yycctest::env } // namespace yycctest::env