Skip to content

Commit 99bdf7c

Browse files
committed
refactor: use vscode.glob instead of glob
1 parent 3723927 commit 99bdf7c

3 files changed

Lines changed: 53 additions & 55 deletions

File tree

packages/vscode-wdio-config/src/find.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
import fs from 'node:fs/promises'
2-
import path from 'node:path'
32

43
import { log } from '@vscode-wdio/logger'
54
import { normalizePath } from '@vscode-wdio/utils'
6-
import { glob } from 'glob'
5+
import * as vscode from 'vscode'
76

87
export async function findWdioConfig(workSpaceRoot: string, configFilePattern: string[]) {
98
log.debug(`Target workspace path: ${workSpaceRoot}`)
109
log.debug(`Detecting the configuration file for WebdriverIO...: ${configFilePattern.join(', ')}`)
10+
const globResult = await Promise.all(
11+
configFilePattern.map((p) =>
12+
vscode.workspace.findFiles(new vscode.RelativePattern(workSpaceRoot, p), '**/node_modules/**')
13+
)
14+
)
1115

12-
const wdioConfigPaths = await glob(configFilePattern, {
13-
cwd: workSpaceRoot,
14-
withFileTypes: false,
15-
ignore: '**/node_modules/**',
16-
})
16+
const wdioConfigPaths = globResult.flatMap((uri) => uri).map((uri) => uri.fsPath)
1717

1818
const configs = (
1919
await Promise.all(
20-
wdioConfigPaths.map(async (_wdioConfigPath) => {
21-
const wdioConfigPath = path.join(workSpaceRoot, _wdioConfigPath)
20+
wdioConfigPaths.map(async (wdioConfigPath) => {
2221
log.debug(`Checking the path: ${wdioConfigPath}`)
2322
try {
2423
await fs.access(wdioConfigPath, fs.constants.R_OK)
Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import fs from 'node:fs/promises'
22
import path from 'node:path'
33

4-
import { glob } from 'glob'
54
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
5+
import * as vscode from 'vscode'
66

77
import { findWdioConfig } from '../src/find.js'
88

99
// Mock dependencies
1010
vi.mock('node:fs/promises')
1111
vi.mock('glob')
1212

13-
vi.mock('vscode', async () => import('../../../tests/__mocks__/vscode.cjs'))
13+
vi.mock('vscode', async () => {
14+
const vscode = await import('../../../tests/__mocks__/vscode.cjs')
15+
return {
16+
...vscode,
17+
RelativePattern: vi.fn(),
18+
workspace: {
19+
findFiles: vi.fn(),
20+
},
21+
}
22+
})
1423
vi.mock('@vscode-wdio/logger', () => import('../../../tests/__mocks__/logger.js'))
1524

1625
describe('findWdioConfig', () => {
@@ -19,6 +28,13 @@ describe('findWdioConfig', () => {
1928

2029
beforeEach(() => {
2130
vi.resetAllMocks()
31+
vi.mocked(vscode.RelativePattern).mockImplementation(function (this: any, r: unknown, p: string) {
32+
this.path = p
33+
this.root = r
34+
this.toString = function () {
35+
return path.join(this.root, this.path)
36+
}
37+
} as any)
2238
})
2339

2440
afterEach(() => {
@@ -30,9 +46,9 @@ describe('findWdioConfig', () => {
3046
const mockConfigFiles = ['wdio.conf.js', 'wdio.conf.ts']
3147
const expectedPaths = mockConfigFiles.map((file) => path.join(mockWorkspaceRoot, file))
3248

33-
// Mock glob to return the config files
34-
vi.mocked(glob).mockResolvedValue(mockConfigFiles)
35-
49+
vi.mocked(vscode.workspace.findFiles).mockImplementation(async (p: vscode.GlobPattern) => {
50+
return [vscode.Uri.file(p.toString())]
51+
})
3652
// Mock fs.access to succeed for all files
3753
vi.mocked(fs.access).mockResolvedValue(undefined)
3854

@@ -41,21 +57,22 @@ describe('findWdioConfig', () => {
4157

4258
// Verify
4359
expect(result).toEqual(expectedPaths)
44-
expect(glob).toHaveBeenCalledWith(defaultConfigFilePattern, {
45-
cwd: mockWorkspaceRoot,
46-
withFileTypes: false,
47-
ignore: '**/node_modules/**',
48-
})
4960
expect(fs.access).toHaveBeenCalledTimes(2)
5061
})
5162

5263
it('should filter out inaccessible files', async () => {
5364
// Setup
54-
const mockConfigFiles = ['wdio.conf.js', 'wdio.conf.ts']
5565
const accessibleFile = path.join(mockWorkspaceRoot, 'wdio.conf.js')
5666

57-
// Mock glob to return the config files
58-
vi.mocked(glob).mockResolvedValue(mockConfigFiles)
67+
// vi.mocked(vscode.workspace.findFiles).mockImplementation(async (p:vscode.GlobPattern)=>{
68+
// if (p.toString() === accessibleFile) {
69+
// return [vscode.Uri.file(p.toString())]
70+
// }
71+
// return []
72+
// })
73+
vi.mocked(vscode.workspace.findFiles).mockImplementation(async (p: vscode.GlobPattern) => {
74+
return [vscode.Uri.file(p.toString())]
75+
})
5976

6077
// Mock fs.access to succeed only for the JS file
6178
vi.mocked(fs.access).mockImplementation(async (filePath) => {
@@ -75,10 +92,9 @@ describe('findWdioConfig', () => {
7592

7693
it('should return empty array when no config files are accessible', async () => {
7794
// Setup
78-
const mockConfigFiles = ['wdio.conf.js', 'wdio.conf.ts']
79-
80-
// Mock glob to return the config files
81-
vi.mocked(glob).mockResolvedValue(mockConfigFiles)
95+
vi.mocked(vscode.workspace.findFiles).mockImplementation(async (p: vscode.GlobPattern) => {
96+
return [vscode.Uri.file(p.toString())]
97+
})
8298

8399
// Mock fs.access to fail for all files
84100
vi.mocked(fs.access).mockRejectedValue(new Error('File not found'))
@@ -93,8 +109,7 @@ describe('findWdioConfig', () => {
93109

94110
it('should handle empty glob results', async () => {
95111
// Setup
96-
// Mock glob to return no files
97-
vi.mocked(glob).mockResolvedValue([])
112+
vi.mocked(vscode.workspace.findFiles).mockResolvedValue([])
98113

99114
// Execute
100115
const result = await findWdioConfig(mockWorkspaceRoot, defaultConfigFilePattern)
@@ -103,28 +118,4 @@ describe('findWdioConfig', () => {
103118
expect(result).toEqual([])
104119
expect(fs.access).not.toHaveBeenCalled()
105120
})
106-
107-
it('should use custom config file pattern when provided', async () => {
108-
// Setup
109-
const customConfigPattern = ['custom.wdio.conf.js']
110-
const mockConfigFiles = ['custom.wdio.conf.js']
111-
const expectedPaths = mockConfigFiles.map((file) => path.join(mockWorkspaceRoot, file))
112-
113-
// Mock glob to return the config files
114-
vi.mocked(glob).mockResolvedValue(mockConfigFiles)
115-
116-
// Mock fs.access to succeed
117-
vi.mocked(fs.access).mockResolvedValue(undefined)
118-
119-
// Execute
120-
const result = await findWdioConfig(mockWorkspaceRoot, customConfigPattern)
121-
122-
// Verify
123-
expect(result).toEqual(expectedPaths)
124-
expect(glob).toHaveBeenCalledWith(customConfigPattern, {
125-
cwd: mockWorkspaceRoot,
126-
withFileTypes: false,
127-
ignore: '**/node_modules/**',
128-
})
129-
})
130121
})

vitest.config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1-
import { defineConfig } from 'vitest/config'
1+
import { defineConfig, defaultExclude } from 'vitest/config'
22

33
export default defineConfig({
4+
// https://github.com/vitest-dev/vitest/discussions/6662
5+
server: {
6+
watch: {
7+
ignored: ['**/dist/**'],
8+
},
9+
},
10+
411
test: {
12+
projects: ['packages/*'],
513
dangerouslyIgnoreUnhandledErrors: true,
614
include: ['packages/**/*.test.ts'],
715
/**
816
* not to ESM ported packages
917
*/
10-
exclude: ['dist', 'out', 'coverage', '.idea', '.git', '.vscode-test', '.cache', '**/node_modules/**'],
18+
exclude: [...defaultExclude, '**/coverage/**', '.vscode-test'],
1119
env: {
1220
WDIO_UNIT_TESTING: '1',
1321
},
@@ -17,7 +25,7 @@ export default defineConfig({
1725
include: ['packages/*/src/**'],
1826
provider: 'v8',
1927
reportOnFailure: true,
20-
exclude: ['**/node_modules/**', 'packages/wdio-vscode-types/src/**'],
28+
exclude: [...defaultExclude, 'packages/wdio-vscode-types/src/**'],
2129
watermarks: {
2230
statements: [85, 90],
2331
functions: [83, 88],

0 commit comments

Comments
 (0)