Skip to content

Commit df93eef

Browse files
authored
fix: ignore case of disk symbol when check workspace on windows (#124)
1 parent a524e5c commit df93eef

File tree

5 files changed

+86
-6
lines changed

5 files changed

+86
-6
lines changed

__mocks__/vscode.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const ThemeColor = jest.fn();
9898
const EventEmitter = jest.fn().mockImplementation(() => {
9999
return {
100100
fire: jest.fn(),
101+
event: jest.fn(),
101102
};
102103
});
103104

@@ -106,8 +107,15 @@ const QuickPickItemKind = {
106107
Default: 0,
107108
};
108109

110+
const TreeItem = jest.fn();
111+
const Disposable = jest.fn();
112+
(Disposable as any).from = jest.fn();
113+
const authentication = { registerAuthenticationProvider: jest.fn() };
114+
109115
export = {
110116
ThemeColor,
117+
Disposable,
118+
authentication,
111119
CodeLens,
112120
languages,
113121
StatusBarAlignment,
@@ -132,4 +140,5 @@ export = {
132140
TestRunRequest,
133141
ViewColumn,
134142
QuickPickItemKind,
143+
TreeItem,
135144
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { workspace } from 'vscode';
2+
3+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
4+
describe('isTargetWorkspace', () => {
5+
const testItems: [current: string, target: string, result: boolean][] = [
6+
['c:/test', 'C:/test', true],
7+
['C:/test', 'c:/test', true],
8+
['c:/test', 'c:/test', true],
9+
['D:/test', 'D:/test', true],
10+
['c:/test', 'd:/test', false],
11+
['d:/test', 'd:/test/', false],
12+
];
13+
let mockSettings: jest.Mock<any, any, any>;
14+
15+
beforeAll(() => {
16+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
17+
const actualGlobalContext = jest.requireActual('./global-state.ts').globalContext;
18+
Object.defineProperty(actualGlobalContext, 'extensionName', {
19+
get: jest.fn().mockReturnValue(''),
20+
});
21+
jest.mock('./global-state', () => ({
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
23+
globalContext: actualGlobalContext,
24+
}));
25+
26+
jest.mock('os', () => ({ platform: jest.fn().mockReturnValue('win32') }));
27+
mockSettings = jest.fn();
28+
jest.mock('./settings.service', () => ({
29+
// eslint-disable-next-line @typescript-eslint/naming-convention
30+
Settings: mockSettings,
31+
}));
32+
});
33+
34+
for (const item of testItems) {
35+
const [current, target, isInTargetWorkspace] = item;
36+
37+
it(`should${isInTargetWorkspace ? '' : ' not'} in target workspace, ${current}, ${target}`, async () => {
38+
const mockedWorkspace = jest.mocked(workspace);
39+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
40+
// @ts-ignore
41+
mockedWorkspace.workspaceFolders = [{ name: '', uri: { path: current } }];
42+
43+
(mockSettings as any).workspaceUri = { path: target };
44+
45+
const { isTargetWorkspace } = await import('@/services/check-workspace');
46+
expect(isTargetWorkspace()).toStrictEqual(isInTargetWorkspace);
47+
});
48+
}
49+
});

src/services/check-workspace.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
import os from 'os';
12
import { commands, workspace } from 'vscode';
23
import { refreshPostCategoriesList } from '../commands/post-category/refresh-post-categories-list';
34
import { refreshPostsList } from '../commands/posts-list/refresh-posts-list';
45
import { globalContext } from './global-state';
56
import { PostFileMapManager } from './post-file-map';
67
import { Settings } from './settings.service';
78

9+
const diskSymbolRegex = /^(\S{1,5}:)(.*)/;
10+
811
export const isTargetWorkspace = (): boolean => {
912
const folders = workspace.workspaceFolders;
10-
const isTarget = !!folders && folders.length === 1 && folders[0].uri.path === Settings.workspaceUri.path;
13+
let currentFolder = folders?.length === 1 ? folders[0].uri.path : undefined;
14+
let targetFolder = Settings.workspaceUri.path;
15+
const platform = os.platform();
16+
if (platform === 'win32' && targetFolder && currentFolder) {
17+
const replacer = (sub: string, m0: string | null | undefined, m2: string | null | undefined) =>
18+
m0 && m2 ? m0.toLowerCase() + m2 : sub;
19+
currentFolder = currentFolder.replace(diskSymbolRegex, replacer);
20+
targetFolder = targetFolder.replace(diskSymbolRegex, replacer);
21+
}
22+
const isTarget = !!currentFolder && currentFolder === targetFolder;
1123
void commands.executeCommand('setContext', `${globalContext.extensionName}.isTargetWorkspace`, isTarget);
1224
return isTarget;
1325
};

test/jest.config.mjs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ const config = {
1414
testPathIgnorePatterns: ['<rootDir>/src/test/suite'],
1515
testEnvironment: 'node',
1616
detectOpenHandles: true,
17-
moduleFileExtensions: ['js', 'ts', 'json'],
17+
moduleFileExtensions: ['js', 'ts', 'json', 'mjs'],
1818
collectCoverage: false,
19-
transform: { '^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }] },
19+
transform: {
20+
'^.+\\.(mjs|ts|js|cjs|jsx)$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json', useESM: true }],
21+
},
2022
rootDir: '../',
23+
globals: {
24+
// eslint-disable-next-line @typescript-eslint/naming-convention
25+
CNBLOGS_CLIENTID: '',
26+
// eslint-disable-next-line @typescript-eslint/naming-convention
27+
CNBLOGS_CLIENTSECRET: '',
28+
},
2129
forceExit: true,
2230
roots: ['<rootDir>'],
2331
verbose: true,
2432
modulePaths: ['<rootDir>', '<rootDir>/node_modules'],
2533
moduleDirectories: ['<rootDir>', '<rootDir>/node_modules'],
26-
transformIgnorePatterns: ['/node_modules/(?!(got|got-fetch)/)'],
34+
transformIgnorePatterns: [
35+
'node_modules/(?!(got-fetch|got|@sindresorhus|p-cancelable|@szmarczak|cacheable-request|cacheable-lookup|normalize-url|responselike|lowercase-keys|mimic-response|form-data-encoder))',
36+
],
2737
projects: ['<rootDir>'],
2838
automock: false,
2939
moduleNameMapper: {

tsconfig.spec.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"compilerOptions": {
66
"rootDir": "",
77
"resolveJsonModule": true,
8-
"moduleResolution": "node16",
98
"skipLibCheck": true,
109
"esModuleInterop": true,
1110
"allowJs": true,
12-
"outDir": "out"
11+
"outDir": "out",
12+
"module": "CommonJS"
1313
}
1414
}

0 commit comments

Comments
 (0)