Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error output when configstore dir not accessible. #1512

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/cspell-lib/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/cspell-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@
"@cspell/dict-python": "^1.0.37",
"@types/configstore": "^5.0.1",
"@types/fs-extra": "^9.0.12",
"@types/jest": "^27.0.1",
"@types/node": "^16.6.0",
"cspell-dict-nl-nl": "^1.1.2",
"jest": "^27.0.6",
"lorem-ipsum": "^2.0.3",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"ts-jest": "^27.0.4"
}
}
34 changes: 28 additions & 6 deletions packages/cspell-lib/src/Settings/GlobalSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getGlobalConfigPath, getRawGlobalSettings, writeRawGlobalSettings } from './GlobalSettings';
import Configstore from 'configstore';
import { mocked } from 'ts-jest/utils';
// eslint-disable-next-line jest/no-mocks-import
import {
clearData as clearConfigstore,
Expand All @@ -8,17 +8,18 @@ import {
mockAll,
mockSetData,
} from '../__mocks__/configstore';
import { getGlobalConfigPath, getRawGlobalSettings, writeRawGlobalSettings } from './GlobalSettings';

const mockLog = jest.fn();
console.log = mockLog;
jest.mock('configstore');

const mockConfigstore = Configstore as jest.Mock<Configstore>;
const mockLog = jest.spyOn(console, 'log').mockImplementation();
const mockError = jest.spyOn(console, 'error').mockImplementation();
const mockConfigstore = mocked(Configstore, true);

describe('Validate GlobalSettings', () => {
beforeEach(() => {
mockConfigstore.mockClear();
mockLog.mockClear();
mockError.mockClear();
mockAll.mockClear();
clearMocks();
clearConfigstore();
});
Expand Down Expand Up @@ -84,4 +85,25 @@ describe('Validate GlobalSettings', () => {
const error2 = writeRawGlobalSettings(updated);
expect(error2).toBeInstanceOf(Error);
});

test('No Access to global settings files', () => {
mockAll.mockImplementation(() => {
throw new SystemLikeError('permission denied', 'EACCES');
});
const s = getRawGlobalSettings();
expect(mockError).toHaveBeenCalledTimes(0);
expect(mockLog).toHaveBeenCalledTimes(0);
expect(s).toEqual({
source: {
name: 'CSpell Configstore',
filename: undefined,
},
});
});
});

class SystemLikeError extends Error {
constructor(msg: string, readonly code: string) {
super(msg);
}
}
6 changes: 4 additions & 2 deletions packages/cspell-lib/src/Settings/GlobalSettings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CSpellSettings, CSpellSettingsWithSourceTrace } from '@cspell/cspell-types';
import { logError } from '../util/logger';
import ConfigStore from 'configstore';
import { logError } from '../util/logger';

const packageName = 'cspell';

Expand Down Expand Up @@ -34,7 +34,9 @@ export function getRawGlobalSettings(): GlobalSettingsWithSource {
};
}
} catch (error) {
logError(error);
if (!['ENOENT', 'EACCES', 'ENOTDIR', 'EISDIR'].includes(error.code)) {
logError(error);
}
}

return globalConf;
Expand Down
5 changes: 5 additions & 0 deletions packages/cspell-lib/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://github.com/facebook/jest/issues/11640
declare namespace NodeJS {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Global {}
}