Permalink
Browse files

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.
  • Loading branch information...
arrbee committed May 25, 2012
1 parent 4728b55 commit 29ef309e2ca39f68d11c755710446ff6d396d203
Showing with 51 additions and 4 deletions.
  1. +16 −4 src/fileops.c
  2. +35 −0 tests-clar/core/env.c
View
@@ -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");
View
@@ -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
+}

0 comments on commit 29ef309

Please sign in to comment.