Skip to content

Commit

Permalink
Use readFile instead of openTextDocument for regex test case discovery (
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoah1 authored Jun 14, 2024
1 parent 49bbf50 commit a438085
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/language-tools/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {SourceFileTestCaseInfo, TestCaseInfo} from '../test-info/test-info'

const TEST_FILE_REGEX = /^(Test.+\.java|.+Test\.java)$/
const JAVA_TEST_REGEX =
/@Test\s+.*\s+public void (?<methodName>\w+)|public class (?<className>\w+Test)\s+extends/
/@Test\s+.*\s+public void (?<methodName>\w+)|public class (?<className>(Test\w*|\w+Test))\s+extends/
const PACKAGE_NAME_REGEX =
/package\s+(?<packageName>([a-zA-Z_][a-zA-Z0-9_]*)(\.[a-zA-Z_][a-zA-Z0-9_]*)*);/

Expand Down Expand Up @@ -80,8 +80,9 @@ export class JavaLanguageTools implements LanguageTools {
uri: vscode.Uri
): Promise<TestFileContents> {
// Get document text.
const textDocument = await vscode.workspace.openTextDocument(uri)
const text = textDocument.getText()
const fileContents = await vscode.workspace.fs.readFile(uri)
const text = fileContents.toString()
const lines = text.split('\n')

const testCases: DocumentTestItem[] = []
let classTestCase: DocumentTestItem | undefined
Expand All @@ -92,8 +93,16 @@ export class JavaLanguageTools implements LanguageTools {

let match
const regex = new RegExp(JAVA_TEST_REGEX, 'g')

let lineStartIndex = 0
let currentLine = 0
while ((match = regex.exec(text)) !== null) {
const position = textDocument.positionAt(match.index)
// Advance the current line each time there is a match.
while (lineStartIndex + lines[currentLine].length < match.index) {
lineStartIndex += lines[currentLine].length + 1 // +1 for the newline character.
currentLine++
}
const position = new vscode.Position(currentLine, 0)

// Each match will contain either the full Class, or an individual test method.
if (match.groups.className) {
Expand Down
8 changes: 8 additions & 0 deletions src/test-explorer/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,14 @@ export class TestResolver implements OnModuleInit, vscode.Disposable {
this.condenseTestItems(parentTest)

// Kick off test case resolution within each of the target's documents.
let counter = 1
for (const doc of allDocumentTestItems) {
updateDescription(
parentTest,
`Loading: analyzing test cases in file ${counter++} of ${
allDocumentTestItems.length
}`
)
await this.resolveDocumentTestCases(doc, cancellationToken)

if (doc.uri) {
Expand All @@ -280,6 +287,7 @@ export class TestResolver implements OnModuleInit, vscode.Disposable {
this.store.updateTestItemWatcher(doc.id, watcher)
}
}
updateDescription(parentTest)
}

private async resolveDocumentTestCases(
Expand Down
25 changes: 24 additions & 1 deletion src/test/suite/language-tools/java.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ suite('Java Language Tools', () => {
testController.dispose()
})

test('process test cases', async () => {
test('process test cases, example 1', async () => {
const result = await languageTools.getDocumentTestCases(
vscode.Uri.file(
path.join(fixtureDir, 'language_files', 'SampleValidExampleTest.java')
Expand All @@ -57,6 +57,29 @@ suite('Java Language Tools', () => {
assert.equal(result.documentTest?.name, 'SampleValidExampleTest')
})

test('process test cases, example 2', async () => {
const result = await languageTools.getDocumentTestCases(
vscode.Uri.file(
path.join(fixtureDir, 'language_files', 'TestSampleValidExample.java')
)
)

assert.strictEqual(result.isTestFile, true)
assert.strictEqual(result.testCases.length, 2)

const expectedBaseTestFilter =
'com.sample.project.common.client.samplepackage.TestSampleValidExample'

const expectedTests = ['testGetInstance', 'testGetSampleClient']
for (const test of result.testCases) {
assert.ok(expectedTests.includes(test.name))
assert.ok(test.testFilter.startsWith(expectedBaseTestFilter))
assert.ok(test.testFilter.endsWith(test.name))
}
assert.equal(result.documentTest?.testFilter, expectedBaseTestFilter)
assert.equal(result.documentTest?.name, 'TestSampleValidExample')
})

test('no matching test class name', async () => {
const result = await languageTools.getDocumentTestCases(
vscode.Uri.file(
Expand Down
26 changes: 26 additions & 0 deletions src/test/testdata/language_files/TestSampleValidExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sample.project.common.client.samplepackage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.uber.testing.base.TestBase;
import org.junit.Test;

public class TestSampleValidExample extends TestBase {
@Test
public void testGetInstance() {
SampleClientProvider instance =
SampleClientProvider.getInstance("sample");
assertNotNull(instance);
}

@Test
public void testGetSampleClient() {
SampleClientProvider instance =
SampleClientProvider.getInstance("sample");
StatementRenderingClient clientA = instance.getSampleClient();
StatementRenderingClient clientB = instance.getSampleClient();
assertNotNull(clientA);
assertEquals(clientA, clientB);
}
}

0 comments on commit a438085

Please sign in to comment.