Skip to content

Commit

Permalink
Make errors for system and global files consistent
Browse files Browse the repository at this point in the history
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 29ef309
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/fileops.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand Down
35 changes: 35 additions & 0 deletions tests-clar/core/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.