Merge #8406 'stdpath(): handle NULL default values'

This commit is contained in:
Justin M. Keyes
2018-05-18 19:33:28 +02:00
committed by GitHub
5 changed files with 44 additions and 41 deletions

View File

@@ -15712,6 +15712,9 @@ static void get_xdg_var_list(const XDGVarType xdg, typval_T *rettv)
rettv->vval.v_list = list; rettv->vval.v_list = list;
tv_list_ref(list); tv_list_ref(list);
char *const dirs = stdpaths_get_xdg_var(xdg); char *const dirs = stdpaths_get_xdg_var(xdg);
if (dirs == NULL) {
return;
}
do { do {
size_t dir_len; size_t dir_len;
const char *dir; const char *dir;
@@ -15737,15 +15740,15 @@ static void f_stdpath(typval_T *argvars, typval_T *rettv, FunPtr fptr)
return; // Type error; errmsg already given. return; // Type error; errmsg already given.
} }
if (strcmp(p, "config") == 0) { if (strequal(p, "config")) {
rettv->vval.v_string = (char_u *)get_xdg_home(kXDGConfigHome); rettv->vval.v_string = (char_u *)get_xdg_home(kXDGConfigHome);
} else if (strcmp(p, "data") == 0) { } else if (strequal(p, "data")) {
rettv->vval.v_string = (char_u *)get_xdg_home(kXDGDataHome); rettv->vval.v_string = (char_u *)get_xdg_home(kXDGDataHome);
} else if (strcmp(p, "cache") == 0) { } else if (strequal(p, "cache")) {
rettv->vval.v_string = (char_u *)get_xdg_home(kXDGCacheHome); rettv->vval.v_string = (char_u *)get_xdg_home(kXDGCacheHome);
} else if (strcmp(p, "config_dirs") == 0) { } else if (strequal(p, "config_dirs")) {
get_xdg_var_list(kXDGConfigDirs, rettv); get_xdg_var_list(kXDGConfigDirs, rettv);
} else if (strcmp(p, "data_dirs") == 0) { } else if (strequal(p, "data_dirs")) {
get_xdg_var_list(kXDGDataDirs, rettv); get_xdg_var_list(kXDGDataDirs, rettv);
} else { } else {
EMSG2(_("E6100: \"%s\" is not a valid stdpath"), p); EMSG2(_("E6100: \"%s\" is not a valid stdpath"), p);

View File

@@ -1735,7 +1735,7 @@ static bool do_user_initialization(void)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_WARN_UNUSED_RESULT
{ {
bool do_exrc = p_exrc; bool do_exrc = p_exrc;
if (process_env("VIMINIT", true) == OK) { if (process_env("VIMINIT") == OK) {
do_exrc = p_exrc; do_exrc = p_exrc;
return do_exrc; return do_exrc;
} }
@@ -1780,7 +1780,7 @@ static bool do_user_initialization(void)
} while (iter != NULL); } while (iter != NULL);
xfree(config_dirs); xfree(config_dirs);
} }
if (process_env("EXINIT", false) == OK) { if (process_env("EXINIT") == OK) {
do_exrc = p_exrc; do_exrc = p_exrc;
return do_exrc; return do_exrc;
} }
@@ -1844,17 +1844,14 @@ static void source_startup_scripts(const mparm_T *const parmp)
/// Get an environment variable, and execute it as Ex commands. /// Get an environment variable, and execute it as Ex commands.
/// ///
/// @param env environment variable to execute /// @param env environment variable to execute
/// @param is_viminit when true, called for VIMINIT
/// ///
/// @return FAIL if the environment variable was not executed, /// @return FAIL if the environment variable was not executed,
/// OK otherwise. /// OK otherwise.
static int process_env(char *env, bool is_viminit) static int process_env(char *env)
FUNC_ATTR_NONNULL_ALL
{ {
const char *initstr = os_getenv(env); const char *initstr = os_getenv(env);
if (initstr != NULL) { if (initstr != NULL) {
if (is_viminit) {
vimrc_found(NULL, NULL);
}
char_u *save_sourcing_name = sourcing_name; char_u *save_sourcing_name = sourcing_name;
linenr_T save_sourcing_lnum = sourcing_lnum; linenr_T save_sourcing_lnum = sourcing_lnum;
sourcing_name = (char_u *)env; sourcing_name = (char_u *)env;

View File

@@ -6583,15 +6583,13 @@ static void paste_option_changed(void)
/// When "fname" is not NULL, use it to set $"envname" when it wasn't set yet. /// When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
void vimrc_found(char_u *fname, char_u *envname) void vimrc_found(char_u *fname, char_u *envname)
{ {
char_u *p; if (fname != NULL && envname != NULL) {
char *p = vim_getenv((char *)envname);
if (fname != NULL) {
p = (char_u *)vim_getenv((char *)envname);
if (p == NULL) { if (p == NULL) {
/* Set $MYVIMRC to the first vimrc file found. */ // Set $MYVIMRC to the first vimrc file found.
p = (char_u *)FullName_save((char *)fname, FALSE); p = FullName_save((char *)fname, false);
if (p != NULL) { if (p != NULL) {
vim_setenv((char *)envname, (char *)p); vim_setenv((char *)envname, p);
xfree(p); xfree(p);
} }
} else { } else {

View File

@@ -65,7 +65,7 @@ char *stdpaths_get_xdg_var(const XDGVarType idx)
const char *env_val = os_getenv(env); const char *env_val = os_getenv(env);
#ifdef WIN32 #ifdef WIN32
if (env_val == NULL) { if (env_val == NULL && xdg_defaults_env_vars[idx] != NULL) {
env_val = os_getenv(xdg_defaults_env_vars[idx]); env_val = os_getenv(xdg_defaults_env_vars[idx]);
} }
#endif #endif

View File

@@ -10,6 +10,7 @@ local eval = helpers.eval
local eq = helpers.eq local eq = helpers.eq
local funcs = helpers.funcs local funcs = helpers.funcs
local insert = helpers.insert local insert = helpers.insert
local iswin = helpers.iswin
local neq = helpers.neq local neq = helpers.neq
local mkdir = helpers.mkdir local mkdir = helpers.mkdir
local rmdir = helpers.rmdir local rmdir = helpers.rmdir
@@ -170,8 +171,7 @@ describe('startup defaults', function()
end) end)
describe('$NVIM_LOG_FILE', function() describe('$NVIM_LOG_FILE', function()
-- TODO(jkeyes): use stdpath('data') instead. local datasubdir = iswin() and 'nvim-data' or 'nvim'
local datasubdir = helpers.iswin() and 'nvim-data' or 'nvim'
local xdgdir = 'Xtest-startup-xdg-logpath' local xdgdir = 'Xtest-startup-xdg-logpath'
local xdgdatadir = xdgdir..'/'..datasubdir local xdgdatadir = xdgdir..'/'..datasubdir
after_each(function() after_each(function()
@@ -428,7 +428,24 @@ end)
describe('stdpath()', function() describe('stdpath()', function()
-- Windows appends 'nvim-data' instead of just 'nvim' to prevent collisions
-- due to XDG_CONFIG_HOME and XDG_DATA_HOME being the same.
local datadir = iswin() and 'nvim-data' or 'nvim'
it('acceptance', function()
clear() -- Do not explicitly set any env vars.
eq('nvim', funcs.fnamemodify(funcs.stdpath('cache'), ':t'))
eq('nvim', funcs.fnamemodify(funcs.stdpath('config'), ':t'))
eq(datadir, funcs.fnamemodify(funcs.stdpath('data'), ':t'))
eq('table', type(funcs.stdpath('config_dirs')))
eq('table', type(funcs.stdpath('data_dirs')))
-- Check for crash. #8393
eq(2, eval('1+1'))
end)
context('returns a String', function() context('returns a String', function()
describe('with "config"' , function () describe('with "config"' , function ()
it('knows XDG_CONFIG_HOME', function() it('knows XDG_CONFIG_HOME', function()
clear({env={ clear({env={
@@ -463,32 +480,20 @@ describe('stdpath()', function()
end) end)
describe('with "data"' , function () describe('with "data"' , function ()
local appended_dir
setup(function()
-- Windows appends 'nvim-data' instead of just 'nvim' to
-- prevent collisions due to XDG_CONFIG_HOME and XDG_DATA_HOME
-- being the same.
if helpers.iswin() then
appended_dir = '/nvim-data'
else
appended_dir = '/nvim'
end
end)
it('knows XDG_DATA_HOME', function() it('knows XDG_DATA_HOME', function()
clear({env={ clear({env={
XDG_DATA_HOME=alter_slashes('/home/docwhat/.local'), XDG_DATA_HOME=alter_slashes('/home/docwhat/.local'),
}}) }})
eq(alter_slashes('/home/docwhat/.local' .. appended_dir), funcs.stdpath('data')) eq(alter_slashes('/home/docwhat/.local/'..datadir), funcs.stdpath('data'))
end) end)
it('handles changes during runtime', function() it('handles changes during runtime', function()
clear({env={ clear({env={
XDG_DATA_HOME=alter_slashes('/home/original'), XDG_DATA_HOME=alter_slashes('/home/original'),
}}) }})
eq(alter_slashes('/home/original' .. appended_dir), funcs.stdpath('data')) eq(alter_slashes('/home/original/'..datadir), funcs.stdpath('data'))
command("let $XDG_DATA_HOME='"..alter_slashes('/home/new').."'") command("let $XDG_DATA_HOME='"..alter_slashes('/home/new').."'")
eq(alter_slashes('/home/new' .. appended_dir), funcs.stdpath('data')) eq(alter_slashes('/home/new/'..datadir), funcs.stdpath('data'))
end) end)
it("doesn't expand $VARIABLES", function() it("doesn't expand $VARIABLES", function()
@@ -496,14 +501,14 @@ describe('stdpath()', function()
XDG_DATA_HOME='$VARIABLES', XDG_DATA_HOME='$VARIABLES',
VARIABLES='this-should-not-happen', VARIABLES='this-should-not-happen',
}}) }})
eq(alter_slashes('$VARIABLES' .. appended_dir), funcs.stdpath('data')) eq(alter_slashes('$VARIABLES/'..datadir), funcs.stdpath('data'))
end) end)
it("doesn't expand ~/", function() it("doesn't expand ~/", function()
clear({env={ clear({env={
XDG_DATA_HOME=alter_slashes('~/frobnitz'), XDG_DATA_HOME=alter_slashes('~/frobnitz'),
}}) }})
eq(alter_slashes('~/frobnitz' .. appended_dir), funcs.stdpath('data')) eq(alter_slashes('~/frobnitz/'..datadir), funcs.stdpath('data'))
end) end)
end) end)
@@ -544,7 +549,7 @@ describe('stdpath()', function()
context('returns a List', function() context('returns a List', function()
-- Some OS specific variables the system would have set. -- Some OS specific variables the system would have set.
local function base_env() local function base_env()
if helpers.iswin() then if iswin() then
return { return {
HOME='C:\\Users\\docwhat', -- technically, is not a usual PATH HOME='C:\\Users\\docwhat', -- technically, is not a usual PATH
HOMEDRIVE='C:', HOMEDRIVE='C:',