testing

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 12, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Workspace Watcher Testing

This package contains tests for the WorkspaceWatcher component. The tests use a real filesystem and a mock LSP client to verify that the watcher correctly detects and reports file events.

Test Suite Overview

The test suite consists of the following tests:

1. Basic Functionality Tests
  • Tests file creation, modification, and deletion events
  • Confirms that appropriate notifications are sent to the LSP client
  • Verifies that each operation triggers the correct event type (Created, Changed, Deleted)
2. Exclusion Pattern Tests
  • Tests that files matching exclusion patterns are not reported
  • Specifically tests:
    • Files with excluded extensions (.tmp)
    • Files ending with tilde (~)
    • Files in excluded directories (.git)
    • Files matching gitignore patterns
3. Debouncing Tests
  • Tests that rapid changes to the same file result in a single notification
  • Verifies the debouncing mechanism works correctly

Mock LSP Client

The MockLSPClient implements the watcher.LSPClient interface and provides functionality for:

  • Recording file events
  • Testing if files are open
  • Opening files
  • Notifying about file changes
  • Waiting for events with a timeout

Running the Tests

To run the tests:

go test -v ./internal/watcher/testing

For more detailed output, enable debug logging:

go test -v -tags debug ./internal/watcher/testing

Known Issues and Limitations

  1. Gitignore Integration:

    • The watcher uses the go-gitignore package to parse and match gitignore patterns.
    • The tests verify that files matching gitignore patterns are excluded from notifications.
    • Additional tests in gitignore_test.go verify more complex patterns and matching scenarios.
  2. File Deletion in Excluded Directories:

    • Since excluded directories are not watched, file deletion events in these directories are not detected.
  3. Large Binary Files:

    • The tests don't verify the handling of large binary files due to test resource limitations.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileEvent

type FileEvent struct {
	URI  string
	Type protocol.FileChangeType
}

FileEvent represents a file event notification

type MockLSPClient

type MockLSPClient struct {
	// contains filtered or unexported fields
}

MockLSPClient implements the watcher.LSPClient interface for testing

func NewMockLSPClient

func NewMockLSPClient() *MockLSPClient

NewMockLSPClient creates a new mock LSP client for testing

func (*MockLSPClient) CountEvents

func (m *MockLSPClient) CountEvents(uri string, eventType protocol.FileChangeType) int

CountEvents counts events for a specific file and event type

func (*MockLSPClient) DidChangeWatchedFiles

func (m *MockLSPClient) DidChangeWatchedFiles(ctx context.Context, params protocol.DidChangeWatchedFilesParams) error

DidChangeWatchedFiles mocks sending watched file events to the server

func (*MockLSPClient) GetEvents

func (m *MockLSPClient) GetEvents() []FileEvent

GetEvents returns a copy of all recorded events

func (*MockLSPClient) IsFileOpen

func (m *MockLSPClient) IsFileOpen(path string) bool

IsFileOpen checks if a file is already open in the editor

func (*MockLSPClient) NotifyChange

func (m *MockLSPClient) NotifyChange(ctx context.Context, path string) error

NotifyChange mocks notifying the server of a file change

func (*MockLSPClient) OpenFile

func (m *MockLSPClient) OpenFile(ctx context.Context, path string) error

OpenFile mocks opening a file in the editor

func (*MockLSPClient) ResetEvents

func (m *MockLSPClient) ResetEvents()

ResetEvents clears the recorded events and drains any pending notification signals from previous activity (e.g. workspace scan, prior subtests). Without the drain, WaitForEvent could return immediately on a stale signal before the watcher's debounce fires for the new event under test, racing with the CountEvents check and producing the "No create event received" flake.

func (*MockLSPClient) SetFileWatchHandler added in v0.3.0

func (m *MockLSPClient) SetFileWatchHandler(handler func(id string, watchers []protocol.FileSystemWatcher))

SetFileWatchHandler satisfies the watcher.LSPClient interface. Tests using MockLSPClient don't exercise server-driven file-watch registrations, so this is intentionally a no-op.

func (*MockLSPClient) WaitForEvent

func (m *MockLSPClient) WaitForEvent(ctx context.Context) bool

WaitForEvent waits for at least one event to be received or context to be done

func (*MockLSPClient) WaitForEventType

func (m *MockLSPClient) WaitForEventType(ctx context.Context, uri string, eventType protocol.FileChangeType) bool

WaitForEventType waits until an event matching uri+eventType has been recorded, or the context is done. This is the race-free variant for tests that assert on a specific event: WaitForEvent returns on the *first* signal, but a newly-created file produces both a Changed and a Created event (the watcher routes the Write-after-Create through NotifyChange because it just auto-opened the file). Tests waiting for the Created event must keep draining signals until the matching event lands.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL