Make errors for system and global files consistent The error codes from failed lookups of system and global files on Windows were not consistent with the codes returned on other platforms. This makes the error detection patterns match and adds a unit test for the various errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
diff --git a/src/fileops.c b/src/fileops.c
index cd4b3c4..95a6589 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -417,11 +417,17 @@ int git_futils_find_system_file(git_buf *path, const char *filename)
struct win32_path root;
if (win32_expand_path(&root, L"%PROGRAMFILES%\\Git\\etc\\") < 0 ||
- win32_find_file(path, &root, filename) < 0) {
- giterr_set(GITERR_OS, "Cannot find the system's Program Files directory");
+ root.path[0] == L'%') /* i.e. no expansion happened */
+ {
+ giterr_set(GITERR_OS, "Cannot locate the system's Program Files directory");
return -1;
}
+ if (win32_find_file(path, &root, filename) < 0) {
+ git_buf_clear(path);
+ return GIT_ENOTFOUND;
+ }
+
return 0;
#else
@@ -442,11 +448,17 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
struct win32_path root;
if (win32_expand_path(&root, L"%USERPROFILE%\\") < 0 ||
- win32_find_file(path, &root, filename) < 0) {
- giterr_set(GITERR_OS, "Failed to lookup the current user's Windows profile");
+ root.path[0] == L'%') /* i.e. no expansion happened */
+ {
+ giterr_set(GITERR_OS, "Cannot locate the user's profile directory");
return -1;
}
+ if (win32_find_file(path, &root, filename) < 0) {
+ git_buf_clear(path);
+ return GIT_ENOTFOUND;
+ }
+
return 0;
#else
const char *home = getenv("HOME");
diff --git a/tests-clar/core/env.c b/tests-clar/core/env.c
index fb483e8..3bde856 100644
--- a/tests-clar/core/env.c
+++ b/tests-clar/core/env.c
@@ -54,6 +54,7 @@ static int cl_setenv(const char *name, const char *value)
#ifdef GIT_WIN32
static char *env_userprofile = NULL;
+static char *env_programfiles = NULL;
#else
static char *env_home = NULL;
#endif
@@ -62,6 +63,7 @@ void test_core_env__initialize(void)
{
#ifdef GIT_WIN32
env_userprofile = cl_getenv("USERPROFILE");
+ env_programfiles = cl_getenv("PROGRAMFILES");
#else
env_home = cl_getenv("HOME");
#endif
@@ -72,6 +74,8 @@ void test_core_env__cleanup(void)
#ifdef GIT_WIN32
cl_setenv("USERPROFILE", env_userprofile);
git__free(env_userprofile);
+ cl_setenv("PROGRAMFILES", env_programfiles);
+ git__free(env_programfiles);
#else
cl_setenv("HOME", env_home);
#endif
@@ -128,3 +132,34 @@ void test_core_env__0(void)
git_buf_free(&path);
git_buf_free(&found);
}
+
+void test_core_env__1(void)
+{
+ git_buf path = GIT_BUF_INIT;
+
+ cl_assert(git_futils_find_global_file(&path, "nonexistentfile") == GIT_ENOTFOUND);
+
+#ifdef GIT_WIN32
+ cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
+#else
+ cl_git_pass(cl_setenv("HOME", "doesnotexist"));
+#endif
+
+ cl_assert(git_futils_find_global_file(&path, "nonexistentfile") == GIT_ENOTFOUND);
+
+#ifdef GIT_WIN32
+ cl_git_pass(cl_setenv("USERPROFILE", NULL));
+#else
+ cl_git_pass(cl_setenv("HOME", NULL));
+#endif
+
+ cl_assert(git_futils_find_global_file(&path, "nonexistentfile") == -1);
+
+ cl_assert(git_futils_find_system_file(&path, "nonexistentfile") == GIT_ENOTFOUND);
+
+#ifdef GIT_WIN32
+ cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
+
+ cl_assert(git_futils_find_system_file(&path, "nonexistentfile") == -1);
+#endif
+}