Skip to content

Commit

Permalink
fix: error output when configstore dir not accessible. (#1512)
Browse files Browse the repository at this point in the history
fix: #1510
  • Loading branch information
Jason3S authored Aug 13, 2021
1 parent 6a44e26 commit 68a63d1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
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 {}
}

0 comments on commit 68a63d1

Please sign in to comment.