Skip to content

Commit

Permalink
Multi root workspaces (#11)
Browse files Browse the repository at this point in the history
* Add multi-root workspace support for runAllTests command

* Add multi-root workspace support for runAllTestsInActiveFile command

* Add multi-root workspace support for runAllTestsInPath command

* Clean up code

* Fix broken tests

* Update changelog
  • Loading branch information
gediminasz authored Feb 4, 2024
1 parent 9b58078 commit bd908e2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 73 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased

- Added support for [multi-root workspaces](https://code.visualstudio.com/docs/editor/multi-root-workspaces).
- Removed support for settings being overridable at language level.

## 2023.7.0

- Added "Run Tests" context menu item to file explorer.
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,37 @@
"properties": {
"justTesting.baseCommand": {
"type": "string",
"scope": "language-overridable",
"scope": "resource",
"default": "python -m pytest -v",
"description": "Base terminal command"
},
"justTesting.runAllCommand": {
"type": "string",
"scope": "language-overridable",
"scope": "resource",
"default": "{base}",
"description": "Terminal command for \"Run all tests\""
},
"justTesting.runFileCommand": {
"type": "string",
"scope": "language-overridable",
"scope": "resource",
"default": "{base} {fileName}",
"description": "Terminal command for \"Run all tests in file\""
},
"justTesting.runOnCursorRegex": {
"type": "string",
"scope": "language-overridable",
"scope": "resource",
"default": "def (test_.+)\\(",
"description": "Regular expression for matching closest test name"
},
"justTesting.runOnCursorCommand": {
"type": "string",
"scope": "language-overridable",
"scope": "resource",
"default": "{base} {fileName} -k {testName}",
"description": "Terminal command for \"Run test on cursor\""
},
"justTesting.expressions": {
"type": "object",
"scope": "language-overridable",
"scope": "resource",
"default": {},
"description": "Custom expressions for template variables"
},
Expand Down
8 changes: 6 additions & 2 deletions src/commands/runAllTestsInPath.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const vscode = require('vscode')

const { runTerminalCommand } = require('../terminal')
const helpers = require('../helpers')

function runAllTestsInPath (extensionContext, configuration, uri) {
function runAllTestsInPath (extensionContext, _, uri) {
const configuration = vscode.workspace.getConfiguration('justTesting', uri)
const template = configuration.get('runFileCommand')
const fileName = helpers.asRelativePath(uri.path)
const context = {
Expand All @@ -12,7 +15,8 @@ function runAllTestsInPath (extensionContext, configuration, uri) {

const command = helpers.interpolate(template, context)

runTerminalCommand(extensionContext, command)
const workspaceFolder = vscode.workspace.getWorkspaceFolder(uri)
runTerminalCommand(extensionContext, command, workspaceFolder)
}

module.exports = { runAllTestsInPath }
4 changes: 2 additions & 2 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const vscode = require('vscode')

const { copyTestOnCursor } = require('./commands/copyTestOnCursor')
const { ExtensionError } = require('./errors')
const { getConfiguration } = require('./helpers')
const { getActiveConfiguration } = require('./helpers')
const { runAllTests } = require('./commands/runAllTests')
const { runAllTestsInActiveFile } = require('./commands/runAllTestsInActiveFile')
const { runAllTestsInPath } = require('./commands/runAllTestsInPath')
Expand All @@ -16,7 +16,7 @@ function activate (context) {
context.subscriptions.push(
vscode.commands.registerCommand(name, (...args) => {
try {
callback(context, getConfiguration(), ...args)
callback(context, getActiveConfiguration(), ...args)
} catch (e) {
if (e instanceof ExtensionError) {
vscode.window.showErrorMessage(e.message)
Expand Down
18 changes: 8 additions & 10 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const vscode = require('vscode')

function getActiveLanguageId () {
const editor = vscode.window.activeTextEditor
return editor && editor.document.languageId
}

function pathToModule (path) {
const components = path.split('/')
const baseName = components.pop()
const moduleName = baseName.split('.')[0]
return components.length ? components.join('.') + '.' + moduleName : moduleName
return [...components, moduleName].join('.')
}

function getConfiguration () {
return vscode.workspace.getConfiguration('justTesting', { languageId: getActiveLanguageId() })
function getActiveConfiguration () {
const editor = vscode.window.activeTextEditor
const scope = editor && editor.document.uri
return vscode.workspace.getConfiguration('justTesting', scope)
}

function interpolate (template, context) {
Expand All @@ -22,9 +19,10 @@ function interpolate (template, context) {
template
)
}

module.exports = {
getConfiguration,
asRelativePath: (path) => vscode.workspace.asRelativePath(path),
getActiveConfiguration,
asRelativePath: (path) => vscode.workspace.asRelativePath(path, false),
pathToModule,
interpolate
}
19 changes: 13 additions & 6 deletions src/terminal.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
const vscode = require('vscode')

const TERMINAL_NAME = 'Just Testing'
const LAST_COMMAND = 'lastCommand'

async function runTerminalCommand (extensionContext, command) {
async function runTerminalCommand (extensionContext, command, workspaceFolder = undefined) {
await vscode.workspace.saveAll()
extensionContext.workspaceState.update(LAST_COMMAND, command)

const terminal = obtainTerminal()
const terminal = obtainTerminal(workspaceFolder || getActiveWorkspaceFolder())
terminal.show(true)
terminal.sendText(command)
}

function obtainTerminal () {
const terminal = vscode.window.terminals.find(terminal => terminal.name === TERMINAL_NAME)
function obtainTerminal (workspaceFolder) {
const name = `Just Testing: ${workspaceFolder.name}`
const terminal = vscode.window.terminals.find(terminal => terminal.name === name)
if (terminal) return terminal
return vscode.window.createTerminal({ name, cwd: workspaceFolder.uri })
}

return vscode.window.createTerminal(TERMINAL_NAME)
function getActiveWorkspaceFolder () {
const editor = vscode.window.activeTextEditor
if (editor === undefined) {
return vscode.workspace.workspaceFolders[0]
}
return vscode.workspace.getWorkspaceFolder(editor.document.uri)
}

module.exports = { LAST_COMMAND, runTerminalCommand }
25 changes: 7 additions & 18 deletions tests/__mocks__/vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ const _lines = [
' assert True'
]

const _configuration = {
justTesting: {
python: new Map([
['baseCommand', 'pytest'],
['runFileCommand', '{base} {fileName}'],
['runOnCursorRegex', 'def (test_.+)\\('],
['runOnCursorCommand', '{base} {fileName} -k {testName}'],
['expressions', {
valueExpression: { value: 'static-value' },
regexExpression: { regex: 'def (.+_test)' }
}]
])
}
}

const vscode = {
window: {
activeTextEditor: {
Expand All @@ -36,7 +21,7 @@ const vscode = {
},
terminals: [
{
name: 'Just Testing',
name: 'Just Testing: foo',
show () {
this._wasShown = true
},
Expand All @@ -48,12 +33,16 @@ const vscode = {
},

workspace: {
getConfiguration (section, { languageId }) {
return _configuration[section][languageId]
_configuration: { justTesting: {} },
getConfiguration (section) {
return new Map(Object.entries(this._configuration[section]))
},
asRelativePath (path) {
return path.replace(/^\/root\//, '')
},
getWorkspaceFolder () {
return { name: 'foo', uri: '/root/' }
},
async saveAll () { return true }
}
}
Expand Down
60 changes: 31 additions & 29 deletions tests/commands/runAllTestsInPath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,61 @@ const { makeExtensionContext } = require('../helpers')

beforeEach(() => {
vscode.window.terminals[0]._lastCommand = undefined
vscode.workspace._configuration = {
justTesting: {
baseCommand: "pytest",
runFileCommand: "{base} {fileName}",
}
}
})

describe('runAllTestsInPath', () => {
it('runs tests in a selected file', async () => {
const extensionContext = makeExtensionContext()
const configuration = new Map([
['baseCommand', 'pytest'],
['runFileCommand', '{base} {fileName}']
])
const uri = { path: '/root/tests/foo/test_bar.py' }

await runAllTestsInPath(extensionContext, configuration, uri)
await runAllTestsInPath(extensionContext, undefined, uri)

expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests/foo/test_bar.py')
expect(extensionContext.workspaceState.get('lastCommand')).toBe('pytest tests/foo/test_bar.py')
})

it('runs tests in a selected directory', async () => {
const extensionContext = makeExtensionContext()
const configuration = new Map([
['baseCommand', 'pytest'],
['runFileCommand', '{base} {fileName}']
])
const uri = { path: '/root/tests/foo/' }

await runAllTestsInPath(extensionContext, configuration, uri)
await runAllTestsInPath(extensionContext, undefined, uri)

expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests/foo/')
})

it('runs tests in a selected file as module', async () => {
const extensionContext = makeExtensionContext()
const configuration = new Map([
['baseCommand', 'pytest'],
['runFileCommand', '{base} {module}']
])
const uri = { path: '/root/tests/foo/test_bar.py' }
describe('running tests as module', () => {
beforeEach(() => {
vscode.workspace._configuration = {
justTesting: {
baseCommand: "pytest",
runFileCommand: "{base} {module}",
}
}
})

await runAllTestsInPath(extensionContext, configuration, uri)
it('runs tests in a selected file as module', async () => {
const extensionContext = makeExtensionContext()
const uri = { path: '/root/tests/foo/test_bar.py' }

expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests.foo.test_bar')
})
await runAllTestsInPath(extensionContext, undefined, uri)

it('runs tests in a selected directory as module', async () => {
const extensionContext = makeExtensionContext()
const configuration = new Map([
['baseCommand', 'pytest'],
['runFileCommand', '{base} {module}']
])
const uri = { path: '/root/tests/foo' }
expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests.foo.test_bar')
})

it('runs tests in a selected directory as module', async () => {
const extensionContext = makeExtensionContext()
const uri = { path: '/root/tests/foo' }

await runAllTestsInPath(extensionContext, configuration, uri)
await runAllTestsInPath(extensionContext, undefined, uri)

expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests.foo')
expect(vscode.window.terminals[0]._lastCommand).toBe('pytest tests.foo')
})
})

})

0 comments on commit bd908e2

Please sign in to comment.