Commit 29ef309e2ca39f68d11c755710446ff6d396d203

Russell Belfer 2012-05-25T09:44:56

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.

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
+}