Skip to content

Commit

Permalink
Fix incorrect paths on Linux (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oden authored and sindresorhus committed Jan 10, 2017
1 parent 34f4cd8 commit 3a2ba84
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ const windows = name => {
};
};

// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
const linux = name => {
const username = path.basename(homedir);

return {
data: env.XDG_DATA_HOME || path.join(homedir, '.local', 'share', name),
config: env.XDG_CONFIG_HOME || path.join(homedir, '.config', name),
cache: env.XDG_CACHE_HOME || path.join(homedir, '.cache', name),
data: path.join(env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'), name),
config: path.join(env.XDG_CONFIG_HOME || path.join(homedir, '.config'), name),
cache: path.join(env.XDG_CACHE_HOME || path.join(homedir, '.cache'), name),
// https://wiki.debian.org/XDGBaseDirectorySpecification#state
log: env.XDG_STATE_HOME || path.join(homedir, '.local', 'state', name),
log: path.join(env.XDG_STATE_HOME || path.join(homedir, '.local', 'state'), name),
temp: path.join(tmpdir, username, name)
};
};
Expand Down
24 changes: 24 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,27 @@ test('no suffix', t => {
const paths = m(name, opts);
t.true(paths.data.endsWith(name));
});

// linux-specific tests
if (process.platform === 'linux') {
test('correct paths with XDG_*_HOME set', t => {
const envVars = {
data: 'XDG_DATA_HOME',
config: 'XDG_CONFIG_HOME',
cache: 'XDG_CACHE_HOME',
log: 'XDG_STATE_HOME'
};

Object.values(envVars).forEach(env => {
process.env[env] = `/tmp/${env}`;
});

const name = 'unicorn';
const paths = m(name);

Object.keys(envVars).forEach(env => {
const expectedPath = process.env[envVars[env]];
t.true(paths[env].startsWith(expectedPath) && paths[env].endsWith(`${name}-nodejs`));
});
});
}

0 comments on commit 3a2ba84

Please sign in to comment.