Skip to content
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
35 changes: 0 additions & 35 deletions tests/__mocks__/filesystem.ts

This file was deleted.

38 changes: 24 additions & 14 deletions tests/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,28 @@ import { vi } from 'vitest'

const vscode = createVSCodeMock(vi)

export const Uri = vscode.Uri
export const workspace = vscode.workspace
export const Range = vscode.Range
export const Position = vscode.Position
export const Location = vscode.Location
export const Selection = vscode.Selection
export const CodeAction = vscode.CodeAction
export const CodeActionKind = vscode.CodeActionKind
export const WorkspaceEdit = vscode.WorkspaceEdit
export const ThemeColor = vscode.ThemeColor
export const ThemeIcon = vscode.ThemeIcon
export const TreeItem = vscode.TreeItem
export const TreeItemCollapsibleState = vscode.TreeItemCollapsibleState
export const Disposable = vscode.Disposable
export const {
Uri,
workspace,
Range,
Position,
Location,
Selection,
ThemeColor,
ThemeIcon,
TreeItem,
TreeItemCollapsibleState,
Disposable,
MarkdownString,
CompletionItem,
CompletionItemKind,
CodeAction,
CodeActionKind,
WorkspaceEdit,
DiagnosticSeverity,
DiagnosticTag,
window,
languages,
} = vscode

export default vscode
42 changes: 0 additions & 42 deletions tests/filesystem.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tests/memoize.test.ts → tests/utils/memoize.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { memoize } from '../src/utils/memoize'
import { memoize } from '../../src/utils/memoize'

describe('memoize', () => {
it('should cache sync function result', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/package.test.ts → tests/utils/package.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { encodePackageName } from '../src/utils/package'
import { encodePackageName } from '../../src/utils/package'

describe('encodePackageName', () => {
it('should encode regular package name', () => {
Expand Down
39 changes: 11 additions & 28 deletions tests/resolve.test.ts → tests/utils/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { describe, expect, it } from 'vitest'
import { Uri } from 'vscode'
import { findNearestFile, walkAncestors } from '../src/utils/resolve'
import { mockFileSystem } from './__mocks__/filesystem'
import { findNearestFile, walkAncestors } from '../../src/utils/resolve'

const root = Uri.file(process.cwd())

describe('walkAncestors', () => {
it('should yield all ancestor directories', () => {
Expand Down Expand Up @@ -34,44 +35,26 @@ describe('walkAncestors', () => {
})

describe('findNearestFile', () => {
beforeEach(() => {
vi.resetAllMocks()
})

it('should find a file in a parent directory', async () => {
mockFileSystem({
'/a/b/target.txt': '',
})

const result = await findNearestFile('target.txt', Uri.file('/a/b/c/d'))
const result = await findNearestFile('package.json', Uri.joinPath(root, 'src/utils'))
expect(result).toBeDefined()
expect(result!.path).toBe('/a/b/target.txt')
expect(result!.fsPath).toBe(Uri.joinPath(root, 'package.json').fsPath)
})

it('should return the closest match', async () => {
mockFileSystem({
'/a/target.txt': '',
'/a/b/c/target.txt': '',
})

const result = await findNearestFile('target.txt', Uri.file('/a/b/c/d'))
const result = await findNearestFile('package.json', Uri.joinPath(root, 'playground'))
expect(result).toBeDefined()
expect(result!.path).toBe('/a/b/c/target.txt')
expect(result!.fsPath).toBe(Uri.joinPath(root, 'playground/package.json').fsPath)
})

it('should return undefined when file is not found', async () => {
mockFileSystem({})

const result = await findNearestFile('target.txt', Uri.file('/a/b/c'))
const result = await findNearestFile('__nonexistent_file__', Uri.joinPath(root, 'src'))
expect(result).toBeUndefined()
})

it('should respect shouldStop', async () => {
mockFileSystem({
'/a/target.txt': '',
})

const result = await findNearestFile('target.txt', Uri.file('/a/b/c'), (u) => u.path === '/a/b')
const stop = Uri.joinPath(root, 'src')
const result = await findNearestFile('package.json', Uri.joinPath(root, 'src/utils'), (u) => u.fsPath === stop.fsPath)
expect(result).toBeUndefined()
})
})
2 changes: 1 addition & 1 deletion tests/version.test.ts → tests/utils/version.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { getPrereleaseId, lt, parseVersion } from '../src/utils/version'
import { getPrereleaseId, lt, parseVersion } from '../../src/utils/version'

describe('parseVersion', () => {
it('should parse plain version', () => {
Expand Down